Endpoint delete document notary

This commit is contained in:
Vins 2024-09-09 15:10:24 +02:00
parent 8f5974ab59
commit a68d8b817a
2 changed files with 87 additions and 101 deletions

View File

@ -0,0 +1,50 @@
import { Response, Request } from "express";
import { Controller, Get } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi";
import DocumentsNotaryService from "@Services/notary/DocumentsNotaryService/DocumentsNotaryService";
import { Prisma } from "@prisma/client";
import { DocumentNotary } from "le-coffre-resources/dist/Notary";
import authHandler from "@App/middlewares/AuthHandler";
// import NotificationBuilder from "@Common/notifications/NotificationBuilder";
@Controller()
@Service()
export default class DocumentsNotaryController extends ApiController {
constructor(
private documentsNotaryService: DocumentsNotaryService,
) {
super();
}
/**
* @description Get all documents
* @returns IDocument[] list of documents
*/
@Get("/api/v1/customer/documents_notary", [authHandler])
protected async get(req: Request, response: Response) {
try {
//get query
let query: Prisma.DocumentsNotaryFindManyArgs = {};
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
if (query.where?.uid) {
this.httpBadRequest(response, "You can't filter by uid");
return;
}
}
//call service to get prisma entity
const documentEntities = await this.documentsNotaryService.get(query);
//Hydrate ressource with prisma entity
const documents = DocumentNotary.hydrateArray<DocumentNotary>(documentEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, documents);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}

View File

@ -1,9 +1,9 @@
import { Response, Request } from "express";
import { Controller, Get, Post } from "@ControllerPattern/index";
import { Controller, Delete, Get, Post } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi";
import DocumentsNotaryService from "@Services/notary/DocumentsNotaryService/DocumentsNotaryService";
import { Prisma } from "@prisma/client";
import { DocumentsNotary, Prisma } from "@prisma/client";
import { DocumentNotary, FileNotary } from "le-coffre-resources/dist/Notary";
import authHandler from "@App/middlewares/AuthHandler";
import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService";
@ -41,10 +41,6 @@ export default class DocumentsNotaryController extends ApiController {
return;
}
}
const officeId: string = req.body.user.office_Id;
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId };
if (!query.where) query.where = { folder: { office: officeWhereInput } };
query.where.folder!.office = officeWhereInput;
//call service to get prisma entity
const documentEntities = await this.documentsNotaryService.get(query);
@ -96,112 +92,52 @@ export default class DocumentsNotaryController extends ApiController {
const fileEntity = FileNotary.hydrate<FileNotary>(JSON.parse(query));
const fileEntityCreated = await this.filesService.createFileNotary(fileEntity, req.file!);
console.log("fileEntityCreated", fileEntityCreated);
if(!fileEntityCreated) {
this.httpBadRequest(response, "File could not be created");
return;
};
const documentNotary = await this.documentsNotaryService.getByUid(documentNotaryEntityCreated.uid);
// //init Document resource with request body values
// const documentNotaryEntity = DocumentNotary.hydrate<DocumentNotary>(req.body);
// console.log(documentNotaryEntity);
// const folder = await this.officeFoldersService.getByUid(documentNotaryEntity.folder?.uid!, {
// folder_anchor: true,
// });
// if (!folder) {
// this.httpBadRequest(response, "Folder not found");
// return;
// }
// const folderRessource = OfficeFolder.hydrate<OfficeFolder>(folder);
// if (folderRessource.folder_anchor) {
// this.httpBadRequest(response, "Cannot add document on an anchored or anchoring folder");
// return;
// }
// //validate document
// await validateOrReject(documentNotaryEntity, { groups: ["createDocument"], forbidUnknownValues: false });
// //call service to get prisma entity
// const documentNotaryEntityCreated = await this.documentsNotaryService.create(documentNotaryEntity);
// //Hydrate ressource with prisma entity
// const documentNotary = DocumentNotary.hydrate<DocumentNotary>(documentNotaryEntityCreated, {
// strategy: "excludeAll",
// });
// //success
// this.httpCreated(response, documentNotary);
const document = DocumentNotary.hydrate<DocumentNotary>(documentNotary!);
//success
this.httpCreated(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
// /**
// * @description Delete a specific document
// */
// @Delete("/api/v1/notary/documents_notary/:uid", [authHandler, ruleHandler, documentHandler])
// protected async delete(req: Request, response: Response) {
// try {
// const uid = req.params["uid"];
// if (!uid) {
// this.httpBadRequest(response, "No uid provided");
// return;
// }
/**
* @description Delete a specific document
*/
@Delete("/api/v1/notary/documents_notary/:uid", [authHandler])
protected async delete(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
// const documentFound = await this.documentsNotaryService.getByUid(uid);
const documentNotaryFound = await this.documentsNotaryService.getByUid(uid);
// if (!documentFound) {
// this.httpNotFoundRequest(response, "document not found");
// return;
// }
if (!documentNotaryFound) {
this.httpNotFoundRequest(response, "document not found");
return;
}
// //call service to get prisma entity
// const documentEntity: DocumentsNotary = await this.documentsNotaryService.delete(uid);
//call service to get prisma entity
const documentNotaryEntity: DocumentsNotary = await this.documentsNotaryService.delete(uid);
// //Hydrate ressource with prisma entity
// const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
//Hydrate ressource with prisma entity
const documentNotary = DocumentNotary.hydrate<DocumentNotary>(documentNotaryEntity, { strategy: "excludeAll" });
// //success
// this.httpSuccess(response, document);
// } catch (error) {
// this.httpInternalError(response, error);
// return;
// }
// }
// /**
// * @description Get a specific document by uid
// */
// @Get("/api/v1/notary/documents_notary/:uid", [authHandler, ruleHandler, documentHandler])
// protected async getOneByUid(req: Request, response: Response) {
// try {
// const uid = req.params["uid"];
// if (!uid) {
// this.httpBadRequest(response, "No uid provided");
// return;
// }
// //get query
// let query;
// if (req.query["q"]) {
// query = JSON.parse(req.query["q"] as string);
// }
// const documentEntity = await this.documentsNotaryService.getByUid(uid, query);
// if (!documentEntity) {
// this.httpNotFoundRequest(response, "document not found");
// return;
// }
// //Hydrate ressource with prisma entity
// const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
// //success
// this.httpSuccess(response, document);
// } catch (error) {
// this.httpInternalError(response, error);
// return;
// }
// }
//success
this.httpSuccess(response, documentNotary);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}