From 4860a952090a3be373351da5c3f09fb885ae8b6b Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Mon, 9 Oct 2023 10:05:25 +0200 Subject: [PATCH 1/6] :sparkles: Delete files on refusing documents --- src/app/api/admin/DocumentsController.ts | 56 +++++++++++++-- src/app/api/notary/DocumentsController.ts | 52 +++++++++++++- .../api/super-admin/DocumentsController.ts | 70 ++++++++++++++++--- .../repositories/DocumentsRepository.ts | 22 +++++- .../DocumentsService/DocumentsService.ts | 18 ++++- .../DocumentsService/DocumentsService.ts | 18 ++++- .../DocumentsService/DocumentsService.ts | 20 ++++-- 7 files changed, 227 insertions(+), 29 deletions(-) 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); } From 294fc3fd9f7a65f9391a0d6a005a2cf6e273773b Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Mon, 9 Oct 2023 11:08:27 +0200 Subject: [PATCH 2/6] :sparkles: Super admin can manage office roles --- src/app/api/super-admin/UsersController.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/app/api/super-admin/UsersController.ts b/src/app/api/super-admin/UsersController.ts index fd135312..bacb7850 100644 --- a/src/app/api/super-admin/UsersController.ts +++ b/src/app/api/super-admin/UsersController.ts @@ -109,30 +109,32 @@ export default class UsersController extends ApiController { //init IUser resource with request body values const userEntity = User.hydrate(req.body); - if(userEntity.role) { + if (userEntity.role) { const role = await this.roleService.getByUid(userEntity.role.uid!); - if(!role) { + if (!role) { this.httpBadRequest(response, "Role not found"); return; } - if (role.name === "super-admin" || userFound.role.name === "super-admin" ) { + if (role.name === "super-admin" || userFound.role.name === "super-admin") { this.httpBadRequest(response, "Cannot assign or remove super-admin role"); return; } } - if(userEntity.office_role) { + if (userEntity.office_role) { const officeRole = await this.officeRoleService.getByUid(userEntity.office_role.uid!); - if(!officeRole) { + if (!officeRole) { this.httpBadRequest(response, "Office role not found"); return; } - if (officeRole.office_uid != userFound.office_uid) { - this.httpBadRequest(response, "Cannot assign an office role from another office"); - return; - } + + // Not needed if you're super admin you can assign every roles from every offices + // if (officeRole.office_uid != userFound.office_uid) { + // this.httpBadRequest(response, "Cannot assign an office role from another office"); + // return; + // } } - + //call service to get prisma entity const userEntityUpdated = await this.usersService.update(uid, userEntity); From 4b790599c22ea1a96f067746ff2c8c49b37510c2 Mon Sep 17 00:00:00 2001 From: OxSaitama Date: Mon, 9 Oct 2023 11:19:51 +0200 Subject: [PATCH 3/6] add middleware for requests on customers --- src/app/api/admin/CustomersController.ts | 5 ++-- src/app/api/notary/CustomersController.ts | 5 ++-- .../api/super-admin/CustomersController.ts | 5 ++-- .../CustomerHandler.ts | 28 +++++++++++++++++++ .../AnchoringProofService.ts | 2 +- 5 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 src/app/middlewares/OfficeMembershipHandlers/CustomerHandler.ts diff --git a/src/app/api/admin/CustomersController.ts b/src/app/api/admin/CustomersController.ts index 0094a678..e1badb0d 100644 --- a/src/app/api/admin/CustomersController.ts +++ b/src/app/api/admin/CustomersController.ts @@ -9,6 +9,7 @@ import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; import roleHandler from "@App/middlewares/RolesHandler"; import { Prisma } from "@prisma/client"; +import customerHandler from "@App/middlewares/OfficeMembershipHandlers/CustomerHandler"; @Controller() @Service() @@ -78,7 +79,7 @@ export default class CustomersController extends ApiController { /** * @description Modify a specific customer by uid */ - @Put("/api/v1/admin/customers/:uid", [authHandler, roleHandler, ruleHandler]) + @Put("/api/v1/admin/customers/:uid", [authHandler, roleHandler, ruleHandler, customerHandler]) protected async put(req: Request, response: Response) { try { const uid = req.params["uid"]; @@ -119,7 +120,7 @@ export default class CustomersController extends ApiController { /** * @description Get a specific customer by uid */ - @Get("/api/v1/admin/customers/:uid", [authHandler, roleHandler, ruleHandler]) + @Get("/api/v1/admin/customers/:uid", [authHandler, roleHandler, ruleHandler, customerHandler]) protected async getOneByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; diff --git a/src/app/api/notary/CustomersController.ts b/src/app/api/notary/CustomersController.ts index 1362bb98..4415fd34 100644 --- a/src/app/api/notary/CustomersController.ts +++ b/src/app/api/notary/CustomersController.ts @@ -8,6 +8,7 @@ import { validateOrReject } from "class-validator"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; import { Prisma } from "@prisma/client"; +import customerHandler from "@App/middlewares/OfficeMembershipHandlers/CustomerHandler"; @Controller() @Service() @@ -76,7 +77,7 @@ export default class CustomersController extends ApiController { /** * @description Modify a specific customer by uid */ - @Put("/api/v1/notary/customers/:uid", [authHandler, ruleHandler]) + @Put("/api/v1/notary/customers/:uid", [authHandler, ruleHandler, customerHandler]) protected async put(req: Request, response: Response) { try { const uid = req.params["uid"]; @@ -117,7 +118,7 @@ export default class CustomersController extends ApiController { /** * @description Get a specific customer by uid */ - @Get("/api/v1/notary/customers/:uid", [authHandler, ruleHandler]) + @Get("/api/v1/notary/customers/:uid", [authHandler, ruleHandler, customerHandler]) protected async getOneByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; diff --git a/src/app/api/super-admin/CustomersController.ts b/src/app/api/super-admin/CustomersController.ts index 62a82548..628fa4ed 100644 --- a/src/app/api/super-admin/CustomersController.ts +++ b/src/app/api/super-admin/CustomersController.ts @@ -9,6 +9,7 @@ import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; import roleHandler from "@App/middlewares/RolesHandler"; import { Prisma } from "@prisma/client"; +import customerHandler from "@App/middlewares/OfficeMembershipHandlers/CustomerHandler"; @Controller() @Service() @@ -78,7 +79,7 @@ export default class CustomersController extends ApiController { /** * @description Modify a specific customer by uid */ - @Put("/api/v1/super-admin/customers/:uid", [authHandler, roleHandler, ruleHandler]) + @Put("/api/v1/super-admin/customers/:uid", [authHandler, roleHandler, ruleHandler, customerHandler]) protected async put(req: Request, response: Response) { try { const uid = req.params["uid"]; @@ -119,7 +120,7 @@ export default class CustomersController extends ApiController { /** * @description Get a specific customer by uid */ - @Get("/api/v1/super-admin/customers/:uid", [authHandler, roleHandler, ruleHandler]) + @Get("/api/v1/super-admin/customers/:uid", [authHandler, roleHandler, ruleHandler, customerHandler]) protected async getOneByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; diff --git a/src/app/middlewares/OfficeMembershipHandlers/CustomerHandler.ts b/src/app/middlewares/OfficeMembershipHandlers/CustomerHandler.ts new file mode 100644 index 00000000..f0b8b532 --- /dev/null +++ b/src/app/middlewares/OfficeMembershipHandlers/CustomerHandler.ts @@ -0,0 +1,28 @@ +import HttpCodes from "@Common/system/controller-pattern/HttpCodes"; +import { NextFunction, Request, Response } from "express"; +import Container from "typedi"; +import CustomersService from "@Services/super-admin/CustomersService/CustomersService"; + +export default async function customerHandler(req: Request, response: Response, next: NextFunction) { + try { + const officeId = req.body.user.office_Id; + const uid = req.path && req.path.split("/")[5]; + + if (uid) { + const customerService = Container.get(CustomersService); + const customer = await customerService.get({where:{AND: [{uid: uid}, {office_folders: {some: {office_uid: officeId}}}]}}); + + if (!customer[0]) { + response.status(HttpCodes.NOT_FOUND).send("Customer not found"); + return; + } + } + + next(); + + } catch (error) { + console.log(error); + response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error"); + return; + } +} diff --git a/src/services/common/AnchoringProofService/AnchoringProofService.ts b/src/services/common/AnchoringProofService/AnchoringProofService.ts index 6f13103f..2522fa33 100644 --- a/src/services/common/AnchoringProofService/AnchoringProofService.ts +++ b/src/services/common/AnchoringProofService/AnchoringProofService.ts @@ -72,7 +72,7 @@ export default class AnchoringProofService extends BaseService { */ public async generate(data: AnchoringProofData): Promise { const browser = await puppeteer.launch({ - headless: true, + headless: 'new', args: ["--no-sandbox", "--disable-gpu"], }); const page = await browser.newPage(); From 542c568e10c3e164ab8465dda99acbfcefc44393 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Mon, 9 Oct 2023 11:22:23 +0200 Subject: [PATCH 4/6] :bug: Cannot anchor a folder if not all documents are validated --- .../api/notary/OfficeFolderAnchorsController.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/app/api/notary/OfficeFolderAnchorsController.ts b/src/app/api/notary/OfficeFolderAnchorsController.ts index 62c9d3d7..3a43a703 100644 --- a/src/app/api/notary/OfficeFolderAnchorsController.ts +++ b/src/app/api/notary/OfficeFolderAnchorsController.ts @@ -2,7 +2,7 @@ import { Response, Request } from "express"; import { Controller, Get, Post } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; -import { OfficeFolder } from "le-coffre-resources/dist/Notary"; +import { Document, OfficeFolder } from "le-coffre-resources/dist/Notary"; import { getFolderHashes } from "@Common/optics/notary"; import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService"; import OfficeFolderAnchorsRepository from "@Repositories/OfficeFolderAnchorsRepository"; @@ -136,6 +136,18 @@ export default class OfficeFoldersController extends ApiController { const officeFolder = OfficeFolder.hydrate(officeFolderFound, { strategy: "excludeAll" }); + // Check if every document is validated in a folder + const documents = officeFolder.documents ?? []; + const documentsValidated = documents.filter((document) => { + let documentHydrated = Document.hydrate(document, { strategy: "excludeAll" }); + return documentHydrated.document_status === "VALIDATED"; + }); + + if (documentsValidated.length !== documents.length && documents.length !== 0) { + this.httpBadRequest(response, "Cannot anchor a folder with non validated documents"); + return; + } + const folderHashes = getFolderHashes(officeFolder); if (folderHashes.length === 0) { From 4923284e8b64372a265c22e574c52ef8e1ce5069 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Mon, 9 Oct 2023 11:36:15 +0200 Subject: [PATCH 5/6] :bug: Reintegrating condition on office roles --- src/app/api/super-admin/UsersController.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/app/api/super-admin/UsersController.ts b/src/app/api/super-admin/UsersController.ts index bacb7850..a4a63b51 100644 --- a/src/app/api/super-admin/UsersController.ts +++ b/src/app/api/super-admin/UsersController.ts @@ -128,11 +128,10 @@ export default class UsersController extends ApiController { return; } - // Not needed if you're super admin you can assign every roles from every offices - // if (officeRole.office_uid != userFound.office_uid) { - // this.httpBadRequest(response, "Cannot assign an office role from another office"); - // return; - // } + if (officeRole.office_uid != userFound.office_uid) { + this.httpBadRequest(response, "Cannot assign an office role from another office"); + return; + } } //call service to get prisma entity From 23ba2672dd68fb00f866dc4c02d7eb200b53169c Mon Sep 17 00:00:00 2001 From: OxSaitama Date: Mon, 9 Oct 2023 12:02:28 +0200 Subject: [PATCH 6/6] refacto GET officeFolder for admin & super-admin --- src/app/api/admin/OfficeFoldersController.ts | 8 ++++---- .../super-admin/OfficeFoldersController.ts | 19 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/app/api/admin/OfficeFoldersController.ts b/src/app/api/admin/OfficeFoldersController.ts index 42fbdc58..bf1c8732 100644 --- a/src/app/api/admin/OfficeFoldersController.ts +++ b/src/app/api/admin/OfficeFoldersController.ts @@ -58,10 +58,10 @@ export default class OfficeFoldersController extends ApiController { }; } - const officeId: string = req.body.user.office_Id; - const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ; - if(!query.where) query.where = { office: officeWhereInput}; - query.where.office = officeWhereInput; + const userId: string = req.body.user.userId; + if (query.where?.stakeholders) delete query.where.stakeholders; + const officeFoldersWhereInput: Prisma.OfficeFoldersWhereInput = { ...query.where, stakeholders: { some: { uid: userId } } }; + query.where = officeFoldersWhereInput; //call service to get prisma entity const officeFolderEntities: OfficeFolders[] = await this.officeFoldersService.get(query); diff --git a/src/app/api/super-admin/OfficeFoldersController.ts b/src/app/api/super-admin/OfficeFoldersController.ts index e0b01345..f31d2807 100644 --- a/src/app/api/super-admin/OfficeFoldersController.ts +++ b/src/app/api/super-admin/OfficeFoldersController.ts @@ -44,12 +44,10 @@ export default class OfficeFoldersController extends ApiController { { customers: { some: { - contact: { - OR: [ - { first_name: { contains: filter, mode: "insensitive" } }, - { last_name: { contains: filter, mode: "insensitive" } }, - ], - }, + OR: [ + { contact: { first_name: { contains: filter, mode: "insensitive" } } }, + { contact: { last_name: { contains: filter, mode: "insensitive" } } }, + ], }, }, }, @@ -57,10 +55,11 @@ export default class OfficeFoldersController extends ApiController { }, }; } - const officeId: string = req.body.user.office_Id; - const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId }; - if (!query.where) query.where = { office: officeWhereInput }; - query.where.office = officeWhereInput; + + const userId: string = req.body.user.userId; + if (query.where?.stakeholders) delete query.where.stakeholders; + const officeFoldersWhereInput: Prisma.OfficeFoldersWhereInput = { ...query.where, stakeholders: { some: { uid: userId } } }; + query.where = officeFoldersWhereInput; //call service to get prisma entity const officeFolderEntities: OfficeFolders[] = await this.officeFoldersService.get(query);