add vote service

This commit is contained in:
OxSaitama 2023-07-13 13:31:15 +02:00
parent 14f07b4475
commit 59f1fe2758
15 changed files with 714 additions and 3 deletions

View File

@ -52,7 +52,7 @@
"cron": "^2.3.1",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.63",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.64",
"module-alias": "^2.2.2",
"multer": "^1.4.5-lts.1",
"next": "^13.1.5",

View File

@ -0,0 +1,103 @@
import { Response, Request } from "express";
import { Controller, Get, Post } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import AppointmentsService from "@Services/super-admin/AppointmentsService/AppointmentsService";
import { Service } from "typedi";
import { Appointment } from "le-coffre-resources/dist/SuperAdmin";
import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler";
@Controller()
@Service()
export default class AppointmentsController extends ApiController {
constructor(private appointmentsService: AppointmentsService) {
super();
}
/**
* @description Get all appointments
*/
@Get("/api/v1/super-admin/appointments", [authHandler])
protected async get(req: Request, response: Response) {
try {
//get query
let query;
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
//call service to get prisma entity
const appointmentsEntities = await this.appointmentsService.get(query);
//Hydrate ressource with prisma entity
const appointments = Appointment.hydrateArray<Appointment>(appointmentsEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, appointments);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Create a new appointment
*/
@Post("/api/v1/super-admin/appointments", [authHandler])
protected async post(req: Request, response: Response) {
try {
//init IUser resource with request body values
const appointmentEntity = Appointment.hydrate<Appointment>(req.body);
//validate user
await validateOrReject(appointmentEntity, { groups: ["createAppointment"]});
//call service to get prisma entity
const appointmentEntityCreated = await this.appointmentsService.create(appointmentEntity);
//Hydrate ressource with prisma entity
const appointment = Appointment.hydrate<Appointment>(appointmentEntityCreated, {
strategy: "excludeAll",
});
//success
this.httpCreated(response, appointment);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific appointment by uid
*/
@Get("/api/v1/super-admin/appointments/:uid", [authHandler])
protected async getOneByUid(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
let query;
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
const appointmentEntity = await this.appointmentsService.getByUid(uid, query);
if (!appointmentEntity) {
this.httpNotFoundRequest(response, "appointment not found");
return;
}
//Hydrate ressource with prisma entity
const appointment = Appointment.hydrate<Appointment>(appointmentEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, appointment);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}

View File

@ -90,12 +90,13 @@ export default class UsersController extends ApiController {
protected async put(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const userFound = await this.usersService.getByUid(uid);
const userFound = await this.usersService.getByUid(uid, {role: true, votes: true});
if (!userFound) {
this.httpNotFoundRequest(response, "user not found");
@ -104,12 +105,15 @@ export default class UsersController extends ApiController {
//init IUser resource with request body values
const userEntity = User.hydrate<User>(req.body);
const userFoundEntity = User.hydrate<User>(userFound, { strategy: "excludeAll" });
//validate user
await validateOrReject(userEntity, { groups: ["updateUser"] });
const userEntityToUpdate = this.voteService.vote(userEntity, userFoundEntity, userId);
//call service to get prisma entity
const userEntityUpdated = await this.usersService.update(uid, userEntity);
const userEntityUpdated = await this.usersService.update(uid, userFoundEntity);
//Hydrate ressource with prisma entity
const user = User.hydrate<User>(userEntityUpdated, {

View File

@ -0,0 +1,147 @@
import { Response, Request } from "express";
import { Controller, Delete, Get, Post } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import VotesService from "@Services/super-admin/VotesService/VotesService";
import { Service } from "typedi";
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";
@Controller()
@Service()
export default class VotesController extends ApiController {
constructor(private votesService: VotesService, private usersService: UsersService) {
super();
}
/**
* @description Get all votes
*/
@Get("/api/v1/super-admin/votes", [authHandler])
protected async get(req: Request, response: Response) {
try {
//get query
let query;
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
//call service to get prisma entity
const votesEntities = await this.votesService.get(query);
//Hydrate ressource with prisma entity
const votes = Vote.hydrateArray<Vote>(votesEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, votes);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Create a new vote
*/
@Post("/api/v1/super-admin/votes", [authHandler])
protected async post(req: Request, response: Response) {
try {
const userId = req.body.user.userId;
//init IUser resource with request body values
const voteEntity = Vote.hydrate<Vote>(req.body);
//validate user
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 voter = await this.usersService.getByUid(userId);
voteEntity.voter = voter!;
//call service to get prisma entity
const voteEntityCreated = await this.votesService.create(voteEntity);
//Hydrate ressource with prisma entity
const vote = Vote.hydrate<Vote>(voteEntityCreated, {
strategy: "excludeAll",
});
//success
this.httpCreated(response, vote);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific vote by uid
*/
@Get("/api/v1/super-admin/votes/:uid", [authHandler])
protected async getOneByUid(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
let query;
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
const voteEntity = await this.votesService.getByUid(uid, query);
if (!voteEntity) {
this.httpNotFoundRequest(response, "vote not found");
return;
}
//Hydrate ressource with prisma entity
const vote = Vote.hydrate<Vote>(voteEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, vote);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Delete a specific folder
*/
@Delete("/api/v1/super-admin/votes/:uid", [authHandler])
protected async delete(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const voteFound = await this.votesService.getByUid(uid);
if (!voteFound) {
this.httpNotFoundRequest(response, "vote not found");
return;
}
//call service to get prisma entity
const votetEntity: Votes = await this.votesService.delete(uid);
//Hydrate ressource with prisma entity
const vote = Vote.hydrate<Vote>(votetEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, vote);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}

View File

@ -40,6 +40,8 @@ import RolesControllerNotary from "./api/notary/RolesController";
import OfficeRolesControllerNotary from "./api/notary/OfficeRolesController";
import FilesControllerCustomer from "./api/customer/FilesController";
import DocumentsControllerCustomer from "./api/customer/DocumentsController";
import AppointmentsController from "./api/super-admin/AppointmentsController";
import VotesController from "./api/super-admin/VotesController";
/**
@ -56,6 +58,8 @@ export default {
Container.get(DeedTypesControllerSuperAdmin);
Container.get(DocumentsControllerSuperAdmin);
Container.get(DocumentTypesControllerSuperAdmin);
Container.get(AppointmentsController);
Container.get(VotesController);
Container.get(IdNotUserController);
Container.get(FranceConnectCustomerController);
Container.get(FilesControllerSuperAdmin);

View File

@ -0,0 +1,38 @@
-- DropForeignKey
ALTER TABLE "users" DROP CONSTRAINT "users_contact_uid_fkey";
-- DropForeignKey
ALTER TABLE "users" DROP CONSTRAINT "users_office_role_uid_fkey";
-- DropForeignKey
ALTER TABLE "users" DROP CONSTRAINT "users_office_uid_fkey";
-- DropForeignKey
ALTER TABLE "users" DROP CONSTRAINT "users_roles_uid_fkey";
-- CreateTable
CREATE TABLE "votes" (
"uid" TEXT NOT NULL,
"user_uid" VARCHAR(255) NOT NULL,
"voters" TEXT[],
CONSTRAINT "votes_pkey" PRIMARY KEY ("uid")
);
-- CreateIndex
CREATE UNIQUE INDEX "votes_uid_key" ON "votes"("uid");
-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_contact_uid_fkey" FOREIGN KEY ("contact_uid") REFERENCES "contacts"("uid") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_roles_uid_fkey" FOREIGN KEY ("roles_uid") REFERENCES "roles"("uid") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_office_role_uid_fkey" FOREIGN KEY ("office_role_uid") REFERENCES "office_roles"("uid") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_office_uid_fkey" FOREIGN KEY ("office_uid") REFERENCES "offices"("uid") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "votes" ADD CONSTRAINT "votes_user_uid_fkey" FOREIGN KEY ("user_uid") REFERENCES "users"("uid") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -0,0 +1,45 @@
/*
Warnings:
- You are about to drop the column `user_uid` on the `votes` table. All the data in the column will be lost.
- You are about to drop the column `voters` on the `votes` table. All the data in the column will be lost.
- Added the required column `appointment_uid` to the `votes` table without a default value. This is not possible if the table is not empty.
- Added the required column `voter_uid` to the `votes` table without a default value. This is not possible if the table is not empty.
*/
-- CreateEnum
CREATE TYPE "EVote" AS ENUM ('NOMINATE', 'DISMISS');
-- CreateEnum
CREATE TYPE "EAppointmentStatus" AS ENUM ('OPEN', 'CLOSED');
-- DropForeignKey
ALTER TABLE "votes" DROP CONSTRAINT "votes_user_uid_fkey";
-- AlterTable
ALTER TABLE "votes" DROP COLUMN "user_uid",
DROP COLUMN "voters",
ADD COLUMN "appointment_uid" VARCHAR(255) NOT NULL,
ADD COLUMN "choice" "EVote" NOT NULL DEFAULT 'NOMINATE',
ADD COLUMN "voter_uid" VARCHAR(255) NOT NULL;
-- CreateTable
CREATE TABLE "appointments" (
"uid" TEXT NOT NULL,
"user_uid" VARCHAR(255) NOT NULL,
"status" "EAppointmentStatus" NOT NULL DEFAULT 'OPEN',
CONSTRAINT "appointments_pkey" PRIMARY KEY ("uid")
);
-- CreateIndex
CREATE UNIQUE INDEX "appointments_uid_key" ON "appointments"("uid");
-- AddForeignKey
ALTER TABLE "appointments" ADD CONSTRAINT "appointments_user_uid_fkey" FOREIGN KEY ("user_uid") REFERENCES "users"("uid") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "votes" ADD CONSTRAINT "votes_appointment_uid_fkey" FOREIGN KEY ("appointment_uid") REFERENCES "appointments"("uid") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "votes" ADD CONSTRAINT "votes_voter_uid_fkey" FOREIGN KEY ("voter_uid") REFERENCES "users"("uid") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -0,0 +1,11 @@
/*
Warnings:
- You are about to drop the column `choice` on the `votes` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "appointments" ADD COLUMN "choice" "EVote" NOT NULL DEFAULT 'NOMINATE';
-- AlterTable
ALTER TABLE "votes" DROP COLUMN "choice";

View File

@ -65,6 +65,8 @@ model Users {
office_uid String @db.VarChar(255)
notifications Notifications[] @relation("UserHasNotifications")
office_folders OfficeFolders[] @relation("OfficeFolderHasStakeholders")
appointment Appointments[]
votes Votes[]
@@map("users")
}
@ -287,6 +289,27 @@ model Emails {
@@map("email")
}
model Appointments {
uid String @id @unique @default(uuid())
user Users @relation(fields: [user_uid], references: [uid], onDelete: Cascade)
user_uid String @db.VarChar(255)
choice EVote @default(NOMINATE)
status EAppointmentStatus @default(OPEN)
votes Votes[]
@@map("appointments")
}
model Votes {
uid String @id @unique @default(uuid())
appointment Appointments @relation(fields: [appointment_uid], references: [uid], onDelete: Cascade)
appointment_uid String @db.VarChar(255)
voter Users @relation(fields: [voter_uid], references: [uid])
voter_uid String @db.VarChar(255)
@@map("votes")
}
enum ECivility {
MALE
FEMALE
@ -321,3 +344,13 @@ enum EDocumentStatus {
ANCHORED
REFUSED
}
enum EVote {
NOMINATE
DISMISS
}
enum EAppointmentStatus {
OPEN
CLOSED
}

View File

@ -0,0 +1,95 @@
import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi";
import { Appointments, EAppointmentStatus, EVote, Prisma } from "@prisma/client";
import { Appointment } from "le-coffre-resources/dist/SuperAdmin";
@Service()
export default class AppointmentsRepository extends BaseRepository {
constructor(private database: Database) {
super();
}
protected get model() {
return this.database.getClient().appointments;
}
protected get instanceDb() {
return this.database.getClient();
}
/**
* @description : Find many appointments
*/
public async findMany(query: Prisma.AppointmentsFindManyArgs) {
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
return this.model.findMany(query);
}
/**
* @description : Create new appointment
*/
public async create(appointment: Appointment): Promise<Appointments> {
const createArgs: Prisma.AppointmentsCreateArgs = {
data: {
user: {
connect: {
uid: appointment.targeted_user!.uid,
},
},
choice: EVote[appointment.choice as keyof typeof EVote],
},
};
return this.model.create(createArgs);
}
/**
* @description : Update data of a appointment
*/
public async update(uid: string, status: EAppointmentStatus): Promise<Appointments> {
const updateArgs: Prisma.AppointmentsUpdateArgs = {
where: {
uid: uid,
},
data: {
status: status,
},
};
return this.model.update(updateArgs);
}
/**
* @description : Find one appointment
*/
public async findOneByUid(uid: string, query?: Prisma.AppointmentsInclude) {
return this.model.findUnique({
where: {
uid: uid,
},
include: query,
});
}
/**
* @description : Find one appointment with votes
*/
public async findOneByUidWithVotes(uid: string) {
return this.model.findUnique({
where: {
uid: uid,
},
include: {votes: true},
});
}
/**
* @description : delete a appointment
*/
public async delete(uid: string): Promise<Appointments> {
return this.model.delete({
where: {
uid: uid,
},
});
}
}

View File

@ -196,6 +196,18 @@ export default class UsersRepository extends BaseRepository {
});
}
/**
* @description : Find one user with office
*/
public async findOneByUidWithRole(uid: string) {
return this.model.findUnique({
where: {
uid: uid,
},
include: { role: true },
});
}
/**
* @description : Find one user
*/

View File

@ -0,0 +1,71 @@
import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi";
import { Votes, Prisma } 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<Votes> {
const createArgs: Prisma.VotesCreateArgs = {
data: {
appointment: {
connect: {
uid: vote.appointment.uid,
},
},
voter: {
connect: {
uid: vote.voter.uid,
},
},
},
};
return this.model.create(createArgs);
}
/**
* @description : Find one vote
*/
public async findOneByUid(uid: string, query?: Prisma.VotesInclude): Promise<Votes | null> {
return this.model.findUnique({
where: {
uid: uid,
},
include: query,
});
}
/**
* @description : delete a vote
*/
public async delete(uid: string): Promise<Votes> {
return this.model.delete({
where: {
uid: uid,
},
});
}
}

View File

@ -0,0 +1,67 @@
import BaseService from "@Services/BaseService";
import { Service } from "typedi";
import { Appointment } from "le-coffre-resources/dist/SuperAdmin";
import AppointmentsRepository from "@Repositories/AppointmentsRepository";
import { Prisma, Appointments, EAppointmentStatus } from "@prisma/client";
import UsersService from "../UsersService/UsersService";
import { EVote } from "le-coffre-resources/dist/SuperAdmin/Appointment";
@Service()
export default class AppointmentService extends BaseService {
constructor(private appointmentRepository: AppointmentsRepository, private userService: UsersService) {
super();
}
/**
* @description : Get all appointments
* @throws {Error} If appointments cannot be get
*/
public get(query: Prisma.AppointmentsFindManyArgs) {
return this.appointmentRepository.findMany(query);
}
/**
* @description : Create a appointment
* @throws {Error} If appointment couldn't be created
*/
public async create(appointment: Appointment): Promise<Appointments> {
const user = await this.userService.getByUidWithRole(appointment.targeted_user!.uid!)
if(!user) throw new Error("User not found");
user.role.name === "super-admin" ? appointment.choice = EVote.DISMISS : appointment.choice = EVote.NOMINATE;
return this.appointmentRepository.create(appointment);
}
/**
* @description : Modify a appointment
* @throws {Error} If appointment cannot be modified
*/
public async update(uid: string, status: EAppointmentStatus): Promise<Appointments> {
return this.appointmentRepository.update(uid, status);
}
/**
* @description : Get a appointment by uid
* @throws {Error} If appointment cannot be get by uid
*/
public getByUid(uid: string, query?: Prisma.AppointmentsInclude): Promise<Appointments | null> {
return this.appointmentRepository.findOneByUid(uid, query);
}
/**
* @description : Get a appointment by uid
* @throws {Error} If appointment cannot be get by uid
*/
public getByUidWithVotes(uid: string) {
return this.appointmentRepository.findOneByUidWithVotes(uid);
}
/**
* @description : delete a appointment by uid
* @throws {Error} If appointment cannot be get by uid
*/
public delete(uid: string) {
return this.appointmentRepository.delete(uid);
}
}

View File

@ -51,6 +51,14 @@ export default class UsersService extends BaseService {
return this.userRepository.findOneByUidWithOffice(uid);
}
/**
* @description : Get a user by uid with role
* @throws {Error} If user cannot be get by uid
*/
public getByUidWithRole(uid: string) {
return this.userRepository.findOneByUidWithRole(uid);
}
/**
* @description : Get a user by uid
* @throws {Error} If user cannot be get by uid

View File

@ -0,0 +1,73 @@
import BaseService from "@Services/BaseService";
import { Service } from "typedi";
import { 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";
import UsersService from "../UsersService/UsersService";
import RolesService from "../RolesService/RolesService";
@Service()
export default class VoteService extends BaseService {
constructor(
private voteRepository: VotesRepository,
private appointmentService: AppointmentService,
private userService: UsersService,
private roleService: RolesService,
) {
super();
}
/**
* @description : Get all votes
* @throws {Error} If votes cannot be get
*/
public get(query: Prisma.VotesFindManyArgs) {
return this.voteRepository.findMany(query);
}
/**
* @description : Create a vote
* @throws {Error} If vote couldn't be created
*/
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) {
const voteCreated = await this.voteRepository.create(vote);
await this.appointmentService.update(appointment.uid!, EAppointmentStatus.CLOSED);
const user = await this.userService.getByUid(appointment.user_uid);
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!);
return voteCreated;
} 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!);
return voteCreated;
}
}
return this.voteRepository.create(vote);
}
/**
* @description : Get a vote by uid
* @throws {Error} If vote cannot be get by uid
*/
public getByUid(uid: string, query?: Prisma.VotesInclude) {
return this.voteRepository.findOneByUid(uid, query);
}
/**
* @description : delete a vote by uid
* @throws {Error} If vote cannot be get by uid
*/
public delete(uid: string) {
return this.voteRepository.delete(uid);
}
}