From 7117c5a30d2f35ac81bc65ab2665a95c6a94c2ea Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 7 May 2024 17:20:48 +0200 Subject: [PATCH 01/39] create extend button --- manager2/src/app/tps/tps.component.html | 1 + manager2/src/app/tps/tps.component.ts | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/manager2/src/app/tps/tps.component.html b/manager2/src/app/tps/tps.component.html index 67524bec5..9bccbb808 100644 --- a/manager2/src/app/tps/tps.component.html +++ b/manager2/src/app/tps/tps.component.html @@ -95,6 +95,7 @@

Reservation

+ diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 2b91d5324..d5da9abb5 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -174,6 +174,17 @@ export class TpsComponent implements OnInit { this.listEvents(); } + extend_reservation() { + this.msg = ''; + this.errmsg = ''; + this.tpService.remove(this.selectedEvent.id).subscribe( + resp => this.msg = resp['message'], + err => this.errmsg = err.error.message + ) + this.selectedEvent.over = true; + this.listEvents(); + } + eventClicked(clickedEvent) { let event = clickedEvent.event; this.selectedEvent = event.meta; From a79867aa0e90b41f4b89cada7d8e70418d96b88f Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 7 May 2024 17:30:14 +0200 Subject: [PATCH 02/39] create extend service --- manager2/src/app/tps/tps.component.ts | 2 +- manager2/src/app/tps/tpservice.service.ts | 11 ++++ routes/tp.js | 63 +++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index d5da9abb5..e004165e4 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -177,7 +177,7 @@ export class TpsComponent implements OnInit { extend_reservation() { this.msg = ''; this.errmsg = ''; - this.tpService.remove(this.selectedEvent.id).subscribe( + this.tpService.extend(this.selectedEvent.id).subscribe( resp => this.msg = resp['message'], err => this.errmsg = err.error.message ) diff --git a/manager2/src/app/tps/tpservice.service.ts b/manager2/src/app/tps/tpservice.service.ts index 7f114a611..44029c733 100644 --- a/manager2/src/app/tps/tpservice.service.ts +++ b/manager2/src/app/tps/tpservice.service.ts @@ -66,4 +66,15 @@ export class TpserviceService { return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/stop', httpOptions) } + + extend(reservationId): Observable { + //let user = this.authService.profile; + let httpOptions = { + //headers: new HttpHeaders({ + // 'x-api-key': user.apikey + //}), + }; + + return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/extend', httpOptions) + } } diff --git a/routes/tp.js b/routes/tp.js index b89bb0c4c..84b5a50d8 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -339,4 +339,67 @@ router.put('/tp/:id/reserve/now', async function(req, res) { res.end(); }); +router.put('/tp/:id/reserve/extend', async function(req, res) { + if(! req.locals.logInfo.is_logged) { + res.status(403).send({message: 'Not authorized'}); + return; + } + if(! sansrv.sanitizeAll([req.params.id])) { + res.status(403).send({message: 'Invalid parameters'}); + return; + } + let user = null; + let isadmin = false; + try { + user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); + isadmin = await rolsrv.is_admin(user); + } catch(e) { + logger.error(e); + res.status(404).send({message: 'User session not found'}); + res.end(); + return; + } + + if(!user) { + res.send({message: 'User does not exist'}); + res.end(); + return; + } + + let is_admin = isadmin; + if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { + res.status(403).send({message: 'Not authorized'}); + return; + } + + let reservation_id = ObjectID.createFromHexString(req.params.id); + + // add filter + let filter = {}; + if(is_admin) { + filter = {_id: reservation_id}; + } + else{ + filter = {_id: reservation_id, owner: user.uid}; + } + let reservation = await dbsrv.mongo_reservations().findOne(filter); + if(!reservation){ + res.status(403).send({message: 'Not allowed to delete this reservation'}); + res.end(); + return; + } + + try { + tpssrv.remove_tp_reservation(reservation_id); + } catch (error) { + logger.error(error); + res.status(500).send({message: 'Error will removing tp reservation'}); + res.end(); + return; + } + + res.send({message: 'Reservation closed'}); + res.end(); +}); + module.exports = router; From f13e47839ee38b374949a8ca0497969c09ddf3a7 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 7 May 2024 18:10:25 +0200 Subject: [PATCH 03/39] create extend api --- core/tps.service.js | 21 +++++++++++++++++++++ routes/tp.js | 11 +++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index 8e521441d..cf05f474b 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -253,6 +253,27 @@ async function remove_tp_reservation(reservation_id) { } +async function extend_tp_reservation(reservation_id, new_expire) { + + logger.info('Extend reservation', reservation_id); + + let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); + logger.debug('Extend reservation', reservation); + + try { + await dbsrv.mongo_reservations().updateOne({'_id': reservation_id}, { + '$set': { + 'to': new_expire + } + }); + await dbsrv.mongo_events().insertOne({ 'owner': 'auto', 'date': new Date().getTime(), 'action': 'extend reservation for ' + reservation.owner , 'logs': [] }); + + } catch (error) { + logger.error(error); + } + +} + async function create_tp_reservation(reservation_id) { logger.info('Create reservation', reservation_id); diff --git a/routes/tp.js b/routes/tp.js index 84b5a50d8..39eb27f2f 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -264,7 +264,7 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { tpssrv.remove_tp_reservation(reservation_id); } catch (error) { logger.error(error); - res.status(500).send({message: 'Error will removing tp reservation'}); + res.status(500).send({message: 'Error while removing tp reservation'}); res.end(); return; } @@ -328,7 +328,7 @@ router.put('/tp/:id/reserve/now', async function(req, res) { newresa = await tpssrv.create_tp_reservation(reservation_id, 'auto'); } catch (error) { logger.error(error); - res.status(500).send({message: 'Error will creating tp reservation'}); + res.status(500).send({message: 'Error while creating tp reservation'}); res.end(); return; } @@ -374,7 +374,6 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { let reservation_id = ObjectID.createFromHexString(req.params.id); - // add filter let filter = {}; if(is_admin) { filter = {_id: reservation_id}; @@ -384,16 +383,16 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ - res.status(403).send({message: 'Not allowed to delete this reservation'}); + res.status(403).send({message: 'Not allowed to extend this reservation'}); res.end(); return; } try { - tpssrv.remove_tp_reservation(reservation_id); + tpssrv.extend_tp_reservation(reservation_id); } catch (error) { logger.error(error); - res.status(500).send({message: 'Error will removing tp reservation'}); + res.status(500).send({message: 'Error while extending tp reservation'}); res.end(); return; } From 7588ef6d9cf30f96b90a4dd01ccae0e0c3147726 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Mon, 13 May 2024 13:24:24 +0200 Subject: [PATCH 04/39] add param to extend back --- routes/tp.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/routes/tp.js b/routes/tp.js index 39eb27f2f..575d35891 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -361,13 +361,12 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } if(!user) { - res.send({message: 'User does not exist'}); + res.status(404).send({message: 'User does not exist'}); res.end(); return; } - let is_admin = isadmin; - if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { + if(! (isadmin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); return; } @@ -375,7 +374,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { let reservation_id = ObjectID.createFromHexString(req.params.id); let filter = {}; - if(is_admin) { + if(isadmin) { filter = {_id: reservation_id}; } else{ @@ -389,7 +388,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } try { - tpssrv.extend_tp_reservation(reservation_id); + tpssrv.extend_tp_reservation(reservation_id, req.body.to); } catch (error) { logger.error(error); res.status(500).send({message: 'Error while extending tp reservation'}); @@ -397,7 +396,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { return; } - res.send({message: 'Reservation closed'}); + res.status(200).send({message: 'Reservation closed'}); res.end(); }); From 4e1d546f9cebb3857a3859f572be5d64fcefdf43 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Mon, 13 May 2024 14:44:10 +0200 Subject: [PATCH 05/39] add param to extend front --- manager2/src/app/tps/tps.component.ts | 4 ++-- manager2/src/app/tps/tpservice.service.ts | 4 ++-- routes/tp.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index e004165e4..d0541d287 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -174,10 +174,10 @@ export class TpsComponent implements OnInit { this.listEvents(); } - extend_reservation() { + extend_reservation(new_expire) { this.msg = ''; this.errmsg = ''; - this.tpService.extend(this.selectedEvent.id).subscribe( + this.tpService.extend(this.selectedEvent.id, new_expire).subscribe( resp => this.msg = resp['message'], err => this.errmsg = err.error.message ) diff --git a/manager2/src/app/tps/tpservice.service.ts b/manager2/src/app/tps/tpservice.service.ts index 44029c733..07f477131 100644 --- a/manager2/src/app/tps/tpservice.service.ts +++ b/manager2/src/app/tps/tpservice.service.ts @@ -67,7 +67,7 @@ export class TpserviceService { return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/stop', httpOptions) } - extend(reservationId): Observable { + extend(reservationId, new_expire): Observable { //let user = this.authService.profile; let httpOptions = { //headers: new HttpHeaders({ @@ -75,6 +75,6 @@ export class TpserviceService { //}), }; - return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/extend', httpOptions) + return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/extend', new_expire, httpOptions) } } diff --git a/routes/tp.js b/routes/tp.js index 575d35891..15451595b 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -388,7 +388,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } try { - tpssrv.extend_tp_reservation(reservation_id, req.body.to); + tpssrv.extend_tp_reservation(reservation_id, req.body); } catch (error) { logger.error(error); res.status(500).send({message: 'Error while extending tp reservation'}); From 5064bd20e2aa6824783a81695097bb205afa7a4d Mon Sep 17 00:00:00 2001 From: remy siminel Date: Mon, 13 May 2024 15:38:49 +0200 Subject: [PATCH 06/39] add TP extension form --- manager2/src/app/tps/tps.component.html | 30 ++++++++++++++++++++++++- manager2/src/app/tps/tps.component.ts | 2 ++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/manager2/src/app/tps/tps.component.html b/manager2/src/app/tps/tps.component.html index 9bccbb808..fd0c1fd59 100644 --- a/manager2/src/app/tps/tps.component.html +++ b/manager2/src/app/tps/tps.component.html @@ -93,14 +93,42 @@

Reservation

+ - + + +
diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index d0541d287..b775949f9 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -39,6 +39,8 @@ export class TpsComponent implements OnInit { activeDayIsOpen: boolean = true; + new_expire: Date + constructor( private authService: AuthService, private configService: ConfigService, From 2ed3bde4afd3de3fe3c0f8d38d44b956fbc3c970 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Mon, 13 May 2024 16:55:48 +0200 Subject: [PATCH 07/39] wording --- manager2/src/app/tps/tps.component.html | 2 +- routes/tp.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/manager2/src/app/tps/tps.component.html b/manager2/src/app/tps/tps.component.html index fd0c1fd59..69d591fb2 100644 --- a/manager2/src/app/tps/tps.component.html +++ b/manager2/src/app/tps/tps.component.html @@ -93,7 +93,7 @@

Reservation

- + diff --git a/routes/tp.js b/routes/tp.js index 15451595b..6e199df95 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -396,7 +396,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { return; } - res.status(200).send({message: 'Reservation closed'}); + res.status(200).send({message: 'Reservation extended'}); res.end(); }); From 6b74a8167222f2dc629ff2fd1f4cd4e77a89532a Mon Sep 17 00:00:00 2001 From: remy siminel Date: Mon, 13 May 2024 17:50:45 +0200 Subject: [PATCH 08/39] fix --- manager2/src/app/tps/tps.component.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index b775949f9..68d15e76b 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -183,7 +183,6 @@ export class TpsComponent implements OnInit { resp => this.msg = resp['message'], err => this.errmsg = err.error.message ) - this.selectedEvent.over = true; this.listEvents(); } From 456f36e2348e37da0e3be60e033f5f6d6c285bd6 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 14 May 2024 11:56:35 +0200 Subject: [PATCH 09/39] add route to app.js --- app.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app.js b/app.js index eabee4092..2016afc38 100644 --- a/app.js +++ b/app.js @@ -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); From f62256107e94f581bae98aca6775e22944da56a7 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 15 May 2024 09:48:19 +0200 Subject: [PATCH 10/39] convert expiration date to unix --- core/tps.service.js | 3 ++- manager2/src/app/tps/tps.component.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index cf05f474b..7a1cdf3aa 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -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) { @@ -254,7 +255,7 @@ async function remove_tp_reservation(reservation_id) { } async function extend_tp_reservation(reservation_id, new_expire) { - + logger.info("The new expiration date is : " + new_expire); logger.info('Extend reservation', reservation_id); let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 68d15e76b..5daf2ae82 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -179,7 +179,7 @@ export class TpsComponent implements OnInit { extend_reservation(new_expire) { this.msg = ''; this.errmsg = ''; - this.tpService.extend(this.selectedEvent.id, new_expire).subscribe( + this.tpService.extend(this.selectedEvent.id, new Date(new_expire).getTime()).subscribe( resp => this.msg = resp['message'], err => this.errmsg = err.error.message ) From 31b79a86f5dfc4f564542cc01c2d5d5686628461 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 15 May 2024 10:28:56 +0200 Subject: [PATCH 11/39] generalize extending to editing --- app.js | 2 +- core/tps.service.js | 11 +++++------ manager2/src/app/tps/tps.component.ts | 3 ++- manager2/src/app/tps/tpservice.service.ts | 4 ++-- routes/tp.js | 10 +++++----- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index 2016afc38..d660d6bc2 100644 --- a/app.js +++ b/app.js @@ -370,7 +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.put('/tp/:id/reserve/edit', tp); app.get('/auth', auth); app.post('/auth/:id', auth); diff --git a/core/tps.service.js b/core/tps.service.js index 7a1cdf3aa..20fe6d82c 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -29,7 +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; +exports.edit_tp_reservation = edit_tp_reservation; async function createExtraGroup(trainingName, ownerName) { @@ -254,17 +254,16 @@ async function remove_tp_reservation(reservation_id) { } -async function extend_tp_reservation(reservation_id, new_expire) { - logger.info("The new expiration date is : " + new_expire); - logger.info('Extend reservation', reservation_id); +async function edit_tp_reservation(reservation_id, new_reservation) { + logger.info('Edit reservation', reservation_id); let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); - logger.debug('Extend reservation', reservation); + logger.debug('Edit reservation', reservation); try { await dbsrv.mongo_reservations().updateOne({'_id': reservation_id}, { '$set': { - 'to': new_expire + 'to': new_reservation.to } }); await dbsrv.mongo_events().insertOne({ 'owner': 'auto', 'date': new Date().getTime(), 'action': 'extend reservation for ' + reservation.owner , 'logs': [] }); diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 5daf2ae82..98d4db1b5 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -179,7 +179,8 @@ export class TpsComponent implements OnInit { extend_reservation(new_expire) { this.msg = ''; this.errmsg = ''; - this.tpService.extend(this.selectedEvent.id, new Date(new_expire).getTime()).subscribe( + const new_reservation = { ...this.selectedEvent, to: new Date(this.new_expire).getTime() } + this.tpService.edit(this.selectedEvent.id, new_reservation).subscribe( resp => this.msg = resp['message'], err => this.errmsg = err.error.message ) diff --git a/manager2/src/app/tps/tpservice.service.ts b/manager2/src/app/tps/tpservice.service.ts index 07f477131..d98ed384e 100644 --- a/manager2/src/app/tps/tpservice.service.ts +++ b/manager2/src/app/tps/tpservice.service.ts @@ -67,7 +67,7 @@ export class TpserviceService { return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/stop', httpOptions) } - extend(reservationId, new_expire): Observable { + edit(reservationId, new_reservation): Observable { //let user = this.authService.profile; let httpOptions = { //headers: new HttpHeaders({ @@ -75,6 +75,6 @@ export class TpserviceService { //}), }; - return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/extend', new_expire, httpOptions) + return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/edit', new_reservation, httpOptions) } } diff --git a/routes/tp.js b/routes/tp.js index 6e199df95..e093cdb7e 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -339,7 +339,7 @@ router.put('/tp/:id/reserve/now', async function(req, res) { res.end(); }); -router.put('/tp/:id/reserve/extend', async function(req, res) { +router.put('/tp/:id/reserve/edit', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); return; @@ -382,21 +382,21 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ - res.status(403).send({message: 'Not allowed to extend this reservation'}); + res.status(403).send({message: 'Not allowed to edit this reservation'}); res.end(); return; } try { - tpssrv.extend_tp_reservation(reservation_id, req.body); + tpssrv.edit_tp_reservation(reservation_id, req.body); } catch (error) { logger.error(error); - res.status(500).send({message: 'Error while extending tp reservation'}); + res.status(500).send({message: 'Error while editing tp reservation'}); res.end(); return; } - res.status(200).send({message: 'Reservation extended'}); + res.status(200).send({message: 'Reservation edited'}); res.end(); }); From 0b059ee8103e49562c815e332ec3f90a43f8f6d7 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 15 May 2024 11:21:56 +0200 Subject: [PATCH 12/39] lint --- core/tps.service.js | 9 ++++----- manager2/src/app/tps/tps.component.ts | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index 20fe6d82c..91849bf82 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -261,11 +261,10 @@ async function edit_tp_reservation(reservation_id, new_reservation) { logger.debug('Edit reservation', reservation); try { - await dbsrv.mongo_reservations().updateOne({'_id': reservation_id}, { - '$set': { - 'to': new_reservation.to - } - }); + await dbsrv.mongo_reservations().updateOne( + {'_id': reservation_id}, + {'$set': { 'to': new_reservation.to } } + ); await dbsrv.mongo_events().insertOne({ 'owner': 'auto', 'date': new Date().getTime(), 'action': 'extend reservation for ' + reservation.owner , 'logs': [] }); } catch (error) { diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 98d4db1b5..938e4fcdc 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -176,7 +176,7 @@ export class TpsComponent implements OnInit { this.listEvents(); } - extend_reservation(new_expire) { + extend_reservation(new_expire: Date) { this.msg = ''; this.errmsg = ''; const new_reservation = { ...this.selectedEvent, to: new Date(this.new_expire).getTime() } From 31cd19b8b029c6aaae17ac0316072ef965a8488d Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 15 May 2024 11:42:11 +0200 Subject: [PATCH 13/39] catch date advance error --- manager2/src/app/tps/tps.component.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 938e4fcdc..cd3adffa8 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -179,11 +179,14 @@ export class TpsComponent implements OnInit { extend_reservation(new_expire: Date) { this.msg = ''; this.errmsg = ''; - const new_reservation = { ...this.selectedEvent, to: new Date(this.new_expire).getTime() } + if (new_expire < this.selectedEvent.to) { + // Error: not meant to advance expiration date + } + const new_reservation = { ...this.selectedEvent, to: new Date(this.new_expire).getTime() }; this.tpService.edit(this.selectedEvent.id, new_reservation).subscribe( resp => this.msg = resp['message'], err => this.errmsg = err.error.message - ) + ); this.listEvents(); } From 8ed8e8db46eac9f524b90e161ccc3e49069f8bc5 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 15 May 2024 12:00:49 +0200 Subject: [PATCH 14/39] un-generalize --- app.js | 2 +- core/tps.service.js | 8 ++++---- manager2/src/app/tps/tps.component.ts | 2 +- manager2/src/app/tps/tpservice.service.ts | 4 ++-- routes/tp.js | 10 +++++----- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app.js b/app.js index d660d6bc2..2016afc38 100644 --- a/app.js +++ b/app.js @@ -370,7 +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/edit', tp); +app.put('/tp/:id/reserve/extend', tp); app.get('/auth', auth); app.post('/auth/:id', auth); diff --git a/core/tps.service.js b/core/tps.service.js index 91849bf82..0219e6d00 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -29,7 +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.edit_tp_reservation = edit_tp_reservation; +exports.extend_tp_reservation = extend_tp_reservation; async function createExtraGroup(trainingName, ownerName) { @@ -254,11 +254,11 @@ async function remove_tp_reservation(reservation_id) { } -async function edit_tp_reservation(reservation_id, new_reservation) { - logger.info('Edit reservation', reservation_id); +async function extend_tp_reservation(reservation_id, new_reservation) { + logger.info('Extend reservation', reservation_id); let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); - logger.debug('Edit reservation', reservation); + logger.debug('Extend reservation', reservation); try { await dbsrv.mongo_reservations().updateOne( diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index cd3adffa8..01da19e91 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -183,7 +183,7 @@ export class TpsComponent implements OnInit { // Error: not meant to advance expiration date } const new_reservation = { ...this.selectedEvent, to: new Date(this.new_expire).getTime() }; - this.tpService.edit(this.selectedEvent.id, new_reservation).subscribe( + this.tpService.extend(this.selectedEvent.id, new_reservation).subscribe( resp => this.msg = resp['message'], err => this.errmsg = err.error.message ); diff --git a/manager2/src/app/tps/tpservice.service.ts b/manager2/src/app/tps/tpservice.service.ts index d98ed384e..a12cd313b 100644 --- a/manager2/src/app/tps/tpservice.service.ts +++ b/manager2/src/app/tps/tpservice.service.ts @@ -67,7 +67,7 @@ export class TpserviceService { return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/stop', httpOptions) } - edit(reservationId, new_reservation): Observable { + extend(reservationId, new_reservation): Observable { //let user = this.authService.profile; let httpOptions = { //headers: new HttpHeaders({ @@ -75,6 +75,6 @@ export class TpserviceService { //}), }; - return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/edit', new_reservation, httpOptions) + return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/extend', new_reservation, httpOptions) } } diff --git a/routes/tp.js b/routes/tp.js index e093cdb7e..6e199df95 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -339,7 +339,7 @@ router.put('/tp/:id/reserve/now', async function(req, res) { res.end(); }); -router.put('/tp/:id/reserve/edit', async function(req, res) { +router.put('/tp/:id/reserve/extend', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); return; @@ -382,21 +382,21 @@ router.put('/tp/:id/reserve/edit', async function(req, res) { } let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ - res.status(403).send({message: 'Not allowed to edit this reservation'}); + res.status(403).send({message: 'Not allowed to extend this reservation'}); res.end(); return; } try { - tpssrv.edit_tp_reservation(reservation_id, req.body); + tpssrv.extend_tp_reservation(reservation_id, req.body); } catch (error) { logger.error(error); - res.status(500).send({message: 'Error while editing tp reservation'}); + res.status(500).send({message: 'Error while extending tp reservation'}); res.end(); return; } - res.status(200).send({message: 'Reservation edited'}); + res.status(200).send({message: 'Reservation extended'}); res.end(); }); From 8698345d2bce6b184e99a9cf1a017c5d5fa87508 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 15 May 2024 12:07:18 +0200 Subject: [PATCH 15/39] catch date advance error backend --- core/tps.service.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/tps.service.js b/core/tps.service.js index 0219e6d00..6ac3beed1 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -260,6 +260,10 @@ async function extend_tp_reservation(reservation_id, new_reservation) { let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); logger.debug('Extend reservation', reservation); + if (new_reservation.to < reservation.to) { + // Error: not meant to advance expiration date + } + try { await dbsrv.mongo_reservations().updateOne( {'_id': reservation_id}, From 856cf35754f469660622617728a3b8a8f03c5fbc Mon Sep 17 00:00:00 2001 From: remy siminel Date: Thu, 16 May 2024 10:18:54 +0200 Subject: [PATCH 16/39] deal with date advance error --- core/tps.service.js | 4 ---- manager2/src/app/tps/tps.component.ts | 5 +++-- routes/tp.js | 10 ++++++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index 6ac3beed1..0219e6d00 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -260,10 +260,6 @@ async function extend_tp_reservation(reservation_id, new_reservation) { let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); logger.debug('Extend reservation', reservation); - if (new_reservation.to < reservation.to) { - // Error: not meant to advance expiration date - } - try { await dbsrv.mongo_reservations().updateOne( {'_id': reservation_id}, diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 01da19e91..1c519b004 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -179,8 +179,9 @@ export class TpsComponent implements OnInit { extend_reservation(new_expire: Date) { this.msg = ''; this.errmsg = ''; - if (new_expire < this.selectedEvent.to) { - // Error: not meant to advance expiration date + if (new Date(this.new_expire).getTime() < this.selectedEvent.end) { // Why did we rename the 'to' field to 'end' for selected events (l.59) ? + this.errmsg = 'Extended end date must be after current end date'; + return; } const new_reservation = { ...this.selectedEvent, to: new Date(this.new_expire).getTime() }; this.tpService.extend(this.selectedEvent.id, new_reservation).subscribe( diff --git a/routes/tp.js b/routes/tp.js index 6e199df95..6316ba509 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -361,7 +361,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } if(!user) { - res.status(404).send({message: 'User does not exist'}); + res.status(404).send({message: 'User does not exist'}); // Why do none of the other routes give a 404 status here ? res.end(); return; } @@ -387,6 +387,12 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { return; } + if (req.body.to < reservation.to) { + res.status(403).send({message: 'Extended end date must be after current end date'}); + // Should I add "res.end();" here ? + return; + } + try { tpssrv.extend_tp_reservation(reservation_id, req.body); } catch (error) { @@ -396,7 +402,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { return; } - res.status(200).send({message: 'Reservation extended'}); + res.status(200).send({message: 'Reservation extended'}); // Why do none of the other routes give a 200 status on success ? res.end(); }); From f4255b7589d6289c19b02fab0bf390fcff32df66 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 21 May 2024 09:57:00 +0200 Subject: [PATCH 17/39] remove comments --- manager2/src/app/tps/tps.component.html | 1 - manager2/src/app/tps/tps.component.ts | 4 ++-- routes/tp.js | 18 +++++++++--------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/manager2/src/app/tps/tps.component.html b/manager2/src/app/tps/tps.component.html index 69d591fb2..30542c1b1 100644 --- a/manager2/src/app/tps/tps.component.html +++ b/manager2/src/app/tps/tps.component.html @@ -93,7 +93,6 @@

Reservation

- diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 1c519b004..443c31b6b 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -179,11 +179,11 @@ export class TpsComponent implements OnInit { extend_reservation(new_expire: Date) { this.msg = ''; this.errmsg = ''; - if (new Date(this.new_expire).getTime() < this.selectedEvent.end) { // Why did we rename the 'to' field to 'end' for selected events (l.59) ? + if (new Date(this.new_expire).getTime() < this.selectedEvent.end) { this.errmsg = 'Extended end date must be after current end date'; return; } - const new_reservation = { ...this.selectedEvent, to: new Date(this.new_expire).getTime() }; + const new_reservation = { ...this.selectedEvent, to: new Date(this.new_expire).getTime()}; this.tpService.extend(this.selectedEvent.id, new_reservation).subscribe( resp => this.msg = resp['message'], err => this.errmsg = err.error.message diff --git a/routes/tp.js b/routes/tp.js index 6316ba509..bc4377819 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -27,7 +27,7 @@ router.get('/tp', async function(req, res) { } let user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); if(!user) { - res.send({message: 'User does not exist'}); + res.status(404).send({message: 'User does not exist'}); res.end(); return; } @@ -64,7 +64,7 @@ router.post('/tp', async function(req, res) { } if(!user) { - res.send({message: 'User does not exist'}); + res.status(404).send({message: 'User does not exist'}); res.end(); return; } @@ -112,7 +112,7 @@ router.get('/tp/:id', async function(req, res) { } if(!user) { - res.send({message: 'User does not exist'}); + res.status(404).send({message: 'User does not exist'}); res.end(); return; } @@ -165,7 +165,7 @@ router.delete('/tp/:id', async function(req, res) { } if(!user) { - res.send({message: 'User does not exist'}); + res.status(404).send({message: 'User does not exist'}); res.end(); return; } @@ -232,7 +232,7 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { } if(!user) { - res.send({message: 'User does not exist'}); + res.status(404).send({message: 'User does not exist'}); res.end(); return; } @@ -295,7 +295,7 @@ router.put('/tp/:id/reserve/now', async function(req, res) { } if(!user) { - res.send({message: 'User does not exist'}); + res.status(404).send({message: 'User does not exist'}); res.end(); return; } @@ -361,7 +361,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } if(!user) { - res.status(404).send({message: 'User does not exist'}); // Why do none of the other routes give a 404 status here ? + res.status(404).send({message: 'User does not exist'}); res.end(); return; } @@ -389,7 +389,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { if (req.body.to < reservation.to) { res.status(403).send({message: 'Extended end date must be after current end date'}); - // Should I add "res.end();" here ? + res.end(); return; } @@ -402,7 +402,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { return; } - res.status(200).send({message: 'Reservation extended'}); // Why do none of the other routes give a 200 status on success ? + res.send({message: 'Reservation extended'}); res.end(); }); From 4f9c2ac60cf857e844bcfe361f9709dee337730e Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 21 May 2024 10:12:04 +0200 Subject: [PATCH 18/39] pass less info through route --- core/tps.service.js | 4 ++-- manager2/src/app/tps/tps.component.ts | 4 ++-- manager2/src/app/tps/tpservice.service.ts | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index 0219e6d00..eb24f36ec 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -254,7 +254,7 @@ async function remove_tp_reservation(reservation_id) { } -async function extend_tp_reservation(reservation_id, new_reservation) { +async function extend_tp_reservation(reservation_id, extension) { logger.info('Extend reservation', reservation_id); let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); @@ -263,7 +263,7 @@ async function extend_tp_reservation(reservation_id, new_reservation) { try { await dbsrv.mongo_reservations().updateOne( {'_id': reservation_id}, - {'$set': { 'to': new_reservation.to } } + {'$set': { 'to': extension.to } } ); await dbsrv.mongo_events().insertOne({ 'owner': 'auto', 'date': new Date().getTime(), 'action': 'extend reservation for ' + reservation.owner , 'logs': [] }); diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 443c31b6b..535635a17 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -183,8 +183,8 @@ export class TpsComponent implements OnInit { this.errmsg = 'Extended end date must be after current end date'; return; } - const new_reservation = { ...this.selectedEvent, to: new Date(this.new_expire).getTime()}; - this.tpService.extend(this.selectedEvent.id, new_reservation).subscribe( + const extension = { 'to': new Date(this.new_expire).getTime() }; + this.tpService.extend(this.selectedEvent.id, extension).subscribe( resp => this.msg = resp['message'], err => this.errmsg = err.error.message ); diff --git a/manager2/src/app/tps/tpservice.service.ts b/manager2/src/app/tps/tpservice.service.ts index a12cd313b..2925ff75f 100644 --- a/manager2/src/app/tps/tpservice.service.ts +++ b/manager2/src/app/tps/tpservice.service.ts @@ -67,7 +67,7 @@ export class TpserviceService { return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/stop', httpOptions) } - extend(reservationId, new_reservation): Observable { + extend(reservationId, extension): Observable { //let user = this.authService.profile; let httpOptions = { //headers: new HttpHeaders({ @@ -75,6 +75,6 @@ export class TpserviceService { //}), }; - return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/extend', new_reservation, httpOptions) + return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/extend', extension, httpOptions) } } From 96e96d8dfde171c71c92c47f31e0fa99a620d769 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 21 May 2024 11:30:44 +0200 Subject: [PATCH 19/39] extend users and project --- core/tps.service.js | 73 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index eb24f36ec..b3922eded 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -87,6 +87,26 @@ 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); @@ -193,7 +213,6 @@ 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{ await fdbs.delete_dbs(user); @@ -207,13 +226,36 @@ async function delete_tp_user(user) { } async function delete_tp_users(users) { - for(let i=0;i Date: Tue, 21 May 2024 11:46:57 +0200 Subject: [PATCH 20/39] prevent retroactive extension or creation --- manager2/src/app/tps/tps.component.ts | 12 ++++++++++-- routes/tp.js | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 535635a17..79316e8f7 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -121,8 +121,12 @@ export class TpsComponent implements OnInit { this.reserrmsg = 'Quantity must be > 0'; return; } - if (this.fromDate > this.toDate) { - this.reserrmsg = 'Final date must be superior to start date'; + if (this.fromDate.getTime() > this.toDate.getTime()) { + this.reserrmsg = 'End date must be superior to start date'; + return; + } + if (this.toDate.getTime() < new Date().getTime()) { + this.reserrmsg = 'End date can not be in the past'; return; } let reservation = { @@ -183,6 +187,10 @@ export class TpsComponent implements OnInit { this.errmsg = 'Extended end date must be after current end date'; return; } + if (new Date(this.new_expire).getTime() < new Date().getTime()) { + this.errmsg = 'Extended end date can not be in the past'; + return; + } const extension = { 'to': new Date(this.new_expire).getTime() }; this.tpService.extend(this.selectedEvent.id, extension).subscribe( resp => this.msg = resp['message'], diff --git a/routes/tp.js b/routes/tp.js index bc4377819..1940eb495 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -323,6 +323,12 @@ router.put('/tp/:id/reserve/now', async function(req, res) { return; } + if (reservation.to < new Date().getTime()) { + res.status(403).send({message: 'End date can not be in the past'}); + res.end(); + return; + } + let newresa; try { newresa = await tpssrv.create_tp_reservation(reservation_id, 'auto'); @@ -393,6 +399,12 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { return; } + if (req.body.to < new Date().getTime()) { + res.status(403).send({message: 'Extended end date can not be in the past'}); + res.end(); + return; + } + try { tpssrv.extend_tp_reservation(reservation_id, req.body); } catch (error) { From e52dfb761cdc3cbd77170d839bf758bb9c7d7b68 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 21 May 2024 14:00:52 +0200 Subject: [PATCH 21/39] get rid of useless 'res.end();'s --- routes/tp.js | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/routes/tp.js b/routes/tp.js index 1940eb495..11c1bdbb9 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -28,12 +28,10 @@ router.get('/tp', async function(req, res) { let user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } let reservations = await dbsrv.mongo_reservations().find({}).toArray(); res.send(reservations); - res.end(); }); router.post('/tp', async function(req, res) { @@ -59,13 +57,11 @@ router.post('/tp', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } @@ -84,7 +80,6 @@ router.post('/tp', async function(req, res) { req.body.name ).then(function(reservation){ res.send({reservation: reservation, message: 'Reservation done'}); - res.end(); return; }); }); @@ -107,13 +102,11 @@ router.get('/tp/:id', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } @@ -136,11 +129,9 @@ router.get('/tp/:id', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to get this reservation'}); - res.end(); return; } res.send({reservation: reservation}); - res.end(); }); router.delete('/tp/:id', async function(req, res) { @@ -160,13 +151,11 @@ router.delete('/tp/:id', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } @@ -189,24 +178,20 @@ router.delete('/tp/:id', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to delete this reservation'}); - res.end(); return; } if(reservation.over){ res.status(403).send({message: 'Reservation is already closed'}); - res.end(); return; } if(reservation.created){ res.status(403).send({message: 'Reservation accounts already created, reservation will be closed after closing date'}); - res.end(); return; } await dbsrv.mongo_reservations().updateOne({'_id': ObjectID.createFromHexString(req.params.id)},{'$set': {'over': true}}); res.send({message: 'Reservation cancelled'}); - res.end(); return; }); @@ -227,13 +212,11 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } @@ -256,7 +239,6 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to delete this reservation'}); - res.end(); return; } @@ -265,12 +247,10 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { } catch (error) { logger.error(error); res.status(500).send({message: 'Error while removing tp reservation'}); - res.end(); return; } res.send({message: 'Reservation closed'}); - res.end(); }); router.put('/tp/:id/reserve/now', async function(req, res) { @@ -290,13 +270,11 @@ router.put('/tp/:id/reserve/now', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } @@ -319,13 +297,11 @@ router.put('/tp/:id/reserve/now', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to reserve now this reservation'}); - res.end(); return; } if (reservation.to < new Date().getTime()) { res.status(403).send({message: 'End date can not be in the past'}); - res.end(); return; } @@ -335,14 +311,12 @@ router.put('/tp/:id/reserve/now', async function(req, res) { } catch (error) { logger.error(error); res.status(500).send({message: 'Error while creating tp reservation'}); - res.end(); return; } logger.debug('set reservation as done', newresa); newresa.created = true; res.send({reservation: newresa}); - res.end(); }); router.put('/tp/:id/reserve/extend', async function(req, res) { @@ -362,13 +336,11 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } @@ -389,19 +361,16 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to extend this reservation'}); - res.end(); return; } if (req.body.to < reservation.to) { res.status(403).send({message: 'Extended end date must be after current end date'}); - res.end(); return; } if (req.body.to < new Date().getTime()) { res.status(403).send({message: 'Extended end date can not be in the past'}); - res.end(); return; } @@ -410,12 +379,10 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } catch (error) { logger.error(error); res.status(500).send({message: 'Error while extending tp reservation'}); - res.end(); return; } res.send({message: 'Reservation extended'}); - res.end(); }); module.exports = router; From cff5ee8febe4ccb76908d23f63cf952b4ace1a79 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 22 May 2024 16:58:27 +0200 Subject: [PATCH 22/39] fix tp user extension --- core/tps.service.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index b3922eded..b89b8165a 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -97,7 +97,7 @@ async function extendExtraProject(project, extension) { return false; } try { - const extended_project = { ...project_to_extend, expire: extension.to } + 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) { @@ -235,25 +235,24 @@ async function delete_tp_users(users) { } -async function extend_tp_user(user) { +async function extend_tp_user(user, extension) { logger.debug('extend_tp_user', user.uid); try{ - user.history.push({'action': 'reactivate', date: new Date().getTime()}); - await dbsrv.mongo_users().updateOne({uid: user.uid}, {'$set': { - status: STATUS_ACTIVE, - expiration: (new Date().getTime() + day_time*duration_list[user.duration]), - }}); + user.history.push({ 'action': 'reactivate', date: new Date().getTime() }); + await dbsrv.mongo_users().updateOne({ uid: user.uid }, { + '$set': { expiration: extension.to } + }); } catch(exception) { logger.error(exception); } } -async function extend_tp_users(users) { +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]}); + let user = await dbsrv.mongo_users().findOne({ 'uid': users[i] }); if (user && user.uid) { - await extend_tp_user(user); + await extend_tp_user(user, extension); } } } @@ -299,11 +298,11 @@ async function extend_tp_reservation(reservation_id, extension) { logger.debug('Extend reservation', reservation); // add grace period to extension - extension = { ...extension, to: extension.to + 1000*3600*24*(CONFIG.tp.extra_expiration) } + 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); + await extend_tp_users(reservation.accounts, extension); } if (reservation.project && reservation.project.id && reservation.project.id != '') { From 447680529808289923c4c395471ad4966764b2d1 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 22 May 2024 18:38:01 +0200 Subject: [PATCH 23/39] fixes --- core/tps.service.js | 24 +++++++++++------------- manager2/src/app/tps/tps.component.ts | 4 ++-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index b89b8165a..7ccda83a9 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -165,9 +165,7 @@ 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) { @@ -297,6 +295,16 @@ async function extend_tp_reservation(reservation_id, extension) { let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); 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) }; @@ -309,16 +317,6 @@ async function extend_tp_reservation(reservation_id, extension) { logger.info('Extend Project', reservation.project.id); await extendExtraProject(reservation.project.id, extension); } - - 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); - } } async function create_tp_reservation(reservation_id) { @@ -366,7 +364,7 @@ async function create_tp_reservation(reservation_id) { newProject ); - for(let i=0;i this.toDate.getTime()) { + if (new Date(this.fromDate).getTime() > new Date(this.toDate).getTime()) { this.reserrmsg = 'End date must be superior to start date'; return; } - if (this.toDate.getTime() < new Date().getTime()) { + if (new Date(this.toDate).getTime() < new Date().getTime()) { this.reserrmsg = 'End date can not be in the past'; return; } From ff5ae262b3d864fb27cdd319957dca2ff02605e6 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 22 May 2024 19:00:27 +0200 Subject: [PATCH 24/39] fix01 --- core/tps.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/tps.service.js b/core/tps.service.js index 7ccda83a9..7941fb09a 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -236,7 +236,7 @@ async function delete_tp_users(users) { async function extend_tp_user(user, extension) { logger.debug('extend_tp_user', user.uid); try{ - user.history.push({ 'action': 'reactivate', date: new Date().getTime() }); + user.history.push({ 'action': 'extend', date: new Date().getTime() }); await dbsrv.mongo_users().updateOne({ uid: user.uid }, { '$set': { expiration: extension.to } }); From 08c31df909cb2506e09d66b4b57f3f3546808c3f Mon Sep 17 00:00:00 2001 From: remy siminel Date: Thu, 23 May 2024 17:26:33 +0200 Subject: [PATCH 25/39] update changelog --- CHANGELOG.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b5d6a87d..d38f670e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -43,4 +45,4 @@ * [docker] change order of commands * on renew by admin or user, reset expiration_notif * add listmonk mailer support -* upgrade jsonwebtoken to v9 to fix CVE https://cve.report/CVE-2022-23540 +* upgrade jsonwebtoken to v9 to fix CVE From 587c738beabe71b5386889c312a09d457fee8f6d Mon Sep 17 00:00:00 2001 From: remy siminel Date: Thu, 23 May 2024 17:28:59 +0200 Subject: [PATCH 26/39] only allow admin to extend tp --- routes/tp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/tp.js b/routes/tp.js index 11c1bdbb9..dfa88b029 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -344,7 +344,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { return; } - if(! (isadmin || (user.is_trainer !== undefined && user.is_trainer))) { + if(!isadmin) { res.status(403).send({message: 'Not authorized'}); return; } From a84131d06bc68801791cc2427d06111299ec39b5 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Fri, 24 May 2024 11:16:42 +0200 Subject: [PATCH 27/39] fix --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d38f670e7..00262e1a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,4 +45,4 @@ * [docker] change order of commands * on renew by admin or user, reset expiration_notif * add listmonk mailer support -* upgrade jsonwebtoken to v9 to fix CVE +* upgrade jsonwebtoken to v9 to fix CVE https://cve.report/CVE-2022-23540 From 8fa9e8e163d429d704d467516beb3fa563c920d4 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Mon, 27 May 2024 14:58:02 +0200 Subject: [PATCH 28/39] fix dumbassery --- manager2/src/app/tps/tps.component.html | 2 +- manager2/src/app/tps/tps.component.ts | 2 +- routes/tp.js | 17 ++++++----------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/manager2/src/app/tps/tps.component.html b/manager2/src/app/tps/tps.component.html index 30542c1b1..e1414224f 100644 --- a/manager2/src/app/tps/tps.component.html +++ b/manager2/src/app/tps/tps.component.html @@ -119,7 +119,7 @@

Extend TP {{ selectedEvent ? selectedEvent.name : '' }}

diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index c4ddd05e8..2df2ad8f8 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -180,7 +180,7 @@ export class TpsComponent implements OnInit { this.listEvents(); } - extend_reservation(new_expire: Date) { + extend_reservation() { this.msg = ''; this.errmsg = ''; if (new Date(this.new_expire).getTime() < this.selectedEvent.end) { diff --git a/routes/tp.js b/routes/tp.js index dfa88b029..96c717592 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -299,7 +299,6 @@ router.put('/tp/:id/reserve/now', async function(req, res) { res.status(403).send({message: 'Not allowed to reserve now this reservation'}); return; } - if (reservation.to < new Date().getTime()) { res.status(403).send({message: 'End date can not be in the past'}); return; @@ -324,6 +323,10 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { res.status(403).send({message: 'Not authorized'}); return; } + if(! req.body.to) { + res.status(403).send({message: 'No input'}); + return; + } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); return; @@ -351,24 +354,16 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { let reservation_id = ObjectID.createFromHexString(req.params.id); - let filter = {}; - if(isadmin) { - filter = {_id: reservation_id}; - } - else{ - filter = {_id: reservation_id, owner: user.uid}; - } + let filter = {_id: reservation_id}; let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ - res.status(403).send({message: 'Not allowed to extend this reservation'}); + res.status(404).send({message: 'Reservation does not exist'}); return; } - if (req.body.to < reservation.to) { res.status(403).send({message: 'Extended end date must be after current end date'}); return; } - if (req.body.to < new Date().getTime()) { res.status(403).send({message: 'Extended end date can not be in the past'}); return; From a65f1aa0b86b765ff25240e205540f9e15616e2a Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 28 May 2024 09:20:59 +0200 Subject: [PATCH 29/39] push user history to db --- core/tps.service.js | 7 ++++--- routes/tp.js | 3 +-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index 7941fb09a..a773feee6 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -237,9 +237,10 @@ async function extend_tp_user(user, extension) { logger.debug('extend_tp_user', user.uid); try{ user.history.push({ 'action': 'extend', date: new Date().getTime() }); - await dbsrv.mongo_users().updateOne({ uid: user.uid }, { - '$set': { expiration: extension.to } - }); + await dbsrv.mongo_users().updateOne({ uid: user.uid }, { '$set': { + expiration: extension.to, + history: user.history + } }); } catch(exception) { logger.error(exception); diff --git a/routes/tp.js b/routes/tp.js index 96c717592..8bd02d7ae 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -354,8 +354,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { let reservation_id = ObjectID.createFromHexString(req.params.id); - let filter = {_id: reservation_id}; - let reservation = await dbsrv.mongo_reservations().findOne(filter); + let reservation = await dbsrv.mongo_reservations().findOne({_id: reservation_id}); if(!reservation){ res.status(404).send({message: 'Reservation does not exist'}); return; From 1b74e45353a16228ee99f0bfff409c6201bf6194 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 28 May 2024 11:13:02 +0200 Subject: [PATCH 30/39] reduce db queries --- core/tps.service.js | 37 ++++++++++++++++--------------------- routes/tp.js | 7 +++---- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index a773feee6..970ba969c 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -212,7 +212,7 @@ 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); @@ -236,11 +236,11 @@ async function delete_tp_users(users) { async function extend_tp_user(user, extension) { logger.debug('extend_tp_user', user.uid); try{ - user.history.push({ 'action': 'extend', date: new Date().getTime() }); - await dbsrv.mongo_users().updateOne({ uid: user.uid }, { '$set': { + 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); @@ -249,7 +249,7 @@ async function extend_tp_user(user, extension) { 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] }); + let user = await dbsrv.mongo_users().findOne({'uid': users[i]}); if (user && user.uid) { await extend_tp_user(user, extension); } @@ -257,11 +257,8 @@ async function extend_tp_users(users, extension) { } -async function remove_tp_reservation(reservation_id) { - - logger.info('Close reservation', reservation_id); - - let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); +async function remove_tp_reservation(reservation) { + logger.info('Close reservation', reservation._id); logger.debug('Close reservation', reservation); if (reservation.accounts) { @@ -280,34 +277,32 @@ async function remove_tp_reservation(reservation_id) { } try { - await dbsrv.mongo_reservations().updateOne({'_id': reservation_id}, { + 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_id, extension) { - logger.info('Extend reservation', reservation_id); - let reservation = await dbsrv.mongo_reservations().findOne({'_id': reservation_id}); +async function extend_tp_reservation(reservation, extension) { + logger.info('Extend reservation', reservation._id); 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': [] }); + 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) }; + extension = {...extension, to: extension.to + 1000*3600*24*(CONFIG.tp.extra_expiration)}; if (reservation.accounts) { logger.info('Extend Accounts', reservation.accounts); diff --git a/routes/tp.js b/routes/tp.js index 8bd02d7ae..0a25e0cc4 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -190,7 +190,7 @@ router.delete('/tp/:id', async function(req, res) { res.status(403).send({message: 'Reservation accounts already created, reservation will be closed after closing date'}); return; } - await dbsrv.mongo_reservations().updateOne({'_id': ObjectID.createFromHexString(req.params.id)},{'$set': {'over': true}}); + await dbsrv.mongo_reservations().updateOne({'_id': ObjectID.createFromHexString(req.params.id)}, {'$set': {'over': true}}); res.send({message: 'Reservation cancelled'}); return; }); @@ -243,7 +243,7 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { } try { - tpssrv.remove_tp_reservation(reservation_id); + tpssrv.remove_tp_reservation(reservation); } catch (error) { logger.error(error); res.status(500).send({message: 'Error while removing tp reservation'}); @@ -353,7 +353,6 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } let reservation_id = ObjectID.createFromHexString(req.params.id); - let reservation = await dbsrv.mongo_reservations().findOne({_id: reservation_id}); if(!reservation){ res.status(404).send({message: 'Reservation does not exist'}); @@ -369,7 +368,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } try { - tpssrv.extend_tp_reservation(reservation_id, req.body); + tpssrv.extend_tp_reservation(reservation, req.body); } catch (error) { logger.error(error); res.status(500).send({message: 'Error while extending tp reservation'}); From ffc5b4e0ddcbd8b32ce8df30489016a4c9c6af45 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 28 May 2024 11:48:38 +0200 Subject: [PATCH 31/39] lint --- core/tps.service.js | 73 ++++++++++++----------- manager2/src/app/tps/tps.component.ts | 1 - manager2/src/app/tps/tpservice.service.ts | 6 -- routes/tp.js | 72 ++++++++-------------- 4 files changed, 65 insertions(+), 87 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index 970ba969c..aa0838cd9 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -111,14 +111,12 @@ async function create_tp_users_db (owner, quantity, duration, end_date, userGrou // 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; } @@ -134,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, @@ -167,10 +164,10 @@ async function create_tp_users_db (owner, quantity, duration, end_date, userGrou } await plgsrv.run_plugins('activate', user.uid, user, 'auto'); } - } - catch (error) { + } catch (error) { logger.error('Error', error); } + return users; } @@ -217,8 +214,7 @@ async function delete_tp_user(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); } } @@ -236,7 +232,10 @@ async function delete_tp_users(users) { 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()}); + 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 @@ -265,24 +264,25 @@ async function remove_tp_reservation(reservation) { 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); } @@ -296,19 +296,21 @@ async function extend_tp_reservation(reservation, extension) { 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': []}); + 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); @@ -316,7 +318,6 @@ async function extend_tp_reservation(reservation, extension) { } async function create_tp_reservation(reservation_id) { - logger.info('Create reservation', reservation_id); // Create users for reservation @@ -324,11 +325,9 @@ async function create_tp_reservation(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; @@ -341,8 +340,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); @@ -366,18 +363,27 @@ async function create_tp_reservation(reservation_id) { } 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) { @@ -400,7 +406,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; diff --git a/manager2/src/app/tps/tps.component.ts b/manager2/src/app/tps/tps.component.ts index 2df2ad8f8..da217f85c 100644 --- a/manager2/src/app/tps/tps.component.ts +++ b/manager2/src/app/tps/tps.component.ts @@ -84,7 +84,6 @@ export class TpsComponent implements OnInit { resp => { this.config = resp; this.group_or_project = this.config.reservation.group_or_project; - }, err => console.log('failed to get config') ); diff --git a/manager2/src/app/tps/tpservice.service.ts b/manager2/src/app/tps/tpservice.service.ts index 2925ff75f..bc14ef7bc 100644 --- a/manager2/src/app/tps/tpservice.service.ts +++ b/manager2/src/app/tps/tpservice.service.ts @@ -18,7 +18,6 @@ export class TpserviceService { // 'x-api-key': user.apikey //}), }; - return this.http.get(environment.apiUrl + '/tp', httpOptions) } @@ -29,7 +28,6 @@ export class TpserviceService { // 'x-api-key': user.apikey //}), }; - return this.http.post(environment.apiUrl + '/tp', reservation, httpOptions) } @@ -40,7 +38,6 @@ export class TpserviceService { // 'x-api-key': user.apikey //}), }; - return this.http.delete(environment.apiUrl + '/tp/' + reservationId, httpOptions) } @@ -52,7 +49,6 @@ export class TpserviceService { // 'x-api-key': user.apikey //}), }; - return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/now', httpOptions) } @@ -63,7 +59,6 @@ export class TpserviceService { // 'x-api-key': user.apikey //}), }; - return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/stop', httpOptions) } @@ -74,7 +69,6 @@ export class TpserviceService { // 'x-api-key': user.apikey //}), }; - return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/extend', extension, httpOptions) } } diff --git a/routes/tp.js b/routes/tp.js index 0a25e0cc4..7794fe447 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -35,41 +35,38 @@ router.get('/tp', async function(req, res) { }); router.post('/tp', async function(req, res) { - if(req.body.quantity === undefined || req.body.quantity === null || req.body.quantity<=0){ + if(req.body.quantity === undefined || req.body.quantity === null || req.body.quantity<=0) { res.status(403).send({message: 'Quantity must be >= 1'}); return; } - if(req.body.about === undefined || req.body.about == ''){ + if(req.body.about === undefined || req.body.about == '') { res.status(403).send({message: 'Tell us why you need some tp accounts'}); return; } - if(! req.locals.logInfo.is_logged) { res.status(401).send({message: 'Not authorized'}); return; } let user = null; - let isadmin = false; + let is_admin = false; try { user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); - isadmin = await rolsrv.is_admin(user); + is_admin = await rolsrv.is_admin(user); } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); return; } - if(!user) { res.status(404).send({message: 'User does not exist'}); return; } - - let is_admin = isadmin; if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); return; } + tpssrv.tp_reservation( user.uid, req.body.from, @@ -95,39 +92,34 @@ router.get('/tp/:id', async function(req, res) { } let user = null; - let isadmin = false; + let is_admin = false; try { user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); - isadmin = await rolsrv.is_admin(user); + is_admin = await rolsrv.is_admin(user); } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); return; } - if(!user) { res.status(404).send({message: 'User does not exist'}); return; } - - let is_admin = isadmin; if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); return; } let reservation_id = ObjectID.createFromHexString(req.params.id); - - // add filter let filter = {}; if(is_admin) { filter = {_id: reservation_id}; } - else{ + else { filter = {_id: reservation_id, owner: user.uid}; } let reservation = await dbsrv.mongo_reservations().findOne(filter); - if(!reservation){ + if(!reservation) { res.status(403).send({message: 'Not allowed to get this reservation'}); return; } @@ -143,31 +135,27 @@ router.delete('/tp/:id', async function(req, res) { res.status(403).send({message: 'Invalid parameters'}); return; } + let user = null; - let isadmin = false; + let is_admin = false; try { user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); - isadmin = await rolsrv.is_admin(user); + is_admin = await rolsrv.is_admin(user); } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); return; } - if(!user) { res.status(404).send({message: 'User does not exist'}); return; } - - let is_admin = isadmin; if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); return; } let reservation_id = ObjectID.createFromHexString(req.params.id); - - // add filter let filter = {}; if(is_admin) { filter = {_id: reservation_id}; @@ -180,17 +168,18 @@ router.delete('/tp/:id', async function(req, res) { res.status(403).send({message: 'Not allowed to delete this reservation'}); return; } - if(reservation.over){ res.status(403).send({message: 'Reservation is already closed'}); return; } - if(reservation.created){ res.status(403).send({message: 'Reservation accounts already created, reservation will be closed after closing date'}); return; } - await dbsrv.mongo_reservations().updateOne({'_id': ObjectID.createFromHexString(req.params.id)}, {'$set': {'over': true}}); + + await dbsrv.mongo_reservations().updateOne({'_id': ObjectID.createFromHexString(req.params.id)}, { + '$set': {'over': true} + }); res.send({message: 'Reservation cancelled'}); return; }); @@ -204,31 +193,27 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { res.status(403).send({message: 'Invalid parameters'}); return; } + let user = null; - let isadmin = false; + let is_admin = false; try { user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); - isadmin = await rolsrv.is_admin(user); + is_admin = await rolsrv.is_admin(user); } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); return; } - if(!user) { res.status(404).send({message: 'User does not exist'}); return; } - - let is_admin = isadmin; if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); return; } let reservation_id = ObjectID.createFromHexString(req.params.id); - - // add filter let filter = {}; if(is_admin) { filter = {_id: reservation_id}; @@ -249,7 +234,6 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { res.status(500).send({message: 'Error while removing tp reservation'}); return; } - res.send({message: 'Reservation closed'}); }); @@ -262,31 +246,27 @@ router.put('/tp/:id/reserve/now', async function(req, res) { res.status(403).send({message: 'Invalid parameters'}); return; } + let user = null; - let isadmin = false; + let is_admin = false; try { user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); - isadmin = await rolsrv.is_admin(user); + is_admin = await rolsrv.is_admin(user); } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); return; } - if(!user) { res.status(404).send({message: 'User does not exist'}); return; } - - let is_admin = isadmin; if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); return; } let reservation_id = ObjectID.createFromHexString(req.params.id); - - // add filter let filter = {}; if(is_admin) { filter = {_id: reservation_id}; @@ -312,7 +292,6 @@ router.put('/tp/:id/reserve/now', async function(req, res) { res.status(500).send({message: 'Error while creating tp reservation'}); return; } - logger.debug('set reservation as done', newresa); newresa.created = true; res.send({reservation: newresa}); @@ -331,11 +310,12 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { res.status(403).send({message: 'Invalid parameters'}); return; } + let user = null; - let isadmin = false; + let is_admin = false; try { user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); - isadmin = await rolsrv.is_admin(user); + is_admin = await rolsrv.is_admin(user); } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); @@ -347,7 +327,7 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { return; } - if(!isadmin) { + if(!is_admin) { res.status(403).send({message: 'Not authorized'}); return; } From e8ab7c0b3ce7d592f1fa7efcf62faaf59fbbf98a Mon Sep 17 00:00:00 2001 From: remy siminel Date: Tue, 28 May 2024 18:24:47 +0200 Subject: [PATCH 32/39] fix --- routes/tp.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/routes/tp.js b/routes/tp.js index 7794fe447..9196ea3dd 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -23,28 +23,34 @@ var STATUS_EXPIRED = 'Expired'; router.get('/tp', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(401).send({message: 'Not authorized'}); + res.end(); return; } let user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); if(!user) { res.status(404).send({message: 'User does not exist'}); + res.end(); return; } let reservations = await dbsrv.mongo_reservations().find({}).toArray(); res.send(reservations); + res.end(); }); router.post('/tp', async function(req, res) { if(req.body.quantity === undefined || req.body.quantity === null || req.body.quantity<=0) { res.status(403).send({message: 'Quantity must be >= 1'}); + res.end(); return; } if(req.body.about === undefined || req.body.about == '') { res.status(403).send({message: 'Tell us why you need some tp accounts'}); + res.end(); return; } if(! req.locals.logInfo.is_logged) { res.status(401).send({message: 'Not authorized'}); + res.end(); return; } @@ -56,14 +62,17 @@ router.post('/tp', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); + res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); + res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } @@ -77,6 +86,7 @@ router.post('/tp', async function(req, res) { req.body.name ).then(function(reservation){ res.send({reservation: reservation, message: 'Reservation done'}); + res.end(); return; }); }); @@ -84,10 +94,12 @@ router.post('/tp', async function(req, res) { router.get('/tp/:id', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); + res.end(); return; } @@ -99,14 +111,17 @@ router.get('/tp/:id', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); + res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); + res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } @@ -121,18 +136,22 @@ router.get('/tp/:id', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation) { res.status(403).send({message: 'Not allowed to get this reservation'}); + res.end(); return; } res.send({reservation: reservation}); + res.end(); }); router.delete('/tp/:id', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); + res.end(); return; } @@ -144,14 +163,17 @@ router.delete('/tp/:id', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); + res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); + res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } @@ -166,14 +188,17 @@ router.delete('/tp/:id', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to delete this reservation'}); + res.end(); return; } if(reservation.over){ res.status(403).send({message: 'Reservation is already closed'}); + res.end(); return; } if(reservation.created){ res.status(403).send({message: 'Reservation accounts already created, reservation will be closed after closing date'}); + res.end(); return; } @@ -181,16 +206,19 @@ router.delete('/tp/:id', async function(req, res) { '$set': {'over': true} }); res.send({message: 'Reservation cancelled'}); + res.end(); return; }); router.put('/tp/:id/reserve/stop', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); + res.end(); return; } @@ -202,14 +230,17 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); + res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); + res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } @@ -224,6 +255,7 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to delete this reservation'}); + res.end(); return; } @@ -232,18 +264,22 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { } catch (error) { logger.error(error); res.status(500).send({message: 'Error while removing tp reservation'}); + res.end(); return; } res.send({message: 'Reservation closed'}); + res.end(); }); router.put('/tp/:id/reserve/now', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); + res.end(); return; } @@ -255,14 +291,17 @@ router.put('/tp/:id/reserve/now', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); + res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); + res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } @@ -277,10 +316,12 @@ router.put('/tp/:id/reserve/now', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to reserve now this reservation'}); + res.end(); return; } if (reservation.to < new Date().getTime()) { res.status(403).send({message: 'End date can not be in the past'}); + res.end(); return; } @@ -290,24 +331,29 @@ router.put('/tp/:id/reserve/now', async function(req, res) { } catch (error) { logger.error(error); res.status(500).send({message: 'Error while creating tp reservation'}); + res.end(); return; } logger.debug('set reservation as done', newresa); newresa.created = true; res.send({reservation: newresa}); + res.end(); }); router.put('/tp/:id/reserve/extend', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } if(! req.body.to) { res.status(403).send({message: 'No input'}); + res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); + res.end(); return; } @@ -319,16 +365,19 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); + res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); + res.end(); return; } if(!is_admin) { res.status(403).send({message: 'Not authorized'}); + res.end(); return; } @@ -336,14 +385,17 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne({_id: reservation_id}); if(!reservation){ res.status(404).send({message: 'Reservation does not exist'}); + res.end(); return; } if (req.body.to < reservation.to) { res.status(403).send({message: 'Extended end date must be after current end date'}); + res.end(); return; } if (req.body.to < new Date().getTime()) { res.status(403).send({message: 'Extended end date can not be in the past'}); + res.end(); return; } @@ -352,10 +404,12 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } catch (error) { logger.error(error); res.status(500).send({message: 'Error while extending tp reservation'}); + res.end(); return; } res.send({message: 'Reservation extended'}); + res.end(); }); module.exports = router; From 76f1bb91616cb45f191f0c3f62e982bb0e1c8942 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 29 May 2024 13:31:02 +0200 Subject: [PATCH 33/39] un-fix --- routes/tp.js | 54 ---------------------------------------------------- 1 file changed, 54 deletions(-) diff --git a/routes/tp.js b/routes/tp.js index 9196ea3dd..7794fe447 100644 --- a/routes/tp.js +++ b/routes/tp.js @@ -23,34 +23,28 @@ var STATUS_EXPIRED = 'Expired'; router.get('/tp', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(401).send({message: 'Not authorized'}); - res.end(); return; } let user = await dbsrv.mongo_users().findOne({'_id': req.locals.logInfo.id}); if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } let reservations = await dbsrv.mongo_reservations().find({}).toArray(); res.send(reservations); - res.end(); }); router.post('/tp', async function(req, res) { if(req.body.quantity === undefined || req.body.quantity === null || req.body.quantity<=0) { res.status(403).send({message: 'Quantity must be >= 1'}); - res.end(); return; } if(req.body.about === undefined || req.body.about == '') { res.status(403).send({message: 'Tell us why you need some tp accounts'}); - res.end(); return; } if(! req.locals.logInfo.is_logged) { res.status(401).send({message: 'Not authorized'}); - res.end(); return; } @@ -62,17 +56,14 @@ router.post('/tp', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } @@ -86,7 +77,6 @@ router.post('/tp', async function(req, res) { req.body.name ).then(function(reservation){ res.send({reservation: reservation, message: 'Reservation done'}); - res.end(); return; }); }); @@ -94,12 +84,10 @@ router.post('/tp', async function(req, res) { router.get('/tp/:id', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); - res.end(); return; } @@ -111,17 +99,14 @@ router.get('/tp/:id', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } @@ -136,22 +121,18 @@ router.get('/tp/:id', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation) { res.status(403).send({message: 'Not allowed to get this reservation'}); - res.end(); return; } res.send({reservation: reservation}); - res.end(); }); router.delete('/tp/:id', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); - res.end(); return; } @@ -163,17 +144,14 @@ router.delete('/tp/:id', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } @@ -188,17 +166,14 @@ router.delete('/tp/:id', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to delete this reservation'}); - res.end(); return; } if(reservation.over){ res.status(403).send({message: 'Reservation is already closed'}); - res.end(); return; } if(reservation.created){ res.status(403).send({message: 'Reservation accounts already created, reservation will be closed after closing date'}); - res.end(); return; } @@ -206,19 +181,16 @@ router.delete('/tp/:id', async function(req, res) { '$set': {'over': true} }); res.send({message: 'Reservation cancelled'}); - res.end(); return; }); router.put('/tp/:id/reserve/stop', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); - res.end(); return; } @@ -230,17 +202,14 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } @@ -255,7 +224,6 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to delete this reservation'}); - res.end(); return; } @@ -264,22 +232,18 @@ router.put('/tp/:id/reserve/stop', async function(req, res) { } catch (error) { logger.error(error); res.status(500).send({message: 'Error while removing tp reservation'}); - res.end(); return; } res.send({message: 'Reservation closed'}); - res.end(); }); router.put('/tp/:id/reserve/now', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); - res.end(); return; } @@ -291,17 +255,14 @@ router.put('/tp/:id/reserve/now', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } if(! (is_admin || (user.is_trainer !== undefined && user.is_trainer))) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } @@ -316,12 +277,10 @@ router.put('/tp/:id/reserve/now', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne(filter); if(!reservation){ res.status(403).send({message: 'Not allowed to reserve now this reservation'}); - res.end(); return; } if (reservation.to < new Date().getTime()) { res.status(403).send({message: 'End date can not be in the past'}); - res.end(); return; } @@ -331,29 +290,24 @@ router.put('/tp/:id/reserve/now', async function(req, res) { } catch (error) { logger.error(error); res.status(500).send({message: 'Error while creating tp reservation'}); - res.end(); return; } logger.debug('set reservation as done', newresa); newresa.created = true; res.send({reservation: newresa}); - res.end(); }); router.put('/tp/:id/reserve/extend', async function(req, res) { if(! req.locals.logInfo.is_logged) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } if(! req.body.to) { res.status(403).send({message: 'No input'}); - res.end(); return; } if(! sansrv.sanitizeAll([req.params.id])) { res.status(403).send({message: 'Invalid parameters'}); - res.end(); return; } @@ -365,19 +319,16 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } catch(e) { logger.error(e); res.status(404).send({message: 'User session not found'}); - res.end(); return; } if(!user) { res.status(404).send({message: 'User does not exist'}); - res.end(); return; } if(!is_admin) { res.status(403).send({message: 'Not authorized'}); - res.end(); return; } @@ -385,17 +336,14 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { let reservation = await dbsrv.mongo_reservations().findOne({_id: reservation_id}); if(!reservation){ res.status(404).send({message: 'Reservation does not exist'}); - res.end(); return; } if (req.body.to < reservation.to) { res.status(403).send({message: 'Extended end date must be after current end date'}); - res.end(); return; } if (req.body.to < new Date().getTime()) { res.status(403).send({message: 'Extended end date can not be in the past'}); - res.end(); return; } @@ -404,12 +352,10 @@ router.put('/tp/:id/reserve/extend', async function(req, res) { } catch (error) { logger.error(error); res.status(500).send({message: 'Error while extending tp reservation'}); - res.end(); return; } res.send({message: 'Reservation extended'}); - res.end(); }); module.exports = router; From 6909a57616dfcc94523e188dc882658cba2715a3 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 29 May 2024 13:42:47 +0200 Subject: [PATCH 34/39] update test --- test/my.js | 99 +++++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 45 deletions(-) diff --git a/test/my.js b/test/my.js index e53e3286a..52a8d6cab 100644 --- a/test/my.js +++ b/test/my.js @@ -24,6 +24,7 @@ var test_group_id3 = 'test3' + Math.random().toString(10).slice(-6); var test_web_id = 'webtest' + Math.random().toString(10).slice(-6); var test_db_id = 'dbtest' + Math.random().toString(10).slice(-6); var test_project_id = 'projecttest' + Math.random().toString(10).slice(-6); +var test_tp_id = 'tptest' + Math.random().toString(10).slice(-6); var user_test_password = null; var user_info = null; @@ -515,7 +516,7 @@ describe('My', () => { .end((err, res) => { expect(res).to.have.status(200); let found = false; - for(let i=0; i { }); describe('Test tp reservation', () => { - it('create a reservation', (done) => { - const create_and_force = async () => { - let today = new Date(); - let res = await chai.request('http://localhost:3000') - .post('/tp') - .set('X-Api-Key', token_id) - .send({ - 'from': today.getTime(), - 'to': today.getTime() + 30, - 'quantity': 2, - 'about': 'test resa', - 'group_or_project': 'group', - 'name': 'test tp' - }); - expect(res).to.have.status(200); - let new_resa = res.body.reservation; - console.error('new resa', new_resa); - assert(new_resa.created == false); - let res2 = await chai.request('http://localhost:3000') - .get('/tp/' + new_resa._id) - .set('X-Api-Key', token_id); - let resa = res2.body.reservation; - expect(res2).to.have.status(200); - assert(resa.created == false); - // Reserve now /tp/:id/reservenow - let resnow = await chai.request('http://localhost:3000') - .put('/tp/' + resa._id + '/reserve/now') - .set('X-Api-Key', token_id) - .send({ - 'from': today.getTime(), - 'to': today.getTime() + 30, - 'quantity': 2, - 'about': 'test resa' - }); - expect(resnow).to.have.status(200); - res2 = await chai.request('http://localhost:3000') - .get('/tp/' + new_resa._id) - .set('X-Api-Key', token_id); - resa = res2.body.reservation; - assert(resa.created == true); - }; - create_and_force().then(() => { - done(); - }); + it('Book reservation', (done) => { + let today = new Date(); + chai.request('http://localhost:3000') + .post('/tp') + .set('X-Api-Key', token_id) + .send({ + 'from': today.getTime(), + 'to': today.getTime() + 30000, + 'quantity': 2, + 'about': 'test resa', + 'group_or_project': 'group', + 'name': 'test tp' + }) + .end((err, res) => { + expect(res).to.have.status(200); + let new_resa = res.body.reservation; + assert(new_resa.created == false); + test_tp_id = new_resa._id; + chai.request('http://localhost:3000') + .get('/tp/' + test_tp_id) + .set('X-Api-Key', token_id) + .end((err, res2) => { + expect(res2).to.have.status(200); + let resa = res2.body.reservation; + assert(resa.created == false); + done(); + }); + }); }); + it('Create reservation', (done) => { + let today = new Date(); + chai.request('http://localhost:3000') + .put('/tp/' + test_tp_id + '/reserve/now') + .set('X-Api-Key', token_id) + .send({ + 'from': today.getTime(), + 'to': today.getTime() + 30000, + 'quantity': 2, + 'about': 'test resa' + }) + .end((err, resnow) => { + expect(resnow).to.have.status(200); + chai.request('http://localhost:3000') + .get('/tp/' + test_tp_id) + .set('X-Api-Key', token_id) + .end((err, res2) => { + expect(res2).to.have.status(200); + let resa = res2.body.reservation; + assert(resa.created == true); + done(); + }); + }); + }); }); }); From e75475708c4ab14467dc59b45d94d2af570e8f7a Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 29 May 2024 14:00:58 +0200 Subject: [PATCH 35/39] add new tests --- test/my.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/test/my.js b/test/my.js index 52a8d6cab..26b35c828 100644 --- a/test/my.js +++ b/test/my.js @@ -775,7 +775,7 @@ describe('My', () => { .set('X-Api-Key', token_id) .send({ 'from': today.getTime(), - 'to': today.getTime() + 30000, + 'to': today.getTime() + 24*3600*1000, 'quantity': 2, 'about': 'test resa', 'group_or_project': 'group', @@ -805,7 +805,7 @@ describe('My', () => { .set('X-Api-Key', token_id) .send({ 'from': today.getTime(), - 'to': today.getTime() + 30000, + 'to': today.getTime() + 24*3600*1000, 'quantity': 2, 'about': 'test resa' }) @@ -822,6 +822,35 @@ describe('My', () => { }); }); }); + + it('Extend reservation', (done) => { + let today = new Date(); + chai.request('http://localhost:3000') + .put('/tp/' + test_tp_id + '/reserve/extend') + .set('X-Api-Key', token_id) + .send({'to': today.getTime() + 2*24*3600*10000}) + .end((err, resnow) => { + expect(resnow).to.have.status(200); + chai.request('http://localhost:3000') + .get('/tp/' + test_tp_id) + .set('X-Api-Key', token_id) + .end((err, res2) => { + expect(res2).to.have.status(200); + let resa = res2.body.reservation; + assert(resa.created == true); + done(); + }); + }); + }); + + it('Remove reservation', (done) => { + chai.request('http://localhost:3000') + .put('/tp/' + test_tp_id + '/reserve/stop') + .set('X-Api-Key', token_id) + .end((err, resnow) => { + expect(resnow).to.have.status(200); + }); + }); }); }); From d818e44c1231e3a8c049fe5b9188dcf9fc2808df Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 29 May 2024 14:06:27 +0200 Subject: [PATCH 36/39] fix --- test/my.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/my.js b/test/my.js index 26b35c828..3c5f40d93 100644 --- a/test/my.js +++ b/test/my.js @@ -849,6 +849,7 @@ describe('My', () => { .set('X-Api-Key', token_id) .end((err, resnow) => { expect(resnow).to.have.status(200); + done(); }); }); }); From f2bbb82f49f011f64f248301b55b4e1e961d38b4 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Wed, 29 May 2024 14:20:32 +0200 Subject: [PATCH 37/39] fix2 --- test/my.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/my.js b/test/my.js index 3c5f40d93..130889088 100644 --- a/test/my.js +++ b/test/my.js @@ -837,7 +837,7 @@ describe('My', () => { .end((err, res2) => { expect(res2).to.have.status(200); let resa = res2.body.reservation; - assert(resa.created == true); + assert(resa.to == today.getTime() + 2*24*3600*10000); done(); }); }); From b5b6f4725479943244b7848de06fa81d8d1cd39a Mon Sep 17 00:00:00 2001 From: remy siminel Date: Thu, 30 May 2024 13:50:24 +0200 Subject: [PATCH 38/39] fix --- core/tps.service.js | 2 -- manager2/src/app/tps/tps.component.html | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/core/tps.service.js b/core/tps.service.js index aa0838cd9..65ac15439 100644 --- a/core/tps.service.js +++ b/core/tps.service.js @@ -257,7 +257,6 @@ async function extend_tp_users(users, extension) { async function remove_tp_reservation(reservation) { - logger.info('Close reservation', reservation._id); logger.debug('Close reservation', reservation); if (reservation.accounts) { @@ -289,7 +288,6 @@ async function remove_tp_reservation(reservation) { } async function extend_tp_reservation(reservation, extension) { - logger.info('Extend reservation', reservation._id); logger.debug('Extend reservation', reservation); try { diff --git a/manager2/src/app/tps/tps.component.html b/manager2/src/app/tps/tps.component.html index e1414224f..e06689a1b 100644 --- a/manager2/src/app/tps/tps.component.html +++ b/manager2/src/app/tps/tps.component.html @@ -94,8 +94,8 @@

Reservation

- + From f0e149f3f90d5e2422056307b138abc507ba2996 Mon Sep 17 00:00:00 2001 From: remy siminel Date: Thu, 30 May 2024 14:10:53 +0200 Subject: [PATCH 39/39] fix UX --- manager2/src/app/tps/tps.component.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manager2/src/app/tps/tps.component.html b/manager2/src/app/tps/tps.component.html index e06689a1b..2ba3bddaa 100644 --- a/manager2/src/app/tps/tps.component.html +++ b/manager2/src/app/tps/tps.component.html @@ -92,8 +92,8 @@

Reservation

- +