From 95bcb05340d44c03a8704cc987234073c23dc614 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Wed, 4 Oct 2023 17:23:32 +0200 Subject: [PATCH] :sparkles: Refacto archive/restore --- src/app/api/notary/OfficeFoldersController.ts | 120 ++++++++++++++++-- 1 file changed, 111 insertions(+), 9 deletions(-) diff --git a/src/app/api/notary/OfficeFoldersController.ts b/src/app/api/notary/OfficeFoldersController.ts index f0b19ead..5df72822 100644 --- a/src/app/api/notary/OfficeFoldersController.ts +++ b/src/app/api/notary/OfficeFoldersController.ts @@ -44,9 +44,9 @@ export default class OfficeFoldersController extends ApiController { customers: { some: { OR: [ - {contact: { first_name: { contains: filter, mode: "insensitive" } }}, - {contact: { last_name: { contains: filter, mode: "insensitive" } }}, - ] + { contact: { first_name: { contains: filter, mode: "insensitive" } } }, + { contact: { last_name: { contains: filter, mode: "insensitive" } } }, + ], }, }, }, @@ -56,8 +56,8 @@ export default class OfficeFoldersController extends ApiController { } 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 }}}; + 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 @@ -111,7 +111,9 @@ export default class OfficeFoldersController extends ApiController { return; } - const officeFolderFound = await this.officeFoldersService.getByUid(uid); + const officeFolderFound = await this.officeFoldersService.getByUid(uid, { + folder_anchor: true, + }); if (!officeFolderFound) { this.httpNotFoundRequest(response, "office folder not found"); @@ -119,13 +121,17 @@ export default class OfficeFoldersController extends ApiController { } //init OfficeFolder resource with request body values - const officeFolderEntity = OfficeFolder.hydrate(req.body); + const officefolderToUpdate = OfficeFolder.hydrate(req.body); //validate folder - await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false }); + await validateOrReject(officefolderToUpdate, { groups: ["updateFolder"], forbidUnknownValues: false }); + if ((officeFolderFound as any).folder_anchor?.status === "VERIFIED_ON_CHAIN") { + this.httpBadRequest(response, "Cannot update a verified folder"); + return; + } //call service to get prisma entity - const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); + const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officefolderToUpdate); //Hydrate ressource with prisma entity const officeFolders = OfficeFolder.hydrate(officeFolderEntityUpdated, { @@ -140,6 +146,102 @@ export default class OfficeFoldersController extends ApiController { } } + /** + * @description Modify a specific folder by uid + */ + @Put("/api/v1/notary/folders/:uid/archive", [authHandler, ruleHandler, folderHandler]) + protected async archive(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const officeFolderFound = await this.officeFoldersService.getByUid(uid, { + folder_anchor: true, + }); + + if (!officeFolderFound) { + this.httpNotFoundRequest(response, "office folder not found"); + return; + } + + //init OfficeFolder resource with request body values + const officefolderToUpdate = OfficeFolder.hydrate(officeFolderFound); + + officefolderToUpdate.status = "ARCHIVED"; + officefolderToUpdate.archived_description = req.body.archived_description ?? ""; + + //validate folder + await validateOrReject(officefolderToUpdate, { groups: ["updateFolder"], forbidUnknownValues: false }); + + if ((officeFolderFound as any).folder_anchor?.status !== "VERIFIED_ON_CHAIN") { + this.httpBadRequest(response, "Cannot archive a not anchored folder"); + return; + } + + //call service to get prisma entity + const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officefolderToUpdate); + + //Hydrate ressource with prisma entity + const officeFolders = OfficeFolder.hydrate(officeFolderEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, officeFolders); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + /** + * @description Modify a specific folder by uid + */ + @Put("/api/v1/notary/folders/:uid/restore", [authHandler, ruleHandler, folderHandler]) + protected async restore(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + const officeFolderFound = await this.officeFoldersService.getByUid(uid, { + folder_anchor: true, + }); + + if (!officeFolderFound) { + this.httpNotFoundRequest(response, "office folder not found"); + return; + } + + //init OfficeFolder resource with request body values + const officefolderToUpdate = OfficeFolder.hydrate(officeFolderFound); + + officefolderToUpdate.status = "LIVE"; + officefolderToUpdate.archived_description = ""; + + //validate folder + await validateOrReject(officefolderToUpdate, { groups: ["updateFolder"], forbidUnknownValues: false }); + + //call service to get prisma entity + const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officefolderToUpdate); + + //Hydrate ressource with prisma entity + const officeFolders = OfficeFolder.hydrate(officeFolderEntityUpdated, { + strategy: "excludeAll", + }); + + //success + this.httpSuccess(response, officeFolders); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } /** * @description Get a specific folder by uid * @returns IFolder