lecoffre-back/src/common/notifications/NotificationBuilder.ts
2023-09-29 15:26:36 +02:00

143 lines
5.4 KiB
TypeScript

import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
import { Documents } from "@prisma/client";
import User, { Document, OfficeFolder, 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";
import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService";
import { BackendVariables } from "@Common/config/variables/Variables";
@Service()
export default class NotificationBuilder {
public constructor(
private notificationsService: NotificationsService,
private documentsService: DocumentsService,
private usersService: UsersService,
private foldersService: OfficeFoldersService,
private backendVariables: BackendVariables
) {}
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: `${this.backendVariables.APP_HOST}/folders/${document.folder?.uid}/documents/${document.uid}`,
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?.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: `${this.backendVariables.APP_HOST}/folders/${document.folder?.uid}/documents/${document.uid}`,
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: `${this.backendVariables.APP_HOST}/users/${vote.appointment.targeted_user.uid}`,
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] || [],
});
}
public async sendDocumentExpiringSoonNotification(folderUid: string, expirationDate: Date) {
const day = expirationDate.getDate();
const month = expirationDate.getMonth() + 1; // Months are 0-based, so add 1
const year = expirationDate.getFullYear();
// Ensure that the day and month have two digits (e.g., 01, 02, 11)
const formattedDay = day < 10 ? `0${day}` : `${day}`;
const formattedMonth = month < 10 ? `0${month}` : `${month}`;
const formattedDate = `${formattedDay}/${formattedMonth}/${year}`;
const folderPrisma = await this.foldersService.getByUid(folderUid, { stakeholders: true, office: true });
if (!folderPrisma) throw new Error("Document not found");
const folder = OfficeFolder.hydrate<OfficeFolder>(folderPrisma);
this.notificationsService.create({
message:
"Vous devez valider le dossier " +
folder.folder_number +
" - " +
folder.name +
" avant le " +
formattedDate +
" (date limite d'expiration des documents).",
redirection_url: `${this.backendVariables.APP_HOST}/folders/${folder.uid}`,
created_at: new Date(),
updated_at: new Date(),
user: folder.stakeholders || [],
});
}
}