diff --git a/package-lock.json b/package-lock.json index 41374105..ad737c6c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,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.71", + "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.72", "module-alias": "^2.2.2", "multer": "^1.4.5-lts.1", "next": "^13.1.5", diff --git a/package.json b/package.json index 45621fda..8bebff1a 100644 --- a/package.json +++ b/package.json @@ -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.71", + "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.72", "module-alias": "^2.2.2", "multer": "^1.4.5-lts.1", "next": "^13.1.5", diff --git a/src/app/api/customer/FilesController.ts b/src/app/api/customer/FilesController.ts index 83f595ec..925b30c7 100644 --- a/src/app/api/customer/FilesController.ts +++ b/src/app/api/customer/FilesController.ts @@ -13,11 +13,12 @@ import fileHandler from "@App/middlewares/CustomerHandler/FileHandler"; import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService"; import { DocumentType } from "le-coffre-resources/dist/SuperAdmin"; import ObjectHydrate from "@Common/helpers/ObjectHydrate"; +import NotificationBuilder from "@Common/notifications/NotificationBuilder"; @Controller() @Service() export default class FilesController extends ApiController { - constructor(private filesService: FilesService, private documentService: DocumentsService, private documentTypesService : DocumentTypesService) { + constructor(private filesService: FilesService, private documentService: DocumentsService, private documentTypesService : DocumentTypesService, private notificationBuilder : NotificationBuilder) { super(); } @@ -90,7 +91,6 @@ export default class FilesController extends ApiController { //init File resource with request body values const fileEntity = File.hydrate(JSON.parse(req.body["q"])); - console.log(fileEntity); //validate File // await validateOrReject(fileEntity, { groups: ["createFile"] }); @@ -103,7 +103,8 @@ export default class FilesController extends ApiController { const documentToUpdate = Document.hydrate(document!); documentToUpdate!.document_status = "DEPOSITED"; - await this.documentService.update(document!.uid!, documentToUpdate); + const documentUpdated = await this.documentService.update(document!.uid!, documentToUpdate); + await this.notificationBuilder.sendDocumentDepositedNotification(documentUpdated!); //Hydrate ressource with prisma entity const fileEntityHydrated = File.hydrate(fileEntityCreated, { diff --git a/src/app/api/notary/UsersController.ts b/src/app/api/notary/UsersController.ts index 49807103..4f87fe6b 100644 --- a/src/app/api/notary/UsersController.ts +++ b/src/app/api/notary/UsersController.ts @@ -3,7 +3,7 @@ import { Controller, Get } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import UsersService from "@Services/notary/UsersService/UsersService"; import { Service } from "typedi"; -import User from "le-coffre-resources/dist/Notary"; +import User from "le-coffre-resources/dist/SuperAdmin"; import { Prisma } from "@prisma/client"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; diff --git a/src/common/databases/migrations/20230907100058_v26/migration.sql b/src/common/databases/migrations/20230907100058_v26/migration.sql new file mode 100644 index 00000000..0514fb0e --- /dev/null +++ b/src/common/databases/migrations/20230907100058_v26/migration.sql @@ -0,0 +1,37 @@ +/* + Warnings: + + - You are about to drop the column `status` on the `notifications` table. All the data in the column will be lost. + - You are about to drop the `_UserHasNotifications` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE "_UserHasNotifications" DROP CONSTRAINT "_UserHasNotifications_A_fkey"; + +-- DropForeignKey +ALTER TABLE "_UserHasNotifications" DROP CONSTRAINT "_UserHasNotifications_B_fkey"; + +-- AlterTable +ALTER TABLE "notifications" DROP COLUMN "status"; + +-- DropTable +DROP TABLE "_UserHasNotifications"; + +-- CreateTable +CREATE TABLE "user_notifications" ( + "uid" TEXT NOT NULL, + "user_uid" VARCHAR(255) NOT NULL, + "read" BOOLEAN NOT NULL DEFAULT false, + "notification_uid" VARCHAR(255) NOT NULL, + + CONSTRAINT "user_notifications_pkey" PRIMARY KEY ("uid") +); + +-- CreateIndex +CREATE UNIQUE INDEX "user_notifications_uid_key" ON "user_notifications"("uid"); + +-- AddForeignKey +ALTER TABLE "user_notifications" ADD CONSTRAINT "user_notifications_user_uid_fkey" FOREIGN KEY ("user_uid") REFERENCES "users"("uid") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "user_notifications" ADD CONSTRAINT "user_notifications_notification_uid_fkey" FOREIGN KEY ("notification_uid") REFERENCES "notifications"("uid") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/src/common/databases/schema.prisma b/src/common/databases/schema.prisma index 582bdc81..1bf547d5 100644 --- a/src/common/databases/schema.prisma +++ b/src/common/databases/schema.prisma @@ -63,10 +63,10 @@ model Users { updated_at DateTime? @updatedAt office_membership Offices @relation(fields: [office_uid], references: [uid], onDelete: Cascade) office_uid String @db.VarChar(255) - notifications Notifications[] @relation("UserHasNotifications") office_folders OfficeFolders[] @relation("OfficeFolderHasStakeholders") appointment Appointments[] votes Votes[] + user_notifications UserNotifications[] @@map("users") } @@ -108,13 +108,23 @@ model Notifications { message String @db.VarChar(255) redirection_url String @db.VarChar(255) created_at DateTime? @default(now()) - status ENotificationStatus @default(UNREAD) - updated_at DateTime? @updatedAt - users Users[] @relation("UserHasNotifications") + updated_at DateTime? @updatedAt + userNotifications UserNotifications[] @@map("notifications") } +model UserNotifications { + uid String @id @unique @default(uuid()) + user Users @relation(fields: [user_uid], references: [uid], onDelete: Cascade) + user_uid String @db.VarChar(255) + read Boolean @default(false) + notification Notifications @relation(fields: [notification_uid], references: [uid], onDelete: Cascade) + notification_uid String @db.VarChar(255) + + @@map("user_notifications") +} + model OfficeFolders { uid String @id @unique @default(uuid()) folder_number String @db.VarChar(255) diff --git a/src/common/notifications/NotificationBuilder.ts b/src/common/notifications/NotificationBuilder.ts index deb41a08..30aac1f1 100644 --- a/src/common/notifications/NotificationBuilder.ts +++ b/src/common/notifications/NotificationBuilder.ts @@ -10,9 +10,12 @@ export default class NotificationBuilder { public constructor(private notificationsService : NotificationsService, private documentsService: DocumentsService){} public async sendDocumentDepositedNotification(documentEntity: Documents){ + console.log(documentEntity); if(documentEntity.document_status !== "DEPOSITED") return; + + - 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, stakeholders: true}} }); if(!documentPrisma) throw new Error("Document not found"); const document = Document.hydrate(documentPrisma); console.log(document); @@ -22,6 +25,7 @@ export default class NotificationBuilder { redirection_url: "", created_at: new Date(), updated_at: new Date(), + user : document.folder!.stakeholders || [], }); } diff --git a/src/common/repositories/NotificationRepository.ts b/src/common/repositories/NotificationRepository.ts index 4a00c77b..ce59622f 100644 --- a/src/common/repositories/NotificationRepository.ts +++ b/src/common/repositories/NotificationRepository.ts @@ -32,31 +32,28 @@ export default class NotificationRepository extends BaseRepository { data: { message: notification.message, redirection_url: notification.redirection_url, - users: { - connect: notification.user - } + // users: { + // connect: notification.user!.map((user) => { + // return { uid: user.uid }; + // }), + // } + userNotifications:{ + create: notification.user!.map((user) => { + return { + user: { + connect: { + uid: user.uid + } + } + } + }) + } }, }; return this.model.create(createArgs); } - /** - * @description : update given email - */ - public async update(uid: string, notification: Notifications): Promise { - const updateArgs: Prisma.NotificationsUpdateArgs = { - where: { - uid: uid, - }, - data: { - status: notification.status, - }, - }; - - return this.model.update(updateArgs); - } - /** * @description : find unique email */ diff --git a/src/services/common/NotificationsService/NotificationsService.ts b/src/services/common/NotificationsService/NotificationsService.ts index f2410d6c..803c04f3 100644 --- a/src/services/common/NotificationsService/NotificationsService.ts +++ b/src/services/common/NotificationsService/NotificationsService.ts @@ -28,15 +28,6 @@ export default class NotificationsService extends BaseService { return this.notificationRepository.create(notification); } - /** - * @description : Modify a new notification - * @returns : T - * @throws {Error} If notification cannot be modified - */ - public async update(uid: string, notificationEntity: Notifications): Promise { - return this.notificationRepository.update(uid, notificationEntity); - } - /** * @description : Get a notification by uid * @returns : T