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