From e9f3fb7e9c475ae216ff4843e775640ad57b4bac Mon Sep 17 00:00:00 2001 From: OxSaitama Date: Wed, 26 Jul 2023 11:49:32 +0200 Subject: [PATCH] add error handling --- .../api/super-admin/AppointmentsController.ts | 9 ++++- src/app/api/super-admin/VotesController.ts | 35 +++++++++++++------ .../super-admin/VotesService/VotesService.ts | 25 ++++++------- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/app/api/super-admin/AppointmentsController.ts b/src/app/api/super-admin/AppointmentsController.ts index 936774c2..fa143534 100644 --- a/src/app/api/super-admin/AppointmentsController.ts +++ b/src/app/api/super-admin/AppointmentsController.ts @@ -6,11 +6,12 @@ import { Service } from "typedi"; import { Appointment } from "le-coffre-resources/dist/SuperAdmin"; import { validateOrReject } from "class-validator"; import authHandler from "@App/middlewares/AuthHandler"; +import UsersService from "@Services/super-admin/UsersService/UsersService"; @Controller() @Service() export default class AppointmentsController extends ApiController { - constructor(private appointmentsService: AppointmentsService) { + constructor(private appointmentsService: AppointmentsService, private usersService: UsersService) { super(); } @@ -51,6 +52,12 @@ export default class AppointmentsController extends ApiController { //validate user await validateOrReject(appointmentEntity, { groups: ["createAppointment"]}); + const targetedUser = await this.usersService.getByUid(appointmentEntity.targeted_user.uid!); + if(!targetedUser) { + this.httpNotFoundRequest(response, "targeted user not found"); + return; + } + //call service to get prisma entity const appointmentEntityCreated = await this.appointmentsService.create(appointmentEntity); //Hydrate ressource with prisma entity diff --git a/src/app/api/super-admin/VotesController.ts b/src/app/api/super-admin/VotesController.ts index eac8b6a9..71057e82 100644 --- a/src/app/api/super-admin/VotesController.ts +++ b/src/app/api/super-admin/VotesController.ts @@ -7,12 +7,13 @@ import { Vote } from "le-coffre-resources/dist/SuperAdmin"; import { validateOrReject } from "class-validator"; import authHandler from "@App/middlewares/AuthHandler"; import UsersService from "@Services/super-admin/UsersService/UsersService"; -import { Votes } from "@prisma/client"; +import { EAppointmentStatus, Votes } from "@prisma/client"; +import AppointmentService from "@Services/super-admin/AppointmentsService/AppointmentsService"; @Controller() @Service() export default class VotesController extends ApiController { - constructor(private votesService: VotesService, private usersService: UsersService) { + constructor(private votesService: VotesService, private usersService: UsersService, private appointmentService: AppointmentService) { super(); } @@ -52,14 +53,29 @@ export default class VotesController extends ApiController { //init IUser resource with request body values const voteEntity = Vote.hydrate(req.body); //validate user - await validateOrReject(voteEntity, { groups: ["createVote"]}); + await validateOrReject(voteEntity, { groups: ["createVote"] }); + + const appointment = await this.appointmentService.getByUid(voteEntity.appointment.uid!); + if (!appointment) { + this.httpNotFoundRequest(response, "Appointment not found"); + return; + } + if (appointment.status === EAppointmentStatus.CLOSED) { + this.httpBadRequest(response, "Appointment is closed"); + return; + } + + const votes = await this.votesService.get({ + where: { AND: [{ appointment: { uid: voteEntity.appointment.uid } }, { voter: { uid: userId } }] }, + }); + console.log(votes); + if (votes.length) { + this.httpBadRequest(response, "Voter already voted for this appointment"); + return; + } - const votes = await this.votesService.get({ where: { AND: [{ appointment: {uid: voteEntity.uid } }, {voter: {uid: userId}}]}}); - console.log(votes) - if (votes.length) throw new Error("Voter already voted for this appointment"); - const voter = await this.usersService.getByUid(userId); - + voteEntity.voter = voter!; //call service to get prisma entity const voteEntityCreated = await this.votesService.create(voteEntity); @@ -92,7 +108,7 @@ export default class VotesController extends ApiController { if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); } - + const voteEntity = await this.votesService.getByUid(uid, query); if (!voteEntity) { @@ -143,5 +159,4 @@ export default class VotesController extends ApiController { return; } } - } diff --git a/src/services/super-admin/VotesService/VotesService.ts b/src/services/super-admin/VotesService/VotesService.ts index bdd95fe4..663058ef 100644 --- a/src/services/super-admin/VotesService/VotesService.ts +++ b/src/services/super-admin/VotesService/VotesService.ts @@ -1,6 +1,6 @@ import BaseService from "@Services/BaseService"; import { Service } from "typedi"; -import { Vote } from "le-coffre-resources/dist/SuperAdmin"; +import User, { Role, Vote } from "le-coffre-resources/dist/SuperAdmin"; import VotesRepository from "@Repositories/VotesRepository"; import { EAppointmentStatus, EVote, Prisma, Votes } from "@prisma/client"; import AppointmentService from "../AppointmentsService/AppointmentsService"; @@ -32,23 +32,24 @@ export default class VoteService extends BaseService { */ public async create(vote: Vote): Promise { const appointment = await this.appointmentService.getByUidWithVotes(vote.appointment.uid!); - if (!appointment) throw new Error("Appointment not found"); - if (appointment.status === EAppointmentStatus.CLOSED) throw new Error("Appointment is closed"); - if (appointment.votes.length >= 2) { + if (appointment!.votes.length >= 2) { const voteCreated = await this.voteRepository.create(vote); - await this.appointmentService.update(appointment.uid!, EAppointmentStatus.CLOSED); - const user = await this.userService.getByUid(appointment.user_uid); + await this.appointmentService.update(appointment!.uid!, EAppointmentStatus.CLOSED); + const user = await this.userService.getByUid(appointment!.user_uid, { role: true }); + const userEntity = User.hydrate(user!, { strategy: "excludeAll" }); - if (appointment.choice === EVote.DISMISS) { + if (appointment!.choice === EVote.DISMISS) { const roles = await this.roleService.get({ where: { name: "default" } }); - user!.roles_uid = roles[0]!.uid; - await this.userService.update(appointment.user_uid, user!); + const roleEntity = Role.hydrate(roles[0]!, { strategy: "excludeAll" }); + userEntity.role = roleEntity; + await this.userService.update(appointment!.user_uid, userEntity); return voteCreated; - } else if (appointment.choice === EVote.NOMINATE) { + } else if (appointment!.choice === EVote.NOMINATE) { const roles = await this.roleService.get({ where: { name: "super-admin" } }); - user!.roles_uid = roles[0]!.uid; - await this.userService.update(appointment.user_uid, user!); + const roleEntity = Role.hydrate(roles[0]!, { strategy: "excludeAll" }); + userEntity!.role = roleEntity; + await this.userService.update(appointment!.user_uid, userEntity); return voteCreated; } }