diff --git a/src/app/api/admin/DocumentsController.ts b/src/app/api/admin/DocumentsController.ts index 05a21c1e..9f9e322b 100644 --- a/src/app/api/admin/DocumentsController.ts +++ b/src/app/api/admin/DocumentsController.ts @@ -3,7 +3,7 @@ import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import DocumentsService from "@Services/admin/DocumentsService/DocumentsService"; -import { Documents, Prisma } from "@prisma/client"; +import { Documents, EDocumentStatus, Prisma } from "@prisma/client"; import { Document } from "le-coffre-resources/dist/Admin"; import { validateOrReject } from "class-validator"; import authHandler from "@App/middlewares/AuthHandler"; @@ -31,8 +31,8 @@ export default class DocumentsController extends ApiController { query = JSON.parse(req.query["q"] as string); } const officeId: string = req.body.user.office_Id; - const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ; - if(!query.where) query.where = { document_type : {office: officeWhereInput}}; + const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId }; + if (!query.where) query.where = { document_type: { office: officeWhereInput } }; query.where.document_type!.office = officeWhereInput; //call service to get prisma entity @@ -104,7 +104,55 @@ export default class DocumentsController extends ApiController { await validateOrReject(documentEntity, { groups: ["updateDocument"] }); //call service to get prisma entity - const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); + const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity); + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Update a specific document + */ + @Put("/api/v1/notary/documents/:uid/refuse", [authHandler, ruleHandler, documentHandler]) + protected async refuseDocument(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const documentFound = await this.documentsService.getByUid(uid, { + files: true, + }); + + if (!documentFound) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + + //init Document resource with request body values + const documentEntity = Document.hydrate(documentFound); + + // Status to refuse + documentEntity.document_status = EDocumentStatus.REFUSED; + + //validate document + await validateOrReject(documentEntity, { groups: ["updateDocument"] }); + + //call service to get prisma entity + const documentEntityUpdated: Documents = await this.documentsService.refuse(uid, documentEntity, req.body.refused_reason); + + //create email for asked document + // this.emailBuilder.sendDocumentEmails(documentEntityUpdated); + // this.notificationBuilder.sendDocumentAnchoredNotificatiom(documentEntityUpdated); //Hydrate ressource with prisma entity const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); diff --git a/src/app/api/notary/DocumentsController.ts b/src/app/api/notary/DocumentsController.ts index 9c75455d..b6510943 100644 --- a/src/app/api/notary/DocumentsController.ts +++ b/src/app/api/notary/DocumentsController.ts @@ -3,7 +3,7 @@ import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import DocumentsService from "@Services/notary/DocumentsService/DocumentsService"; -import { Documents, Prisma } from "@prisma/client"; +import { Documents, EDocumentStatus, Prisma } from "@prisma/client"; import { Document, OfficeFolder } from "le-coffre-resources/dist/Notary"; import { validateOrReject } from "class-validator"; import authHandler from "@App/middlewares/AuthHandler"; @@ -127,7 +127,55 @@ export default class DocumentsController extends ApiController { await validateOrReject(documentEntity, { groups: ["updateDocument"] }); //call service to get prisma entity - const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); + const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity); + + //create email for asked document + // this.emailBuilder.sendDocumentEmails(documentEntityUpdated); + // this.notificationBuilder.sendDocumentAnchoredNotificatiom(documentEntityUpdated); + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Update a specific document + */ + @Put("/api/v1/notary/documents/:uid/refuse", [authHandler, ruleHandler, documentHandler]) + protected async refuseDocument(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const documentFound = await this.documentsService.getByUid(uid, { + files: true, + }); + + if (!documentFound) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + + //init Document resource with request body values + const documentEntity = Document.hydrate(documentFound); + + // Status to refuse + documentEntity.document_status = EDocumentStatus.REFUSED; + + //validate document + await validateOrReject(documentEntity, { groups: ["updateDocument"] }); + + //call service to get prisma entity + const documentEntityUpdated: Documents = await this.documentsService.refuse(uid, documentEntity, req.body.refused_reason); //create email for asked document // this.emailBuilder.sendDocumentEmails(documentEntityUpdated); diff --git a/src/app/api/super-admin/DocumentsController.ts b/src/app/api/super-admin/DocumentsController.ts index f589ce4c..f33075d1 100644 --- a/src/app/api/super-admin/DocumentsController.ts +++ b/src/app/api/super-admin/DocumentsController.ts @@ -4,7 +4,7 @@ import roleHandler from "@App/middlewares/RolesHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; -import { Documents, Prisma } from "@prisma/client"; +import { Documents, EDocumentStatus, Prisma } from "@prisma/client"; import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; import { validateOrReject } from "class-validator"; import { Request, Response } from "express"; @@ -31,16 +31,16 @@ export default class DocumentsController extends ApiController { query = JSON.parse(req.query["q"] as string); } const officeId: string = req.body.user.office_Id; - - const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ; - - if(!query.where) query.where = { document_type : {office: officeWhereInput}}; - - // query.where.document_type!.office = officeWhereInput; + + const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId }; + + if (!query.where) query.where = { document_type: { office: officeWhereInput } }; + + // query.where.document_type!.office = officeWhereInput; //call service to get prisma entity - - const documentEntities = await this.documentsService.get(query); + + const documentEntities = await this.documentsService.get(query); //Hydrate ressource with prisma entity const documents = Document.hydrateArray(documentEntities, { strategy: "excludeAll" }); @@ -108,7 +108,55 @@ export default class DocumentsController extends ApiController { await validateOrReject(documentEntity, { groups: ["updateDocument"] }); //call service to get prisma entity - const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); + const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity); + + //Hydrate ressource with prisma entity + const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Update a specific document + */ + @Put("/api/v1/notary/documents/:uid/refuse", [authHandler, ruleHandler, documentHandler]) + protected async refuseDocument(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const documentFound = await this.documentsService.getByUid(uid, { + files: true, + }); + + if (!documentFound) { + this.httpNotFoundRequest(response, "document not found"); + return; + } + + //init Document resource with request body values + const documentEntity = Document.hydrate(documentFound); + + // Status to refuse + documentEntity.document_status = EDocumentStatus.REFUSED; + + //validate document + await validateOrReject(documentEntity, { groups: ["updateDocument"] }); + + //call service to get prisma entity + const documentEntityUpdated: Documents = await this.documentsService.refuse(uid, documentEntity, req.body.refused_reason); + + //create email for asked document + // this.emailBuilder.sendDocumentEmails(documentEntityUpdated); + // this.notificationBuilder.sendDocumentAnchoredNotificatiom(documentEntityUpdated); //Hydrate ressource with prisma entity const document = Document.hydrate(documentEntityUpdated, { strategy: "excludeAll" }); @@ -170,7 +218,7 @@ export default class DocumentsController extends ApiController { if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); } - + const documentEntity = await this.documentsService.getByUid(uid, query); if (!documentEntity) { diff --git a/src/common/repositories/DocumentsRepository.ts b/src/common/repositories/DocumentsRepository.ts index 4c15dc52..5fcbf2f0 100644 --- a/src/common/repositories/DocumentsRepository.ts +++ b/src/common/repositories/DocumentsRepository.ts @@ -21,7 +21,6 @@ export default class DocumentsRepository extends BaseRepository { * @description : Find many documents */ public async findMany(query: Prisma.DocumentsFindManyArgs) { - query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); return this.model.findMany(query); } @@ -96,7 +95,26 @@ export default class DocumentsRepository extends BaseRepository { /** * @description : Update data of a document */ - public async update(uid: string, document: Partial, refusedReason?: string): Promise { + public async update(uid: string, document: Partial): Promise { + return this.model.update({ + where: { + uid: uid, + }, + data: { + document_status: EDocumentStatus[document.document_status as keyof typeof EDocumentStatus], + document_history: { + create: { + document_status: EDocumentStatus[document.document_status as keyof typeof EDocumentStatus], + }, + }, + }, + }); + } + + /** + * @description : Update data of a document + */ + public async refuse(uid: string, document: Partial, refusedReason?: string): Promise { return this.model.update({ where: { uid: uid, diff --git a/src/services/admin/DocumentsService/DocumentsService.ts b/src/services/admin/DocumentsService/DocumentsService.ts index c8e7aa0b..5bdbead9 100644 --- a/src/services/admin/DocumentsService/DocumentsService.ts +++ b/src/services/admin/DocumentsService/DocumentsService.ts @@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/Admin"; import DocumentsRepository from "@Repositories/DocumentsRepository"; import BaseService from "@Services/BaseService"; import { Service } from "typedi"; +import FilesRepository from "@Repositories/FilesRepository"; @Service() export default class DocumentsService extends BaseService { - constructor(private documentsRepository: DocumentsRepository) { + constructor(private documentsRepository: DocumentsRepository, private filesRepository: FilesRepository) { super(); } @@ -38,8 +39,19 @@ export default class DocumentsService extends BaseService { * @description : Modify a document * @throws {Error} If document cannot be modified */ - public async update(uid: string, document: Partial, refused_reason?: string): Promise { - return this.documentsRepository.update(uid, document, refused_reason); + public async update(uid: string, document: Partial): Promise { + return this.documentsRepository.update(uid, document); + } + + public async refuse(uid: string, document: Partial, refused_reason: string): Promise { + if (document.files) { + for (let i = 0; i < document.files.length; i++) { + console.log("archiving file", document.files[i]?.uid); + await this.filesRepository.deleteKeyAndArchive(document.files[i]?.uid as string); + } + } + + return this.documentsRepository.refuse(uid, document, refused_reason); } /** diff --git a/src/services/notary/DocumentsService/DocumentsService.ts b/src/services/notary/DocumentsService/DocumentsService.ts index 7ddb9c6a..c2260763 100644 --- a/src/services/notary/DocumentsService/DocumentsService.ts +++ b/src/services/notary/DocumentsService/DocumentsService.ts @@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/Notary"; import DocumentsRepository from "@Repositories/DocumentsRepository"; import BaseService from "@Services/BaseService"; import { Service } from "typedi"; +import FilesRepository from "@Repositories/FilesRepository"; @Service() export default class DocumentsService extends BaseService { - constructor(private documentsRepository: DocumentsRepository) { + constructor(private documentsRepository: DocumentsRepository, private filesRepository: FilesRepository) { super(); } @@ -38,8 +39,19 @@ export default class DocumentsService extends BaseService { * @description : Modify a document * @throws {Error} If document cannot be modified */ - public async update(uid: string, document: Partial, refused_reason?: string): Promise { - return this.documentsRepository.update(uid, document, refused_reason); + public async update(uid: string, document: Partial): Promise { + return this.documentsRepository.update(uid, document); + } + + public async refuse(uid: string, document: Partial, refused_reason: string): Promise { + if (document.files) { + for (let i = 0; i < document.files.length; i++) { + console.log("archiving file", document.files[i]?.uid); + await this.filesRepository.deleteKeyAndArchive(document.files[i]?.uid as string); + } + } + + return this.documentsRepository.refuse(uid, document, refused_reason); } /** diff --git a/src/services/super-admin/DocumentsService/DocumentsService.ts b/src/services/super-admin/DocumentsService/DocumentsService.ts index cf2bf617..41041d7e 100644 --- a/src/services/super-admin/DocumentsService/DocumentsService.ts +++ b/src/services/super-admin/DocumentsService/DocumentsService.ts @@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/SuperAdmin"; import DocumentsRepository from "@Repositories/DocumentsRepository"; import BaseService from "@Services/BaseService"; import { Service } from "typedi"; +import FilesRepository from "@Repositories/FilesRepository"; @Service() export default class DocumentsService extends BaseService { - constructor(private documentsRepository: DocumentsRepository) { + constructor(private documentsRepository: DocumentsRepository, private filesRepository: FilesRepository) { super(); } @@ -38,8 +39,19 @@ export default class DocumentsService extends BaseService { * @description : Modify a document * @throws {Error} If document cannot be modified */ - public async update(uid: string, document: Partial, refused_reason?: string): Promise { - return this.documentsRepository.update(uid, document, refused_reason); + public async update(uid: string, document: Partial): Promise { + return this.documentsRepository.update(uid, document); + } + + public async refuse(uid: string, document: Partial, refused_reason: string): Promise { + if (document.files) { + for (let i = 0; i < document.files.length; i++) { + console.log("archiving file", document.files[i]?.uid); + await this.filesRepository.deleteKeyAndArchive(document.files[i]?.uid as string); + } + } + + return this.documentsRepository.refuse(uid, document, refused_reason); } /** @@ -69,7 +81,7 @@ export default class DocumentsService extends BaseService { * @description : Get a document by uid * @throws {Error} If document cannot be get by uid */ - public async getByUidWithFiles(uid: string): Promise { + public async getByUidWithFiles(uid: string): Promise<(Documents & { files: Files[] | null }) | null> { return this.documentsRepository.findOneByUidWithFiles(uid); }