add error handling

This commit is contained in:
OxSaitama 2023-07-26 11:49:32 +02:00
parent 0a675027ca
commit e9f3fb7e9c
3 changed files with 46 additions and 23 deletions

View File

@ -6,11 +6,12 @@ import { Service } from "typedi";
import { Appointment } from "le-coffre-resources/dist/SuperAdmin"; import { Appointment } from "le-coffre-resources/dist/SuperAdmin";
import { validateOrReject } from "class-validator"; import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
import UsersService from "@Services/super-admin/UsersService/UsersService";
@Controller() @Controller()
@Service() @Service()
export default class AppointmentsController extends ApiController { export default class AppointmentsController extends ApiController {
constructor(private appointmentsService: AppointmentsService) { constructor(private appointmentsService: AppointmentsService, private usersService: UsersService) {
super(); super();
} }
@ -51,6 +52,12 @@ export default class AppointmentsController extends ApiController {
//validate user //validate user
await validateOrReject(appointmentEntity, { groups: ["createAppointment"]}); 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 //call service to get prisma entity
const appointmentEntityCreated = await this.appointmentsService.create(appointmentEntity); const appointmentEntityCreated = await this.appointmentsService.create(appointmentEntity);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity

View File

@ -7,12 +7,13 @@ import { Vote } from "le-coffre-resources/dist/SuperAdmin";
import { validateOrReject } from "class-validator"; import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
import UsersService from "@Services/super-admin/UsersService/UsersService"; 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() @Controller()
@Service() @Service()
export default class VotesController extends ApiController { export default class VotesController extends ApiController {
constructor(private votesService: VotesService, private usersService: UsersService) { constructor(private votesService: VotesService, private usersService: UsersService, private appointmentService: AppointmentService) {
super(); super();
} }
@ -54,9 +55,24 @@ export default class VotesController extends ApiController {
//validate user //validate user
await validateOrReject(voteEntity, { groups: ["createVote"] }); await validateOrReject(voteEntity, { groups: ["createVote"] });
const votes = await this.votesService.get({ where: { AND: [{ appointment: {uid: voteEntity.uid } }, {voter: {uid: userId}}]}}); const appointment = await this.appointmentService.getByUid(voteEntity.appointment.uid!);
console.log(votes) if (!appointment) {
if (votes.length) throw new Error("Voter already voted for this 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 voter = await this.usersService.getByUid(userId); const voter = await this.usersService.getByUid(userId);
@ -143,5 +159,4 @@ export default class VotesController extends ApiController {
return; return;
} }
} }
} }

View File

@ -1,6 +1,6 @@
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { Service } from "typedi"; 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 VotesRepository from "@Repositories/VotesRepository";
import { EAppointmentStatus, EVote, Prisma, Votes } from "@prisma/client"; import { EAppointmentStatus, EVote, Prisma, Votes } from "@prisma/client";
import AppointmentService from "../AppointmentsService/AppointmentsService"; import AppointmentService from "../AppointmentsService/AppointmentsService";
@ -32,23 +32,24 @@ export default class VoteService extends BaseService {
*/ */
public async create(vote: Vote): Promise<Votes> { public async create(vote: Vote): Promise<Votes> {
const appointment = await this.appointmentService.getByUidWithVotes(vote.appointment.uid!); 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); const voteCreated = await this.voteRepository.create(vote);
await this.appointmentService.update(appointment.uid!, EAppointmentStatus.CLOSED); await this.appointmentService.update(appointment!.uid!, EAppointmentStatus.CLOSED);
const user = await this.userService.getByUid(appointment.user_uid); const user = await this.userService.getByUid(appointment!.user_uid, { role: true });
const userEntity = User.hydrate<User>(user!, { strategy: "excludeAll" });
if (appointment.choice === EVote.DISMISS) { if (appointment!.choice === EVote.DISMISS) {
const roles = await this.roleService.get({ where: { name: "default" } }); const roles = await this.roleService.get({ where: { name: "default" } });
user!.roles_uid = roles[0]!.uid; const roleEntity = Role.hydrate<Role>(roles[0]!, { strategy: "excludeAll" });
await this.userService.update(appointment.user_uid, user!); userEntity.role = roleEntity;
await this.userService.update(appointment!.user_uid, userEntity);
return voteCreated; return voteCreated;
} else if (appointment.choice === EVote.NOMINATE) { } else if (appointment!.choice === EVote.NOMINATE) {
const roles = await this.roleService.get({ where: { name: "super-admin" } }); const roles = await this.roleService.get({ where: { name: "super-admin" } });
user!.roles_uid = roles[0]!.uid; const roleEntity = Role.hydrate<Role>(roles[0]!, { strategy: "excludeAll" });
await this.userService.update(appointment.user_uid, user!); userEntity!.role = roleEntity;
await this.userService.update(appointment!.user_uid, userEntity);
return voteCreated; return voteCreated;
} }
} }