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 DocumentsService from "@Services/customer/DocumentsService/DocumentsService"; import { Documents, Prisma } from "@prisma/client"; import { Document } from "le-coffre-resources/dist/Customer"; import authHandler from "@App/middlewares/AuthHandler"; import documentHandler from "@App/middlewares/CustomerHandler/DocumentHandler"; import { validateOrReject } from "class-validator"; @Controller() @Service() export default class DocumentsController extends ApiController { constructor(private documentsService: DocumentsService) { super(); } /** * @description Get all documents * @returns IDocument[] list of documents */ @Get("/api/v1/customer/documents", [authHandler]) protected async get(req: Request, response: Response) { try { //get query let query: Prisma.CustomersFindManyArgs = {}; if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); } const customerId: string = req.body.user.customerId; const customerWhereInput: Prisma.DocumentsWhereInput ={ depositor: { uid: customerId } }; query.where = customerWhereInput; //call service to get prisma entity const documentEntities: Documents[] = await this.documentsService.get(query); //Hydrate ressource with prisma entity const documents = Document.hydrateArray(documentEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, documents); } catch (error) { this.httpInternalError(response); return; } } /** * @description Get a specific document by uid */ @Get("/api/v1/customer/documents/:uid",[authHandler,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.documentsService.getByUid(uid, query); if (!documentEntity) { this.httpNotFoundRequest(response, "document not found"); return; } //Hydrate ressource with prisma entity const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, document); } catch (error) { this.httpInternalError(response); return; } } /** * @description Create a new File * @returns File created */ @Post("/api/v1/customer/documents", [authHandler]) protected async post(req: Request, response: Response) { try { //init Document resource with request body values const documentEntity = Document.hydrate(req.body); console.log(documentEntity); //validate document await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); //call service to get prisma entity const documentEntityCreated = await this.documentsService.create(documentEntity); //Hydrate ressource with prisma entity const document = Document.hydrate(documentEntityCreated, { strategy: "excludeAll", }); //success this.httpCreated(response, document); } catch (error) { this.httpInternalError(response, error); return; } } }