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 { 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

View File

@ -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,11 +53,26 @@ export default class VotesController extends ApiController {
//init IUser resource with request body values
const voteEntity = Vote.hydrate<Vote>(req.body);
//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}}]}});
console.log(votes)
if (votes.length) throw new Error("Voter already voted for this appointment");
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 voter = await this.usersService.getByUid(userId);
@ -143,5 +159,4 @@ export default class VotesController extends ApiController {
return;
}
}
}

View File

@ -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<Votes> {
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>(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<Role>(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<Role>(roles[0]!, { strategy: "excludeAll" });
userEntity!.role = roleEntity;
await this.userService.update(appointment!.user_uid, userEntity);
return voteCreated;
}
}