This commit is contained in:
Vins 2023-08-01 11:48:24 +02:00
parent 194565e962
commit e54df01c91
8 changed files with 149 additions and 29 deletions

View File

@ -10,11 +10,12 @@ 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 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 notificationBuilder: NotificationBuilder) {
super(); super();
} }
@ -103,6 +104,8 @@ 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"] });
@ -111,6 +114,7 @@ export default class DocumentsController extends ApiController {
//create email for asked document //create email for asked document
this.emailBuilder.sendDocumentEmails(documentEntityUpdated); this.emailBuilder.sendDocumentEmails(documentEntityUpdated);
this.notificationBuilder.sendDocumentAnchoredNotificatiom(documentEntityUpdated);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" }); const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "notifications" ADD COLUMN "status" "ENotificationStatus" NOT NULL DEFAULT 'UNREAD';

View File

@ -6,8 +6,8 @@ generator client {
} }
datasource db { datasource db {
provider = "postgresql" provider = "postgresql"
url = env("DEV_PRISMA_STUDIO_DB_URL") url = env("DEV_PRISMA_STUDIO_DB_URL")
shadowDatabaseUrl = env("DEV_PRISMA_STUDIO_SHADOW_URL") shadowDatabaseUrl = env("DEV_PRISMA_STUDIO_SHADOW_URL")
} }
@ -102,12 +102,13 @@ model Customers {
} }
model Notifications { model Notifications {
uid String @id @unique @default(uuid()) uid String @id @unique @default(uuid())
message String @db.VarChar(255) message String @db.VarChar(255)
redirection_url String @db.VarChar(255) redirection_url String @db.VarChar(255)
created_at DateTime? @default(now()) created_at DateTime? @default(now())
status ENotificationStatus @default(UNREAD)
updated_at DateTime? @updatedAt updated_at DateTime? @updatedAt
users Users[] @relation("UserHasNotifications") users Users[] @relation("UserHasNotifications")
@@map("notifications") @@map("notifications")
} }

View File

@ -0,0 +1,45 @@
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
import { Documents } from "@prisma/client";
import { Document } from "le-coffre-resources/dist/SuperAdmin";
import { Service } from "typedi";
import NotificationsService from "@Services/common/NotificationsService/NotificationsService";
@Service()
export default class NotificationBuilder {
public constructor(private notificationsService : NotificationsService, private documentsService: DocumentsService){}
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}} });
if(!documentPrisma) throw new Error("Document not found");
const document = Document.hydrate<Document>(documentPrisma);
console.log(document);
this.notificationsService.create({
message: "Document déposé",
redirection_url: "",
created_at: new Date(),
updated_at: new Date(),
});
}
public async sendDocumentAnchoredNotificatiom(documentEntity: Documents){
if(documentEntity.document_status !== "ANCHORED") return;
const documentPrisma = await this.documentsService.getByUid(documentEntity.uid, { depositor: {include: {contact: true}}, folder:{include:{ office: true}} });
if(!documentPrisma) throw new Error("Document not found");
const document = Document.hydrate<Document>(documentPrisma);
console.log(document);
this.notificationsService.create({
message: "Document anchré",
redirection_url: "",
created_at: new Date(),
updated_at: new Date(),
});
}
}

View File

@ -0,0 +1,4 @@
export const ETemplates = {
DOCUMENT_ASKED: "DOCUMENT_ASKED",
DOCUMENT_REFUSED: "DOCUMENT_REFUSED",
};

View File

@ -0,0 +1,73 @@
import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi";
import { Notifications, Prisma } from "prisma/prisma-client";
import { Notification } from "le-coffre-resources/dist/SuperAdmin";
@Service()
export default class NotificationRepository extends BaseRepository {
constructor(private database: Database) {
super();
}
protected get model() {
return this.database.getClient().notifications;
}
protected get instanceDb() {
return this.database.getClient();
}
/**
* @description : Find many emails
*/
public async findMany(query: Prisma.NotificationsFindManyArgs) {
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
return this.model.findMany(query);
}
/**
* @description : Create an email
*/
public async create(notification: Notification): Promise<Notifications> {
const createArgs: Prisma.NotificationsCreateArgs = {
data: {
message: notification.message,
redirection_url: notification.redirection_url,
users: {
connect: notification.user
}
},
};
return this.model.create(createArgs);
}
/**
* @description : update given email
*/
public async update(uid: string, notification: Notifications): Promise<Notifications> {
const updateArgs: Prisma.NotificationsUpdateArgs = {
where: {
uid: uid,
},
data: {
status: notification.status,
},
};
return this.model.update(updateArgs);
}
/**
* @description : find unique email
*/
public async findOneByUid(uid: string) {
return this.model.findUnique({
where: {
uid: uid,
},
});
}
}

View File

@ -1,9 +1,12 @@
import NotificationRepository from "@Repositories/NotificationRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { Notifications } from "@prisma/client";
import { Notification } from "le-coffre-resources/dist/SuperAdmin";
import { Service } from "typedi"; import { Service } from "typedi";
@Service() @Service()
export default class NotificationsService extends BaseService { export default class NotificationsService extends BaseService {
constructor() { constructor(private notificationRepository: NotificationRepository) {
super(); super();
} }
@ -11,47 +14,35 @@ export default class NotificationsService extends BaseService {
* @description : Get all notifications * @description : Get all notifications
* @returns : T * @returns : T
* @throws {Error} If notifications cannot be get * @throws {Error} If notifications cannot be get
* @param : projectEntity: Partial<ProjectEntity>
*/ */
public async get() { public async get(query: any): Promise<Notifications[]> {
// const notifications = await this.usersRepository.findOne(uuid); return this.notificationRepository.findMany(query);
// if (!notifications) Promise.reject(new Error("Cannot get notifications"));
return { response: "/api/notifications > GET : All notifications > Not implemented yet" };
} }
/** /**
* @description : Create a new notification * @description : Create a new notification
* @returns : T * @returns : T
* @throws {Error} If notification cannot be created * @throws {Error} If notification cannot be created
* @param : projectEntity: Partial<ProjectEntity>
*/ */
public async create() { public async create(notification: Notification): Promise<Notifications> {
// const notification = await this.projectRepository.create(projectEntity); return this.notificationRepository.create(notification);
// if (!notification) Promise.reject(new Error("Cannot create project"));
return { response: "/api/notifications > POST : Create notification > Not implemented yet" };
} }
/** /**
* @description : Modify a new notification * @description : Modify a new notification
* @returns : T * @returns : T
* @throws {Error} If notification cannot be modified * @throws {Error} If notification cannot be modified
* @param : projectEntity: Partial<ProjectEntity>
*/ */
public async put() { public async update(uid: string, notificationEntity: Notifications): Promise<Notifications> {
// const notification = await this.projectRepository.create(projectEntity); return this.notificationRepository.update(uid, notificationEntity);
// if (!notification) Promise.reject(new Error("Cannot create project"));
return { response: "/api/notifications > PUT : Modified notification > Not implemented yet" };
} }
/** /**
* @description : Get a notification by uid * @description : Get a notification by uid
* @returns : T * @returns : T
* @throws {Error} If project cannot be created * @throws {Error} If project cannot be created
* @param : projectEntity: Partial<ProjectEntity>
*/ */
public async getByUid(uid: string) { public async getByUid(uid: string, query?: any): Promise<Notifications | null> {
// const notification = await this.usersRepository.findOne(uid); return this.notificationRepository.findOneByUid(uid);
// if (!notification) Promise.reject(new Error("Cannot get notification by uid"));
return { response: "/api/notifications/:uid > GET : notification by uid > Not implemented yet" };
} }
} }