fix sending vote notifications (#126)

This commit is contained in:
Arnaud D. Natali 2023-10-06 14:49:11 +02:00 committed by GitHub
commit f86040af55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 12 deletions

View File

@ -1,6 +1,5 @@
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
import roleHandler from "@App/middlewares/RolesHandler"; import roleHandler from "@App/middlewares/RolesHandler";
import NotificationBuilder from "@Common/notifications/NotificationBuilder";
import ApiController from "@Common/system/controller-pattern/ApiController"; import ApiController from "@Common/system/controller-pattern/ApiController";
import { Controller, Delete, Post } from "@ControllerPattern/index"; import { Controller, Delete, Post } from "@ControllerPattern/index";
import { EAppointmentStatus, Votes } from "@prisma/client"; import { EAppointmentStatus, Votes } from "@prisma/client";
@ -17,7 +16,6 @@ export default class LiveVoteController extends ApiController {
constructor( constructor(
private liveVoteService: LiveVoteService, private liveVoteService: LiveVoteService,
private usersService: UsersService, private usersService: UsersService,
private notificationBuilder: NotificationBuilder,
) { ) {
super(); super();
} }
@ -81,8 +79,6 @@ export default class LiveVoteController extends ApiController {
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const vote = Vote.hydrate<Vote>(voteEntityCreated, { strategy: "excludeAll" }); const vote = Vote.hydrate<Vote>(voteEntityCreated, { strategy: "excludeAll" });
await this.notificationBuilder.sendVoteNotification(vote);
//success //success
this.httpCreated(response, vote); this.httpCreated(response, vote);
} catch (error) { } catch (error) {

View File

@ -69,6 +69,8 @@ export default class NotificationBuilder {
public async sendVoteNotification(vote: Vote) { public async sendVoteNotification(vote: Vote) {
if (vote.appointment.status !== "OPEN") return; if (vote.appointment.status !== "OPEN") return;
const superAdminList = await this.usersService.get({ where: { role: { name: "super-admin" } } }); 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 userTargeted = await this.usersService.getByUid(vote.appointment.user.uid!, { contact: true });
const userTargetedEntity = User.hydrate<User>(userTargeted!, { strategy: "excludeAll" }); const userTargetedEntity = User.hydrate<User>(userTargeted!, { strategy: "excludeAll" });
let message = ""; let message = "";
@ -83,7 +85,7 @@ export default class NotificationBuilder {
redirection_url: `${this.backendVariables.APP_HOST}/users/${vote.appointment.user.uid}`, redirection_url: `${this.backendVariables.APP_HOST}/users/${vote.appointment.user.uid}`,
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
user: superAdminList || [], user: superAdminList,
}); });
} }

View File

@ -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}}}});
} }
/** /**

View File

@ -67,7 +67,7 @@ export default class LiveVoteService extends BaseService {
return this.appointmentRepository.findOneByUid(uid, query); 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) { if (vote.appointment.uid) {
return this.appointmentRepository.findOneByUidWithVotes(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) { 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); const apointmentFound = await this.appointmentRepository.findOneByStatusUserAndChoice(
if(apointmentFound) { appointment.user_uid,
EVote[appointment.choice as keyof typeof EVote],
EAppointmentStatus.CLOSED,
);
if (apointmentFound) {
await this.appointmentRepository.delete(apointmentFound.uid); await this.appointmentRepository.delete(apointmentFound.uid);
} }
await this.appointmentRepository.update(vote.appointment_uid, EAppointmentStatus.CLOSED); await this.appointmentRepository.update(vote.appointment_uid, EAppointmentStatus.CLOSED);
@ -127,7 +131,7 @@ export default class LiveVoteService extends BaseService {
if (appointment) { if (appointment) {
const voteEntity = Vote.hydrateArray<Vote>(appointment.votes, { strategy: "excludeAll" }); const voteEntity = Vote.hydrateArray<Vote>(appointment.votes, { strategy: "excludeAll" });
const appointementWithVotesHydrated = {...appointment, votes: voteEntity}; const appointementWithVotesHydrated = { ...appointment, votes: voteEntity };
const appointmentEntity = Appointment.hydrate<Appointment>(appointementWithVotesHydrated, { strategy: "excludeAll" }); const appointmentEntity = Appointment.hydrate<Appointment>(appointementWithVotesHydrated, { strategy: "excludeAll" });
if (appointmentEntity.votes && appointmentEntity.votes.length >= 2) { if (appointmentEntity.votes && appointmentEntity.votes.length >= 2) {
const voteCreated = await this.voteRepository.create(vote); const voteCreated = await this.voteRepository.create(vote);
@ -137,7 +141,9 @@ export default class LiveVoteService extends BaseService {
const approvedChoice = await this.verifyVoterChoice(vote); const approvedChoice = await this.verifyVoterChoice(vote);
if (!approvedChoice) return null; if (!approvedChoice) return null;
const voteCreated = await this.voteRepository.create(vote);
return this.voteRepository.create(vote); const voteEntity = Vote.hydrate<Vote>(voteCreated, { strategy: "excludeAll" });
await this.notificationBuilder.sendVoteNotification(voteEntity);
return voteCreated;
} }
} }