From 2fc307f3c3b44eb5c9bb87382e8fcf3bd1038e3c Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Wed, 27 Sep 2023 16:34:23 +0200 Subject: [PATCH] :sparkles: Delete appointment if no votes in appointment --- src/app/api/super-admin/LiveVoteController.ts | 16 ++++++++++++---- .../LiveVoteService/LiveVoteService.ts | 8 ++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/app/api/super-admin/LiveVoteController.ts b/src/app/api/super-admin/LiveVoteController.ts index ade311ca..65956b50 100644 --- a/src/app/api/super-admin/LiveVoteController.ts +++ b/src/app/api/super-admin/LiveVoteController.ts @@ -105,7 +105,11 @@ export default class LiveVoteController extends ApiController { return; } - const voteFound = await this.liveVoteService.getVoteByUid(uid); + const voteFound = await this.liveVoteService.getVoteByUid(uid, { + appointment: { + include: { votes: true }, + }, + }); if (!voteFound) { this.httpNotFoundRequest(response, "vote not found"); @@ -117,14 +121,18 @@ export default class LiveVoteController extends ApiController { return; } + const vote = Vote.hydrate(voteFound, { strategy: "excludeAll" }); + //call service to get prisma entity const voteEntity: Votes = await this.liveVoteService.deleteVote(uid); - //Hydrate ressource with prisma entity - const vote = Vote.hydrate(voteEntity, { strategy: "excludeAll" }); + if (vote.appointment.uid && vote.appointment.votes && vote.appointment.votes.length === 1) { + await this.liveVoteService.deleteAppointment(vote.appointment.uid); + } + const voteToReturn = Vote.hydrate(voteEntity, { strategy: "excludeAll" }); //success - this.httpSuccess(response, vote); + this.httpSuccess(response, voteToReturn); } catch (error) { this.httpInternalError(response, error); return; diff --git a/src/services/super-admin/LiveVoteService/LiveVoteService.ts b/src/services/super-admin/LiveVoteService/LiveVoteService.ts index 028078f6..a034f976 100644 --- a/src/services/super-admin/LiveVoteService/LiveVoteService.ts +++ b/src/services/super-admin/LiveVoteService/LiveVoteService.ts @@ -49,6 +49,14 @@ export default class LiveVoteService extends BaseService { return this.voteRepository.delete(uid); } + /** + * @description : delete an appointment by uid + * @throws {Error} If appointment cannot be get by uid + */ + public deleteAppointment(uid: string) { + return this.appointmentRepository.delete(uid); + } + /** * @description : Get a appointment by uid * @throws {Error} If appointment cannot be get by uid