Refacto archive/restore

This commit is contained in:
Maxime Lalo 2023-10-04 17:23:32 +02:00
parent bc53a14b1a
commit 95bcb05340

View File

@ -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<OfficeFolder>(req.body);
const officefolderToUpdate = OfficeFolder.hydrate<OfficeFolder>(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<OfficeFolder>(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<OfficeFolder>(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<OfficeFolder>(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<OfficeFolder>(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<OfficeFolder>(officeFolderEntityUpdated, {
strategy: "excludeAll",
});
//success
this.httpSuccess(response, officeFolders);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific folder by uid
* @returns IFolder