add error handling
This commit is contained in:
parent
0a675027ca
commit
e9f3fb7e9c
@ -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
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,14 +53,29 @@ export default class VotesController extends ApiController {
|
|||||||
//init IUser resource with request body values
|
//init IUser resource with request body values
|
||||||
const voteEntity = Vote.hydrate<Vote>(req.body);
|
const voteEntity = Vote.hydrate<Vote>(req.body);
|
||||||
//validate user
|
//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);
|
const voter = await this.usersService.getByUid(userId);
|
||||||
|
|
||||||
voteEntity.voter = voter!;
|
voteEntity.voter = voter!;
|
||||||
//call service to get prisma entity
|
//call service to get prisma entity
|
||||||
const voteEntityCreated = await this.votesService.create(voteEntity);
|
const voteEntityCreated = await this.votesService.create(voteEntity);
|
||||||
@ -92,7 +108,7 @@ export default class VotesController extends ApiController {
|
|||||||
if (req.query["q"]) {
|
if (req.query["q"]) {
|
||||||
query = JSON.parse(req.query["q"] as string);
|
query = JSON.parse(req.query["q"] as string);
|
||||||
}
|
}
|
||||||
|
|
||||||
const voteEntity = await this.votesService.getByUid(uid, query);
|
const voteEntity = await this.votesService.getByUid(uid, query);
|
||||||
|
|
||||||
if (!voteEntity) {
|
if (!voteEntity) {
|
||||||
@ -143,5 +159,4 @@ export default class VotesController extends ApiController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user