diff --git a/src/app/api/super-admin/LiveVoteController.ts b/src/app/api/super-admin/LiveVoteController.ts index 295ab1a7..30494cf2 100644 --- a/src/app/api/super-admin/LiveVoteController.ts +++ b/src/app/api/super-admin/LiveVoteController.ts @@ -1,6 +1,5 @@ import authHandler from "@App/middlewares/AuthHandler"; import roleHandler from "@App/middlewares/RolesHandler"; -import NotificationBuilder from "@Common/notifications/NotificationBuilder"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Controller, Delete, Post } from "@ControllerPattern/index"; import { EAppointmentStatus, Votes } from "@prisma/client"; @@ -17,7 +16,6 @@ export default class LiveVoteController extends ApiController { constructor( private liveVoteService: LiveVoteService, private usersService: UsersService, - private notificationBuilder: NotificationBuilder, ) { super(); } @@ -81,8 +79,6 @@ export default class LiveVoteController extends ApiController { //Hydrate ressource with prisma entity const vote = Vote.hydrate(voteEntityCreated, { strategy: "excludeAll" }); - await this.notificationBuilder.sendVoteNotification(vote); - //success this.httpCreated(response, vote); } catch (error) { diff --git a/src/common/notifications/NotificationBuilder.ts b/src/common/notifications/NotificationBuilder.ts index 30e88fc1..e51abb68 100644 --- a/src/common/notifications/NotificationBuilder.ts +++ b/src/common/notifications/NotificationBuilder.ts @@ -69,6 +69,8 @@ export default class NotificationBuilder { public async sendVoteNotification(vote: Vote) { if (vote.appointment.status !== "OPEN") return; const superAdminList = await this.usersService.get({ where: { role: { name: "super-admin" } } }); + const voterIndex = superAdminList.findIndex((user) => user.uid === vote.voter.uid); + superAdminList.splice(voterIndex, 1); const userTargeted = await this.usersService.getByUid(vote.appointment.user.uid!, { contact: true }); const userTargetedEntity = User.hydrate(userTargeted!, { strategy: "excludeAll" }); let message = ""; @@ -83,7 +85,7 @@ export default class NotificationBuilder { redirection_url: `${this.backendVariables.APP_HOST}/users/${vote.appointment.user.uid}`, created_at: new Date(), updated_at: new Date(), - user: superAdminList || [], + user: superAdminList, }); } diff --git a/src/common/repositories/VotesRepository.ts b/src/common/repositories/VotesRepository.ts index d5f2bf55..a48243df 100644 --- a/src/common/repositories/VotesRepository.ts +++ b/src/common/repositories/VotesRepository.ts @@ -61,7 +61,7 @@ export default class VotesRepository extends BaseRepository { } }; - return this.model.create({...createArgs, include: {appointment: {include: {votes: true, user: true}}}}); + return this.model.create({...createArgs, include: {voter: true, appointment: {include: {votes: true, user: true}}}}); } /** diff --git a/src/services/super-admin/LiveVoteService/LiveVoteService.ts b/src/services/super-admin/LiveVoteService/LiveVoteService.ts index 65aaa726..804a888c 100644 --- a/src/services/super-admin/LiveVoteService/LiveVoteService.ts +++ b/src/services/super-admin/LiveVoteService/LiveVoteService.ts @@ -67,7 +67,7 @@ export default class LiveVoteService extends BaseService { return this.appointmentRepository.findOneByUid(uid, query); } - public async getAppointmentWithVotes(vote: Vote): Promise<(Appointments & {votes: Votes[], user: Users}) | null> { + public async getAppointmentWithVotes(vote: Vote): Promise<(Appointments & { votes: Votes[]; user: Users }) | null> { if (vote.appointment.uid) { return this.appointmentRepository.findOneByUidWithVotes(vote.appointment.uid); } @@ -88,8 +88,12 @@ export default class LiveVoteService extends BaseService { } private async closeVote(appointment: Appointments, vote: Votes) { - const apointmentFound = await this.appointmentRepository.findOneByStatusUserAndChoice(appointment.user_uid, EVote[appointment.choice as keyof typeof EVote], EAppointmentStatus.CLOSED); - if(apointmentFound) { + const apointmentFound = await this.appointmentRepository.findOneByStatusUserAndChoice( + appointment.user_uid, + EVote[appointment.choice as keyof typeof EVote], + EAppointmentStatus.CLOSED, + ); + if (apointmentFound) { await this.appointmentRepository.delete(apointmentFound.uid); } await this.appointmentRepository.update(vote.appointment_uid, EAppointmentStatus.CLOSED); @@ -127,7 +131,7 @@ export default class LiveVoteService extends BaseService { if (appointment) { const voteEntity = Vote.hydrateArray(appointment.votes, { strategy: "excludeAll" }); - const appointementWithVotesHydrated = {...appointment, votes: voteEntity}; + const appointementWithVotesHydrated = { ...appointment, votes: voteEntity }; const appointmentEntity = Appointment.hydrate(appointementWithVotesHydrated, { strategy: "excludeAll" }); if (appointmentEntity.votes && appointmentEntity.votes.length >= 2) { const voteCreated = await this.voteRepository.create(vote); @@ -137,7 +141,9 @@ export default class LiveVoteService extends BaseService { const approvedChoice = await this.verifyVoterChoice(vote); if (!approvedChoice) return null; - - return this.voteRepository.create(vote); + const voteCreated = await this.voteRepository.create(vote); + const voteEntity = Vote.hydrate(voteCreated, { strategy: "excludeAll" }); + await this.notificationBuilder.sendVoteNotification(voteEntity); + return voteCreated; } }