lecoffre-back/src/common/notifications/NotificationBuilder.ts
2023-09-25 15:31:24 +02:00

86 lines
3.7 KiB
TypeScript

import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
import { Documents } from "@prisma/client";
import User, { Document, Vote } from "le-coffre-resources/dist/SuperAdmin";
import { Service } from "typedi";
import NotificationsService from "@Services/common/NotificationsService/NotificationsService";
import UsersService from "@Services/super-admin/UsersService/UsersService";
@Service()
export default class NotificationBuilder {
public constructor(private notificationsService : NotificationsService, private documentsService: DocumentsService, private usersService: UsersService){}
public async sendDocumentDepositedNotification(documentEntity: Documents){
if(documentEntity.document_status !== "DEPOSITED") return;
const documentPrisma = await this.documentsService.getByUid(documentEntity.uid, { depositor: {include: {contact: true}}, folder:{include:{ office: true, stakeholders: true}} });
if(!documentPrisma) throw new Error("Document not found");
const document = Document.hydrate<Document>(documentPrisma);
this.notificationsService.create({
message: "Votre client " + document.depositor?.contact?.first_name + " " + document.depositor?.contact?.last_name + " vous a envoyé un document à valider",
redirection_url: "",
created_at: new Date(),
updated_at: new Date(),
user : document.folder!.stakeholders || [],
});
}
public async sendDocumentAnchoredNotification(documentEntity: Documents){
const documentPrisma = await this.documentsService.getByUid(documentEntity.uid, { depositor: {include: {contact: true}}, folder:{include:{ folder_anchor : true ,office: true, stakeholders: true}} });
if(!documentPrisma) throw new Error("Document not found");
const document = Document.hydrate<Document>(documentPrisma);
if(document.folder?.anchor?.status !== "VERIFIED_ON_CHAIN") return;
this.notificationsService.create({
message: "Le dossier " + document.folder?.folder_number + " - " + document.folder?.name + " a été certifié. Vous pouvez désormais télécharger le certificat de dépôt pour le mettre dans la GED de votre logiciel de rédaction d'acte.",
redirection_url: "",
created_at: new Date(),
updated_at: new Date(),
user : document.folder!.stakeholders || [],
});
}
public async sendVoteNotification(vote: Vote){
if(vote.appointment.status !== "OPEN") return;
const superAdminList = await this.usersService.get({where: {role : {label : "super-admin"}}});
let message = "";
if(vote.appointment.choice === "NOMINATE"){
message = "Un collaborateur souhaite attribuer le titre de Super Administrateur à " + vote.appointment.targeted_user + ". Cliquez ici pour voter."
}
else if(vote.appointment.choice === "DISMISS"){
message = "Un collaborateur souhaite retirer le titre de Super Administrateur à " + vote.appointment.targeted_user + ". Cliquez ici pour voter."
}
this.notificationsService.create({
message: message,
redirection_url: "",
created_at: new Date(),
updated_at: new Date(),
user : superAdminList || [],
});
}
public async sendDismissNotification(user: User){
this.notificationsService.create({
message: "Vous n'avez désormais plus le rôle de Super Administrateur de la plateforme.",
redirection_url: "",
created_at: new Date(),
updated_at: new Date(),
user : [user] || [],
});
}
public async sendNominateNotification(user: User){
this.notificationsService.create({
message: "Vous avez désormais le rôle de Super Administrateur de la plateforme.",
redirection_url: "",
created_at: new Date(),
updated_at: new Date(),
user : [user] || [],
});
}
}