import Database from "@Common/databases/database"; import BaseRepository from "@Repositories/BaseRepository"; import { Service } from "typedi"; import { Votes, Prisma, EVote, EAppointmentStatus } from "@prisma/client"; import { Vote } from "le-coffre-resources/dist/SuperAdmin"; @Service() export default class VotesRepository extends BaseRepository { constructor(private database: Database) { super(); } protected get model() { return this.database.getClient().votes; } protected get instanceDb() { return this.database.getClient(); } /** * @description : Find many votes */ public async findMany(query: Prisma.VotesFindManyArgs) { query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); return this.model.findMany(query); } /** * @description : Create new vote */ public async create(vote: Vote): Promise { let whereArg: Prisma.AppointmentsWhereUniqueInput; if(vote.appointment.targeted_user.uid) { whereArg = { user_uid_choice_status: { user_uid: vote.appointment.targeted_user.uid, choice: EVote[vote.appointment.choice as keyof typeof EVote], status: EAppointmentStatus.OPEN, } }; } else { whereArg = { uid: vote.appointment.uid, }; } const createArgs: Prisma.VotesCreateArgs = { data: { appointment: { connectOrCreate: { where: whereArg, create: { user_uid: vote.appointment.targeted_user.uid!, } }, }, voter: { connect: { uid: vote.voter.uid, }, }, } }; return this.model.create({...createArgs, include: {appointment: {include: {votes: true}}}}); } /** * @description : Find one vote */ public async findOneByUid(uid: string, query?: Prisma.VotesInclude): Promise { return this.model.findUnique({ where: { uid: uid, }, include: query, }); } /** * @description : delete a vote */ public async delete(uid: string): Promise { return this.model.delete({ where: { uid: uid, }, }); } }