Skip to content

Commit

Permalink
Merge branch 'genouest:master' into project-description-requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
rsiminel authored May 30, 2024
2 parents b698807 + 6b6a2ac commit 08c3f63
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 176 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## 1.4.30 (TBA)

* Added 'End date' to the projet request page
* Set 'description' as a required field in the projet request page and set a minimal length of 30 char
* Added 'End date' to the project request page
* Set 'description' as a required field in the project request page and set a minimal length of 30 char
* Added a link to user profile on admin panels -> projects pending table
* Added 'Extend' button for admins to extend active TPs
* Prevented retroactive TP reservations

## 1.4.29

Expand Down
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ app.delete('/tp/:id', tp);
app.get('/tp/:id', tp);
app.put('/tp/:id/reserve/now', tp);
app.put('/tp/:id/reserve/stop', tp);
app.put('/tp/:id/reserve/extend', tp);

app.get('/auth', auth);
app.post('/auth/:id', auth);
Expand Down
151 changes: 108 additions & 43 deletions core/tps.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var STATUS_EXPIRED = 'Expired';
exports.tp_reservation = tp_reservation;
exports.create_tp_reservation = create_tp_reservation;
exports.remove_tp_reservation = remove_tp_reservation;
exports.extend_tp_reservation = extend_tp_reservation;


async function createExtraGroup(trainingName, ownerName) {
Expand Down Expand Up @@ -86,18 +87,36 @@ async function deleteExtraProject(project) {
}


async function extendExtraProject(project, extension) {
if (project === undefined || project === null) {
return false;
}
let project_to_extend = await dbsrv.mongo_projects().findOne({'id': project});
if(!project_to_extend) {
logger.error('Cant find project to extend ' + project);
return false;
}
try {
const extended_project = { ...project_to_extend, expire: extension.to };
let res = await prjsrv.update_project(project_to_extend.id, extended_project);
return res;
} catch(error) {
logger.error(error);
}
return false;
}


async function create_tp_users_db (owner, quantity, duration, end_date, userGroup, userProject) {
// Duration in days
logger.debug('create_tp_users ', owner, quantity, duration);
let startnbr = await idsrv.getUserAvailableId();

let groupName = '';
let projectName = '';

if (userGroup && userGroup.name) {
groupName = userGroup.name;
}

if (userProject && userProject.id) {
projectName = userProject.id;
}
Expand All @@ -113,7 +132,6 @@ async function create_tp_users_db (owner, quantity, duration, end_date, userGrou
} else {
tp_email = tp_email.replace('${id}', CONFIG.tp.prefix + startnbr);
}

let user = {
uid: CONFIG.tp.prefix + startnbr,
firstname: CONFIG.tp.prefix,
Expand Down Expand Up @@ -144,14 +162,12 @@ async function create_tp_users_db (owner, quantity, duration, end_date, userGrou
if (projectName != '') {
await usrsrv.add_user_to_project(userProject.id, user.uid, 'auto', false);
}

await plgsrv.run_plugins('activate', user.uid, user, 'auto');

}
}
catch (error) {
} catch (error) {
logger.error('Error', error);
}

return users;
}

Expand Down Expand Up @@ -192,81 +208,124 @@ async function send_user_passwords(owner, from_date, to_date, users, group) {


async function delete_tp_user(user) {

logger.debug('delete_tp_user', user.uid);
try{
try {
await fdbs.delete_dbs(user);
await fwebs.delete_webs(user);
await usrsrv.delete_user(user);
await plgsrv.run_plugins('remove', user.uid, user, 'auto@tp');
}
catch(exception) {
} catch(exception) {
logger.error(exception);
}
}

async function delete_tp_users(users) {
for(let i=0;i<users.length;i++) {
for(let i = 0; i < users.length; i++) {
let user = await dbsrv.mongo_users().findOne({'uid': users[i]});
if (user && user.uid) {
await delete_tp_user(user);
}
}

}


async function remove_tp_reservation(reservation_id) {
async function extend_tp_user(user, extension) {
logger.debug('extend_tp_user', user.uid);
try{
user.history.push( {
'action': 'Extend user until ' + new Date(extension.to).toDateString(),
date: new Date().getTime()
});
await dbsrv.mongo_users().updateOne({uid: user.uid}, {'$set': {
expiration: extension.to,
history: user.history
}});
}
catch(exception) {
logger.error(exception);
}
}

async function extend_tp_users(users, extension) {
for(let i = 0; i < users.length; i++) {
let user = await dbsrv.mongo_users().findOne({'uid': users[i]});
if (user && user.uid) {
await extend_tp_user(user, extension);
}
}
}

logger.info('Close reservation', reservation_id);

let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id});
async function remove_tp_reservation(reservation) {
logger.debug('Close reservation', reservation);

if (reservation.accounts)
{
logger.info('Delete Account', reservation.accounts);
if (reservation.accounts) {
logger.info('Delete Accounts', reservation.accounts);
await delete_tp_users(reservation.accounts);
}

if (reservation.group && reservation.group.name && reservation.group.name != '') {
logger.info('Delete Group', reservation.group.name);
await deleteExtraGroup(reservation.group.name);
}

if (reservation.project && reservation.project.id && reservation.project.id != '') {
logger.info('Delete Project', reservation.project.id);
await deleteExtraProject(reservation.project.id);
}

try {
await dbsrv.mongo_reservations().updateOne({'_id': reservation_id}, {
'$set': {
'over': true
}
await dbsrv.mongo_reservations().updateOne({'_id': reservation._id}, {'$set': {
'over': true
}});
await dbsrv.mongo_events().insertOne( {
'owner': 'auto',
'date': new Date().getTime(),
'action': 'close reservation for ' + reservation.owner ,
'logs': []
});
await dbsrv.mongo_events().insertOne({ 'owner': 'auto', 'date': new Date().getTime(), 'action': 'close reservation for ' + reservation.owner , 'logs': [] });
} catch (error) {
logger.error(error);
}
}

async function extend_tp_reservation(reservation, extension) {
logger.debug('Extend reservation', reservation);

try {
await dbsrv.mongo_reservations().updateOne({ '_id': reservation._id }, {
'$set': { 'to': extension.to }
});
await dbsrv.mongo_events().insertOne( {
'owner': 'auto',
'date': new Date().getTime(),
'action': 'extend reservation for ' + reservation.owner ,
'logs': []});
} catch (error) {
logger.error(error);
}

// add grace period to extension
extension = {...extension, to: extension.to + 1000*3600*24*(CONFIG.tp.extra_expiration)};
if (reservation.accounts) {
logger.info('Extend Accounts', reservation.accounts);
await extend_tp_users(reservation.accounts, extension);
}
if (reservation.project && reservation.project.id && reservation.project.id != '') {
logger.info('Extend Project', reservation.project.id);
await extendExtraProject(reservation.project.id, extension);
}
}

async function create_tp_reservation(reservation_id) {

logger.info('Create reservation', reservation_id);

// Create users for reservation
let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id});
if(!reservation) {
throw {code: 404, message: 'Reservation not found'};
}

if (!reservation.name) {
reservation.name = 'tp';
}

let trainingName = latinize(reservation.name.toLowerCase()).replace(/[^0-9a-z]+/gi,'_');
if (CONFIG.tp.prefix) {
trainingName = CONFIG.tp.prefix + '_' + trainingName;
Expand All @@ -279,8 +338,6 @@ async function create_tp_reservation(reservation_id) {
newGroup = await createExtraGroup(trainingName, reservation.owner);
gpname = newGroup.name;
}


let newProject;
if (reservation.group_or_project == 'project') {
logger.info('Create Project', trainingName);
Expand All @@ -298,24 +355,33 @@ async function create_tp_reservation(reservation_id) {
newProject
);

for(let i=0;i<activated_users.length;i++) {
for(let i = 0; i < activated_users.length; i++) {
logger.debug('activated user ', activated_users[i].uid);
reservation.accounts.push(activated_users[i].uid);
}
try{
logger.info('Send Password', trainingName);
await send_user_passwords(reservation.owner, reservation.from, reservation.to, activated_users, gpname);
await dbsrv.mongo_reservations().updateOne({'_id': reservation_id}, {
'$set': {
'accounts': reservation.accounts,
'group': newGroup,
'project': newProject,
'created': true
}
});
await send_user_passwords(
reservation.owner,
reservation.from,
reservation.to,
activated_users,
gpname
);
await dbsrv.mongo_reservations().updateOne({'_id': reservation_id}, {'$set': {
'accounts': reservation.accounts,
'group': newGroup,
'project': newProject,
'created': true
}});

logger.debug('reservation ', reservation);
await dbsrv.mongo_events().insertOne({ 'owner': 'auto', 'date': new Date().getTime(), 'action': 'create reservation for ' + reservation.owner , 'logs': [] });
await dbsrv.mongo_events().insertOne( {
'owner': 'auto',
'date': new Date().getTime(),
'action': 'create reservation for ' + reservation.owner ,
'logs': []
});
return reservation;
}
catch(exception) {
Expand All @@ -338,7 +404,6 @@ async function tp_reservation(userId, from_date, to_date, quantity, about, group
'group_or_project': group_or_project,
'name': name
};

await dbsrv.mongo_reservations().insertOne(reservation);
logger.debug('reservation ', reservation);
return reservation;
Expand Down
30 changes: 29 additions & 1 deletion manager2/src/app/tps/tps.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,42 @@ <h3>Reservation</h3>
</tbody>
</table>
</div>
<button *ngIf="!selectedEvent.created && !selectedEvent.over && authorized" class="p-button p-button-sm p-button-secondary" (click)="cancel_reservation()">Cancel</button>
<button *ngIf="!selectedEvent.created && session_user.is_admin" class="p-button p-button-sm p-button-primary" (click)="create_reservation()">Create</button>
<button *ngIf="!selectedEvent.created && !selectedEvent.over && authorized" class="p-button p-button-sm p-button-secondary" (click)="cancel_reservation()">Cancel</button>
<button *ngIf="selectedEvent.created && !selectedEvent.over && session_user.is_admin" type="button" class="p-button p-button-sm p-button-primary" data-toggle="modal" data-target="#extend">Extend</button>
<button *ngIf="selectedEvent.created && !selectedEvent.over && session_user.is_admin" class="p-button p-button-sm p-button-warning" (click)="remove_reservation()">Remove</button>
</div>
</div>
</div>
</div>

<div class="modal" style="z-index: 1500;" id="extend" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h3>Extend TP {{ selectedEvent ? selectedEvent.name : '' }}</h3>
</div>
<form aria-label="TP extension request form" role="form" class="user-form form-horizonal">
<div class="modal-body">
<div class="form-group row col-sm-8">
<label for="new_expire">New Expiration Date&nbsp;
<span style="color:red">(required)&nbsp;</span>
<span class="glyphicon glyphicon-question-sign" title="Project expiration date" tooltip></span>
</label>
<input required id="new_expire" [ngModelOptions]="{standalone: true}" [(ngModel)]="new_expire" type="date" class="form-control"/>
</div>
</div>
<div class="modal-footer">
<div class="form-group row col-sm-12">
<button type="button" class="p-button p-button-sm p-button-primary" (click)="extend_reservation()" data-dismiss="modal">Extend</button>
<button type="button" class="p-button p-button-sm p-button-secondary" data-dismiss="modal">Cancel</button>
</div>
</div>
</form>
</div>
</div>
</div>

<div *ngIf="authorized" class="row">
<div class="col-md-8">
<div class="card bg-light">
Expand Down
Loading

0 comments on commit 08c3f63

Please sign in to comment.