Merge Dev in Staging

This commit is contained in:
Arnaud D. Natali 2023-10-04 21:03:45 +02:00 committed by GitHub
commit 451e54ece5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 209 additions and 62 deletions

View File

@ -53,7 +53,7 @@
"express": "^4.18.2", "express": "^4.18.2",
"fp-ts": "^2.16.1", "fp-ts": "^2.16.1",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.89", "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.90",
"module-alias": "^2.2.2", "module-alias": "^2.2.2",
"monocle-ts": "^2.3.13", "monocle-ts": "^2.3.13",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",

View File

@ -2,7 +2,7 @@ import { Response, Request } from "express";
import { Controller, Post } from "@ControllerPattern/index"; import { Controller, Post } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController"; import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi"; import { Service } from "typedi";
import Id360Service from "@Services/common/Id360Service/Id360Service"; import Id360Service, { EnrollmentResponse } from "@Services/common/Id360Service/Id360Service";
import CustomersService from "@Services/customer/CustomersService/CustomersService"; import CustomersService from "@Services/customer/CustomersService/CustomersService";
import AuthService from "@Services/common/AuthService/AuthService"; import AuthService from "@Services/common/AuthService/AuthService";
import { Customer } from "le-coffre-resources/dist/SuperAdmin"; import { Customer } from "le-coffre-resources/dist/SuperAdmin";
@ -34,7 +34,11 @@ export default class CustomerController extends ApiController {
return; return;
} }
try { try {
const enrollment = await this.id360Service.getEnrollment(callbackToken); const res = await this.id360Service.getEnrollment(callbackToken);
const enrollment = await res.json() as EnrollmentResponse;
if(enrollment.status === "STARTED") {
this.loginCallback(req, response);
}
if (enrollment.status !== "OK") { if (enrollment.status !== "OK") {
this.httpUnauthorized(response, "Enrollment status is not OK"); this.httpUnauthorized(response, "Enrollment status is not OK");
return; return;

View File

@ -20,7 +20,6 @@ export default class DocumentController extends ApiController {
protected async getDocumentVerificationFromId360(req: Request, response: Response) { protected async getDocumentVerificationFromId360(req: Request, response: Response) {
try { try {
console.log("document callback", req, response)
this.httpSuccess(response); this.httpSuccess(response);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
@ -33,7 +32,6 @@ export default class DocumentController extends ApiController {
protected async getCustomerVerificationFromId360(req: Request, response: Response) { protected async getCustomerVerificationFromId360(req: Request, response: Response) {
try { try {
console.log("customer callback", req, response)
this.httpSuccess(response); this.httpSuccess(response);
} catch (error) { } catch (error) {
console.log(error); console.log(error);

View File

@ -25,6 +25,10 @@ export default class UserController extends ApiController {
if (!code) throw new Error("code is required"); if (!code) throw new Error("code is required");
const idNotToken = await this.idNotService.getIdNotToken(code); const idNotToken = await this.idNotService.getIdNotToken(code);
if(!idNotToken) {
this.httpValidationError(response, "IdNot token undefined");
return;
}
const user = await this.idNotService.getOrCreateUser(idNotToken); const user = await this.idNotService.getOrCreateUser(idNotToken);
if(!user) { if(!user) {

View File

@ -4,18 +4,23 @@ import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi"; import { Service } from "typedi";
import DocumentsService from "@Services/notary/DocumentsService/DocumentsService"; import DocumentsService from "@Services/notary/DocumentsService/DocumentsService";
import { Documents, Prisma } from "@prisma/client"; import { Documents, Prisma } from "@prisma/client";
import { Document } from "le-coffre-resources/dist/Notary"; import { Document, OfficeFolder } from "le-coffre-resources/dist/Notary";
import { validateOrReject } from "class-validator"; import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler"; import ruleHandler from "@App/middlewares/RulesHandler";
import documentHandler from "@App/middlewares/OfficeMembershipHandlers/DocumentHandler"; import documentHandler from "@App/middlewares/OfficeMembershipHandlers/DocumentHandler";
import EmailBuilder from "@Common/emails/EmailBuilder"; import EmailBuilder from "@Common/emails/EmailBuilder";
import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService";
// import NotificationBuilder from "@Common/notifications/NotificationBuilder"; // import NotificationBuilder from "@Common/notifications/NotificationBuilder";
@Controller() @Controller()
@Service() @Service()
export default class DocumentsController extends ApiController { export default class DocumentsController extends ApiController {
constructor(private documentsService: DocumentsService, private emailBuilder: EmailBuilder) { constructor(
private documentsService: DocumentsService,
private emailBuilder: EmailBuilder,
private officeFoldersService: OfficeFoldersService,
) {
super(); super();
} }
@ -32,8 +37,8 @@ export default class DocumentsController extends ApiController {
query = JSON.parse(req.query["q"] as string); query = JSON.parse(req.query["q"] as string);
} }
const officeId: string = req.body.user.office_Id; const officeId: string = req.body.user.office_Id;
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ; const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId };
if(!query.where) query.where = { document_type : {office: officeWhereInput}}; if (!query.where) query.where = { document_type: { office: officeWhereInput } };
query.where.document_type!.office = officeWhereInput; query.where.document_type!.office = officeWhereInput;
//call service to get prisma entity //call service to get prisma entity
@ -60,6 +65,20 @@ export default class DocumentsController extends ApiController {
//init Document resource with request body values //init Document resource with request body values
const documentEntity = Document.hydrate<Document>(req.body); const documentEntity = Document.hydrate<Document>(req.body);
const folder = await this.officeFoldersService.getByUid(documentEntity.folder?.uid!, {
folder_anchor: true,
});
if (!folder) {
this.httpBadRequest(response, "Folder not found");
return;
}
const folderRessource = OfficeFolder.hydrate<OfficeFolder>(folder);
if (folderRessource.folder_anchor) {
this.httpBadRequest(response, "Cannot ask document on an anchored or anchoring folder");
return;
}
//validate document //validate document
await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false });
@ -104,8 +123,6 @@ export default class DocumentsController extends ApiController {
//init Document resource with request body values //init Document resource with request body values
const documentEntity = Document.hydrate<Document>(req.body); const documentEntity = Document.hydrate<Document>(req.body);
//validate document //validate document
await validateOrReject(documentEntity, { groups: ["updateDocument"] }); await validateOrReject(documentEntity, { groups: ["updateDocument"] });

View File

@ -44,9 +44,9 @@ export default class OfficeFoldersController extends ApiController {
customers: { customers: {
some: { some: {
OR: [ OR: [
{contact: { first_name: { contains: filter, mode: "insensitive" } }}, { contact: { first_name: { contains: filter, mode: "insensitive" } } },
{contact: { last_name: { contains: filter, mode: "insensitive" } }}, { contact: { last_name: { contains: filter, mode: "insensitive" } } },
] ],
}, },
}, },
}, },
@ -56,8 +56,8 @@ export default class OfficeFoldersController extends ApiController {
} }
const userId: string = req.body.user.userId; const userId: string = req.body.user.userId;
if(query.where?.stakeholders) delete query.where.stakeholders; if (query.where?.stakeholders) delete query.where.stakeholders;
const officeFoldersWhereInput: Prisma.OfficeFoldersWhereInput = { ...query.where, stakeholders: {some: {uid: userId }}}; const officeFoldersWhereInput: Prisma.OfficeFoldersWhereInput = { ...query.where, stakeholders: { some: { uid: userId } } };
query.where = officeFoldersWhereInput; query.where = officeFoldersWhereInput;
//call service to get prisma entity //call service to get prisma entity
@ -111,7 +111,9 @@ export default class OfficeFoldersController extends ApiController {
return; return;
} }
const officeFolderFound = await this.officeFoldersService.getByUid(uid); const officeFolderFound = await this.officeFoldersService.getByUid(uid, {
folder_anchor: true,
});
if (!officeFolderFound) { if (!officeFolderFound) {
this.httpNotFoundRequest(response, "office folder not found"); this.httpNotFoundRequest(response, "office folder not found");
@ -119,13 +121,17 @@ export default class OfficeFoldersController extends ApiController {
} }
//init OfficeFolder resource with request body values //init OfficeFolder resource with request body values
const officeFolderEntity = OfficeFolder.hydrate<OfficeFolder>(req.body); const officefolderToUpdate = OfficeFolder.hydrate<OfficeFolder>(req.body);
const officeFolderFoundRessource = OfficeFolder.hydrate<OfficeFolder>(officeFolderFound);
//validate folder //validate folder
await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false }); await validateOrReject(officefolderToUpdate, { groups: ["updateFolder"], forbidUnknownValues: false });
if (officeFolderFoundRessource.folder_anchor?.status === "VERIFIED_ON_CHAIN") {
this.httpBadRequest(response, "Cannot update a verified folder");
return;
}
//call service to get prisma entity //call service to get prisma entity
const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officefolderToUpdate);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntityUpdated, { const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntityUpdated, {
@ -140,6 +146,102 @@ export default class OfficeFoldersController extends ApiController {
} }
} }
/**
* @description Modify a specific folder by uid
*/
@Put("/api/v1/notary/folders/:uid/archive", [authHandler, ruleHandler, folderHandler])
protected async archive(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const officeFolderFound = await this.officeFoldersService.getByUid(uid, {
folder_anchor: true,
});
if (!officeFolderFound) {
this.httpNotFoundRequest(response, "office folder not found");
return;
}
//init OfficeFolder resource with request body values
const officefolderToUpdate = OfficeFolder.hydrate<OfficeFolder>(officeFolderFound);
officefolderToUpdate.status = "ARCHIVED";
officefolderToUpdate.archived_description = req.body.archived_description ?? "";
//validate folder
await validateOrReject(officefolderToUpdate, { groups: ["updateFolder"], forbidUnknownValues: false });
if ((officeFolderFound as any).folder_anchor?.status !== "VERIFIED_ON_CHAIN") {
this.httpBadRequest(response, "Cannot archive a not anchored folder");
return;
}
//call service to get prisma entity
const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officefolderToUpdate);
//Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntityUpdated, {
strategy: "excludeAll",
});
//success
this.httpSuccess(response, officeFolders);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Modify a specific folder by uid
*/
@Put("/api/v1/notary/folders/:uid/restore", [authHandler, ruleHandler, folderHandler])
protected async restore(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const officeFolderFound = await this.officeFoldersService.getByUid(uid, {
folder_anchor: true,
});
if (!officeFolderFound) {
this.httpNotFoundRequest(response, "office folder not found");
return;
}
//init OfficeFolder resource with request body values
const officefolderToUpdate = OfficeFolder.hydrate<OfficeFolder>(officeFolderFound);
officefolderToUpdate.status = "LIVE";
officefolderToUpdate.archived_description = "";
//validate folder
await validateOrReject(officefolderToUpdate, { groups: ["updateFolder"], forbidUnknownValues: false });
//call service to get prisma entity
const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officefolderToUpdate);
//Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntityUpdated, {
strategy: "excludeAll",
});
//success
this.httpSuccess(response, officeFolders);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/** /**
* @description Get a specific folder by uid * @description Get a specific folder by uid
* @returns IFolder * @returns IFolder

View File

@ -30,7 +30,7 @@ export default class LiveVoteController extends ApiController {
try { try {
const userId = req.body.user.userId; const userId = req.body.user.userId;
//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, { strategy: "excludeAll" });
//validate user //validate user
await validateOrReject(voteEntity, { groups: ["createVote"] }); await validateOrReject(voteEntity, { groups: ["createVote"] });
@ -54,7 +54,7 @@ export default class LiveVoteController extends ApiController {
AND: [ AND: [
{ {
appointment: { appointment: {
AND: [{ user_uid: voteEntity.appointment.targeted_user.uid }, { status: EAppointmentStatus.OPEN }], AND: [{ user_uid: voteEntity.appointment.user.uid }, { status: EAppointmentStatus.OPEN }],
}, },
}, },
{ voter: { uid: userId } }, { voter: { uid: userId } },
@ -79,9 +79,7 @@ export default class LiveVoteController extends ApiController {
return; return;
} }
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const vote = Vote.hydrate<Vote>(voteEntityCreated, { const vote = Vote.hydrate<Vote>(voteEntityCreated, { strategy: "excludeAll" });
strategy: "excludeAll",
});
await this.notificationBuilder.sendVoteNotification(vote); await this.notificationBuilder.sendVoteNotification(vote);

View File

@ -17,6 +17,11 @@ export default class EmailBuilder {
const documentPrisma = await this.documentsService.getByUid(documentEntity.uid, { depositor: {include: {contact: true}}, folder:{include:{ office: true}} }); const documentPrisma = await this.documentsService.getByUid(documentEntity.uid, { depositor: {include: {contact: true}}, folder:{include:{ office: true}} });
if(!documentPrisma) throw new Error("Document not found"); if(!documentPrisma) throw new Error("Document not found");
const document = Document.hydrate<Document>(documentPrisma); const document = Document.hydrate<Document>(documentPrisma);
//Use mailchimpService.get get if an email was sent to the user in the lst hour
const lastEmail = await this.mailchimpService.get({ where: { to: document.depositor!.contact!.email, sentAt: { gte: new Date(Date.now() - 3600000) } } });
if(lastEmail.length > 0) return;
const to = document.depositor!.contact!.email; const to = document.depositor!.contact!.email;
const civility = this.getCivility(document.depositor!.contact!.civility); const civility = this.getCivility(document.depositor!.contact!.civility);
const templateVariables = { const templateVariables = {

View File

@ -14,7 +14,7 @@ export default class NotificationBuilder {
private documentsService: DocumentsService, private documentsService: DocumentsService,
private usersService: UsersService, private usersService: UsersService,
private foldersService: OfficeFoldersService, private foldersService: OfficeFoldersService,
private backendVariables: BackendVariables private backendVariables: BackendVariables,
) {} ) {}
public async sendDocumentDepositedNotification(documentEntity: Documents) { public async sendDocumentDepositedNotification(documentEntity: Documents) {
@ -42,10 +42,12 @@ export default class NotificationBuilder {
} }
public async sendFolderAnchoredNotification(folderEntity: OfficeFolders) { public async sendFolderAnchoredNotification(folderEntity: OfficeFolders) {
if(!folderEntity.uid) return; if (!folderEntity.uid) return;
const officeFolderPrisma = await this.foldersService.getByUid(folderEntity.uid, const officeFolderPrisma = await this.foldersService.getByUid(folderEntity.uid, {
{ folder_anchor: true, office: true, stakeholders: true } folder_anchor: true,
); office: true,
stakeholders: true,
});
if (!officeFolderPrisma) throw new Error("Folder not found"); if (!officeFolderPrisma) throw new Error("Folder not found");
const folder = OfficeFolder.hydrate<OfficeFolder>(officeFolderPrisma); const folder = OfficeFolder.hydrate<OfficeFolder>(officeFolderPrisma);
if (folder.folder_anchor?.status !== "VERIFIED_ON_CHAIN") return; if (folder.folder_anchor?.status !== "VERIFIED_ON_CHAIN") return;
@ -66,23 +68,19 @@ export default class NotificationBuilder {
public async sendVoteNotification(vote: Vote) { public async sendVoteNotification(vote: Vote) {
if (vote.appointment.status !== "OPEN") return; if (vote.appointment.status !== "OPEN") return;
const superAdminList = await this.usersService.get({ where: { role: { label: "super-admin" } } }); const superAdminList = await this.usersService.get({ where: { role: { name: "super-admin" } } });
const userTargeted = await this.usersService.getByUid(vote.appointment.user.uid!, { contact: true });
const userTargetedEntity = User.hydrate<User>(userTargeted!, { strategy: "excludeAll" });
let message = ""; let message = "";
if (vote.appointment.choice === "NOMINATE") { if (vote.appointment.choice === "NOMINATE") {
message = message = `Un collaborateur souhaite attribuer le titre de Super Administrateur à ${userTargetedEntity.contact?.first_name} ${userTargetedEntity.contact?.last_name}. Cliquez ici pour voter.`;
"Un collaborateur souhaite attribuer le titre de Super Administrateur à " +
vote.appointment.targeted_user +
". Cliquez ici pour voter.";
} else if (vote.appointment.choice === "DISMISS") { } else if (vote.appointment.choice === "DISMISS") {
message = message = `Un collaborateur souhaite retirer le titre de Super Administrateur à ${userTargetedEntity.contact?.first_name} ${userTargetedEntity.contact?.last_name}. Cliquez ici pour voter.`;
"Un collaborateur souhaite retirer le titre de Super Administrateur à " +
vote.appointment.targeted_user +
". Cliquez ici pour voter.";
} }
this.notificationsService.create({ this.notificationsService.create({
message: message, message: message,
redirection_url: `${this.backendVariables.APP_HOST}/users/${vote.appointment.targeted_user.uid}`, redirection_url: `${this.backendVariables.APP_HOST}/users/${vote.appointment.user.uid}`,
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
user: superAdminList || [], user: superAdminList || [],

View File

@ -1,7 +1,7 @@
import Database from "@Common/databases/database"; import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository"; import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi"; import { Service } from "typedi";
import { Appointments, EAppointmentStatus, Prisma } from "@prisma/client"; import { Appointments, EAppointmentStatus, EVote, Prisma, Users, Votes } from "@prisma/client";
@Service() @Service()
export default class AppointmentsRepository extends BaseRepository { export default class AppointmentsRepository extends BaseRepository {
@ -39,6 +39,18 @@ export default class AppointmentsRepository extends BaseRepository {
return this.model.update(updateArgs); return this.model.update(updateArgs);
} }
public async findOneByStatusUserAndChoice(userUid: string, choice: EVote, status: EAppointmentStatus): Promise<Appointments | null> {
return this.model.findUnique({
where: {
user_uid_choice_status: {
user_uid: userUid,
choice: choice,
status: status,
},
},
});
}
/** /**
* @description : Find one appointment * @description : Find one appointment
*/ */
@ -54,12 +66,12 @@ export default class AppointmentsRepository extends BaseRepository {
/** /**
* @description : Find one appointment with votes * @description : Find one appointment with votes
*/ */
public async findOneByUidWithVotes(uid: string) { public async findOneByUidWithVotes(uid: string): Promise<(Appointments & {votes: Votes[], user: Users}) | null> {
return this.model.findUnique({ return this.model.findUnique({
where: { where: {
uid: uid, uid: uid,
}, },
include: {votes: true}, include: {votes: true, user: true},
}); });
} }

View File

@ -29,10 +29,10 @@ export default class VotesRepository extends BaseRepository {
*/ */
public async create(vote: Vote): Promise<Votes> { public async create(vote: Vote): Promise<Votes> {
let whereArg: Prisma.AppointmentsWhereUniqueInput; let whereArg: Prisma.AppointmentsWhereUniqueInput;
if(vote.appointment.targeted_user.uid) { if(vote.appointment.user.uid) {
whereArg = { whereArg = {
user_uid_choice_status: { user_uid_choice_status: {
user_uid: vote.appointment.targeted_user.uid, user_uid: vote.appointment.user.uid,
choice: EVote[vote.appointment.choice as keyof typeof EVote], choice: EVote[vote.appointment.choice as keyof typeof EVote],
status: EAppointmentStatus.OPEN, status: EAppointmentStatus.OPEN,
} }
@ -49,7 +49,7 @@ export default class VotesRepository extends BaseRepository {
where: whereArg, where: whereArg,
create: { create: {
choice: EVote[vote.appointment.choice as keyof typeof EVote], choice: EVote[vote.appointment.choice as keyof typeof EVote],
user_uid: vote.appointment.targeted_user.uid!, user_uid: vote.appointment.user.uid!,
} }
}, },
}, },
@ -61,7 +61,7 @@ export default class VotesRepository extends BaseRepository {
} }
}; };
return this.model.create({...createArgs, include: {appointment: {include: {votes: true}}}}); return this.model.create({...createArgs, include: {appointment: {include: {votes: true, user: true}}}});
} }
/** /**

View File

@ -4,7 +4,7 @@ import { BackendVariables } from "@Common/config/variables/Variables";
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
import FilesService from "../FilesService/FilesService"; import FilesService from "../FilesService/FilesService";
type EnrollmentResponse = { export type EnrollmentResponse = {
url: string; url: string;
id: number; id: number;
api_key: string; api_key: string;
@ -196,13 +196,12 @@ export default class Id360Service extends BaseService {
} }
public async getEnrollment(token: string) { public async getEnrollment(token: string) {
const res = await fetch( return await fetch(
`${ `${
this.variables.DOCAPOST_BASE_URL + this.variables.DOCAPOST_ROOT + this.variables.DOCAPOST_VERSION this.variables.DOCAPOST_BASE_URL + this.variables.DOCAPOST_ROOT + this.variables.DOCAPOST_VERSION
}/enrollment/status/${token}`, }/enrollment/status/${token}`,
{ method: "GET" }, { method: "GET" },
); );
return (await res.json()) as EnrollmentResponse;
} }
public async finalizeEnrollment(apiKey: string) { public async finalizeEnrollment(apiKey: string) {

View File

@ -1,6 +1,6 @@
import EmailRepository from "@Repositories/EmailRepository"; import EmailRepository from "@Repositories/EmailRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { Emails } from "@prisma/client"; import { Emails, Prisma } from "@prisma/client";
import { Service } from "typedi"; import { Service } from "typedi";
import MailchimpClient from "@mailchimp/mailchimp_transactional"; import MailchimpClient from "@mailchimp/mailchimp_transactional";
import { BackendVariables } from "@Common/config/variables/Variables"; import { BackendVariables } from "@Common/config/variables/Variables";
@ -24,7 +24,7 @@ export default class MailchimpService extends BaseService {
* @description : Get all emails * @description : Get all emails
* @throws {Error} If emails cannot be get * @throws {Error} If emails cannot be get
*/ */
public async get(query: any): Promise<Emails[]> { public async get(query: Prisma.EmailsFindManyArgs): Promise<Emails[]> {
return this.emailRepository.findMany(query); return this.emailRepository.findMany(query);
} }

View File

@ -2,10 +2,11 @@ import BaseService from "@Services/BaseService";
import { Service } from "typedi"; import { Service } from "typedi";
import User, { Appointment, Role, Vote } from "le-coffre-resources/dist/SuperAdmin"; import User, { Appointment, Role, Vote } from "le-coffre-resources/dist/SuperAdmin";
import VotesRepository from "@Repositories/VotesRepository"; import VotesRepository from "@Repositories/VotesRepository";
import { Appointments, EAppointmentStatus, EVote, Prisma, Votes } from "@prisma/client"; import { Appointments, EAppointmentStatus, EVote, Prisma, Users, Votes } from "@prisma/client";
import UsersService from "../UsersService/UsersService"; import UsersService from "../UsersService/UsersService";
import RolesService from "../RolesService/RolesService"; import RolesService from "../RolesService/RolesService";
import AppointmentsRepository from "@Repositories/AppointmentsRepository"; import AppointmentsRepository from "@Repositories/AppointmentsRepository";
import NotificationBuilder from "@Common/notifications/NotificationBuilder";
@Service() @Service()
export default class LiveVoteService extends BaseService { export default class LiveVoteService extends BaseService {
@ -14,12 +15,13 @@ export default class LiveVoteService extends BaseService {
private appointmentRepository: AppointmentsRepository, private appointmentRepository: AppointmentsRepository,
private userService: UsersService, private userService: UsersService,
private roleService: RolesService, private roleService: RolesService,
private notificationBuilder: NotificationBuilder,
) { ) {
super(); super();
} }
public async verifyVoterChoice(vote: Vote): Promise<boolean> { public async verifyVoterChoice(vote: Vote): Promise<boolean> {
const userWithRole = await this.userService.getByUidWithRole(vote.appointment.targeted_user.uid!); const userWithRole = await this.userService.getByUidWithRole(vote.appointment.user.uid!);
if (userWithRole!.role.name === "super-admin" && vote.appointment.choice === EVote.DISMISS) { if (userWithRole!.role.name === "super-admin" && vote.appointment.choice === EVote.DISMISS) {
return true; return true;
} }
@ -65,27 +67,31 @@ export default class LiveVoteService extends BaseService {
return this.appointmentRepository.findOneByUid(uid, query); return this.appointmentRepository.findOneByUid(uid, query);
} }
public async getAppointmentWithVotes(vote: Vote): Promise<Appointments | null> { public async getAppointmentWithVotes(vote: Vote): Promise<(Appointments & {votes: Votes[], user: Users}) | null> {
if (vote.appointment.uid) { if (vote.appointment.uid) {
return this.appointmentRepository.findOneByUid(vote.appointment.uid); return this.appointmentRepository.findOneByUidWithVotes(vote.appointment.uid);
} }
const appointmentByUser = await this.appointmentRepository.findMany({ const appointmentByUser = await this.appointmentRepository.findMany({
where: { where: {
AND: [ AND: [
{ user_uid: vote.appointment.targeted_user.uid }, { user_uid: vote.appointment.user.uid },
{ status: EAppointmentStatus.OPEN }, { status: EAppointmentStatus.OPEN },
{ choice: EVote[vote.appointment.choice as keyof typeof EVote] }, { choice: EVote[vote.appointment.choice as keyof typeof EVote] },
], ],
}, },
include: { votes: true }, include: { votes: true, user: true },
}); });
if (appointmentByUser.length >= 1) { if (appointmentByUser.length != 0) {
return this.appointmentRepository.findOneByUidWithVotes(appointmentByUser[0]!.uid); return this.appointmentRepository.findOneByUidWithVotes(appointmentByUser[0]!.uid);
} }
return null; return null;
} }
private async closeVote(appointment: Appointments, vote: Votes) { private async closeVote(appointment: Appointments, vote: Votes) {
const apointmentFound = await this.appointmentRepository.findOneByStatusUserAndChoice(appointment.user_uid, EVote[appointment.choice as keyof typeof EVote], EAppointmentStatus.CLOSED);
if(apointmentFound) {
await this.appointmentRepository.delete(apointmentFound.uid);
}
await this.appointmentRepository.update(vote.appointment_uid, EAppointmentStatus.CLOSED); await this.appointmentRepository.update(vote.appointment_uid, EAppointmentStatus.CLOSED);
const user = await this.userService.getByUid(appointment.user_uid, { role: true }); const user = await this.userService.getByUid(appointment.user_uid, { role: true });
const userEntity = User.hydrate<User>(user!, { strategy: "excludeAll" }); const userEntity = User.hydrate<User>(user!, { strategy: "excludeAll" });
@ -98,12 +104,14 @@ export default class LiveVoteService extends BaseService {
const roles = await this.roleService.get({ where: { name: "default" } }); const roles = await this.roleService.get({ where: { name: "default" } });
const roleEntity = Role.hydrate<Role>(roles[0]!, { strategy: "excludeAll" }); const roleEntity = Role.hydrate<Role>(roles[0]!, { strategy: "excludeAll" });
userEntity.role = roleEntity; userEntity.role = roleEntity;
await this.notificationBuilder.sendDismissNotification(userEntity);
await this.userService.update(appointment!.user_uid, userEntity); await this.userService.update(appointment!.user_uid, userEntity);
return vote; return vote;
} 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" } });
const roleEntity = Role.hydrate<Role>(roles[0]!, { strategy: "excludeAll" }); const roleEntity = Role.hydrate<Role>(roles[0]!, { strategy: "excludeAll" });
userEntity!.role = roleEntity; userEntity!.role = roleEntity;
await this.notificationBuilder.sendNominateNotification(userEntity);
await this.userService.update(appointment!.user_uid, userEntity); await this.userService.update(appointment!.user_uid, userEntity);
return vote; return vote;
} }
@ -118,8 +126,10 @@ export default class LiveVoteService extends BaseService {
const appointment = await this.getAppointmentWithVotes(vote); const appointment = await this.getAppointmentWithVotes(vote);
if (appointment) { if (appointment) {
const appointmentEntity = Appointment.hydrate<Appointment>(appointment, { strategy: "excludeAll" }); const voteEntity = Vote.hydrateArray<Vote>(appointment.votes, { strategy: "excludeAll" });
if (appointmentEntity?.votes && appointmentEntity.votes.length >= 2) { const appointementWithVotesHydrated = {...appointment, votes: voteEntity};
const appointmentEntity = Appointment.hydrate<Appointment>(appointementWithVotesHydrated, { strategy: "excludeAll" });
if (appointmentEntity.votes && appointmentEntity.votes.length >= 2) {
const voteCreated = await this.voteRepository.create(vote); const voteCreated = await this.voteRepository.create(vote);
return this.closeVote(appointment, voteCreated); return this.closeVote(appointment, voteCreated);
} }