Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended tp registrations #465

Merged
merged 40 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7117c5a
create extend button
rsiminel May 7, 2024
a79867a
create extend service
rsiminel May 7, 2024
f13e478
create extend api
rsiminel May 7, 2024
7588ef6
add param to extend back
rsiminel May 13, 2024
4e1d546
add param to extend front
rsiminel May 13, 2024
5064bd2
add TP extension form
rsiminel May 13, 2024
2ed3bde
wording
rsiminel May 13, 2024
6b74a81
fix
rsiminel May 13, 2024
456f36e
add route to app.js
rsiminel May 14, 2024
f622561
convert expiration date to unix
rsiminel May 15, 2024
31b79a8
generalize extending to editing
rsiminel May 15, 2024
0b059ee
lint
rsiminel May 15, 2024
31cd19b
catch date advance error
rsiminel May 15, 2024
8ed8e8d
un-generalize
rsiminel May 15, 2024
8698345
catch date advance error backend
rsiminel May 15, 2024
856cf35
deal with date advance error
rsiminel May 16, 2024
f4255b7
remove comments
rsiminel May 21, 2024
4f9c2ac
pass less info through route
rsiminel May 21, 2024
96e96d8
extend users and project
rsiminel May 21, 2024
a1dd048
prevent retroactive extension or creation
rsiminel May 21, 2024
e52dfb7
get rid of useless 'res.end();'s
rsiminel May 21, 2024
cff5ee8
fix tp user extension
rsiminel May 22, 2024
4476805
fixes
rsiminel May 22, 2024
ff5ae26
fix01
rsiminel May 22, 2024
43b4bd6
Merge branch 'genouest:master' into extended-TP-registrations
rsiminel May 23, 2024
08c31df
update changelog
rsiminel May 23, 2024
587c738
only allow admin to extend tp
rsiminel May 23, 2024
a84131d
fix
rsiminel May 24, 2024
8fa9e8e
fix dumbassery
rsiminel May 27, 2024
a65f1aa
push user history to db
rsiminel May 28, 2024
1b74e45
reduce db queries
rsiminel May 28, 2024
ffc5b4e
lint
rsiminel May 28, 2024
e8ab7c0
fix
rsiminel May 28, 2024
76f1bb9
un-fix
rsiminel May 29, 2024
6909a57
update test
rsiminel May 29, 2024
e754757
add new tests
rsiminel May 29, 2024
d818e44
fix
rsiminel May 29, 2024
f2bbb82
fix2
rsiminel May 29, 2024
b5b6f47
fix
rsiminel May 30, 2024
f0e149f
fix UX
rsiminel May 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ app.delete('/tp/:id', tp);
app.get('/tp/:id', tp);
app.put('/tp/:id/reserve/now', tp);
app.put('/tp/:id/reserve/stop', tp);
app.put('/tp/:id/reserve/extend', tp);

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


async function createExtraGroup(trainingName, ownerName) {
Expand Down Expand Up @@ -253,6 +254,25 @@ async function remove_tp_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});
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
logger.debug('Extend reservation', reservation);

try {
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) {
logger.error(error);
}

}

async function create_tp_reservation(reservation_id) {

logger.info('Create reservation', reservation_id);
Expand Down
29 changes: 29 additions & 0 deletions manager2/src/app/tps/tps.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,42 @@ <h3>Reservation</h3>
</table>
</div>
<button *ngIf="!selectedEvent.created && !selectedEvent.over && authorized" class="p-button p-button-sm p-button-secondary" (click)="cancel_reservation()">Cancel</button>
<!-- <button *ngIf="selectedEvent.created && authorized && !session_user.is_admin" class="p-button p-button-sm p-button-primary" (click)="ask_extend_reservation()">Ask for Extension</button> -->
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
<button *ngIf="!selectedEvent.created && session_user.is_admin" class="p-button p-button-sm p-button-primary" (click)="create_reservation()">Create</button>
<button *ngIf="selectedEvent.created && !selectedEvent.over && session_user.is_admin" class="p-button p-button-sm p-button-warning" (click)="remove_reservation()">Remove</button>
<button *ngIf="selectedEvent.created && !selectedEvent.over && session_user.is_admin" type="button" class="p-button p-button-sm p-button-primary" data-toggle="modal" data-target="#extend">Extend</button>
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
</div>
</div>
</div>
</div>

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

<div *ngIf="authorized" class="row">
<div class="col-md-8">
<div class="card bg-light">
Expand Down
17 changes: 17 additions & 0 deletions manager2/src/app/tps/tps.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export class TpsComponent implements OnInit {

activeDayIsOpen: boolean = true;

new_expire: Date

constructor(
private authService: AuthService,
private configService: ConfigService,
Expand Down Expand Up @@ -174,6 +176,21 @@ export class TpsComponent implements OnInit {
this.listEvents();
}

extend_reservation(new_expire: Date) {
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
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) ?
mboudet marked this conversation as resolved.
Show resolved Hide resolved
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(
resp => this.msg = resp['message'],
err => this.errmsg = err.error.message
);
this.listEvents();
}

eventClicked(clickedEvent) {
let event = clickedEvent.event;
this.selectedEvent = event.meta;
Expand Down
11 changes: 11 additions & 0 deletions manager2/src/app/tps/tpservice.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,15 @@ export class TpserviceService {

return this.http.put(environment.apiUrl + '/tp/' + reservationId + '/reserve/stop', httpOptions)
}

extend(reservationId, new_reservation): Observable<any> {
//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', new_reservation, httpOptions)
}
}
71 changes: 69 additions & 2 deletions routes/tp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -339,4 +339,71 @@ 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])) {
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
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.status(404).send({message: 'User does not exist'}); // Why do none of the other routes give a 404 status here ?
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
res.end();
return;
}

if(! (isadmin || (user.is_trainer !== undefined && user.is_trainer))) {
res.status(403).send({message: 'Not authorized'});
return;
}

let reservation_id = ObjectID.createFromHexString(req.params.id);

let filter = {};
if(isadmin) {
filter = {_id: reservation_id};
}
else{
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
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 extend this reservation'});
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
res.end();
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 ?
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
return;
}

try {
tpssrv.extend_tp_reservation(reservation_id, req.body);
} catch (error) {
logger.error(error);
res.status(500).send({message: 'Error while extending tp reservation'});
res.end();
return;
}

res.status(200).send({message: 'Reservation extended'}); // Why do none of the other routes give a 200 status on success ?
rsiminel marked this conversation as resolved.
Show resolved Hide resolved
res.end();
});

module.exports = router;