import { Response, Request } from "express"; import { Controller, Delete, Get, Post } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import OfficerRibService from "@Services/common/OfficeRibService/OfficeRibService"; import authHandler from "@App/middlewares/AuthHandler"; import OfficesService from "@Services/notary/OfficesService/OfficesService"; import { Office as OfficeResource } from "le-coffre-resources/dist/Notary"; @Controller() @Service() export default class OfficeRibController extends ApiController { constructor(private officeRibService: OfficerRibService, private officesService: OfficesService) { super(); } @Get("/api/v1/notary/office/rib", [authHandler]) protected async getRibStream(req: Request, response: Response) { const officeId: string = req.body.user.office_Id; if (!officeId) throw new Error("No officeId provided"); const office = await this.officesService.getByUid(officeId, { address: true }); if (!office) { this.httpNotFoundRequest(response, "Office not found"); return; } const fileName = office.rib_name; if (!fileName) { this.httpNotFoundRequest(response, "No file found"); return; } try { const file = await this.officeRibService.getByUid(officeId, fileName!); response.attachment(fileName!); response.send(file.Body); } catch (error) { this.httpInternalError(response, error); } } @Post("/api/v1/notary/office/rib", [authHandler]) protected async post(req: Request, response: Response) { try { const officeId: string = req.body.user.office_Id; if (!req.file) throw new Error("No file provided"); if (!officeId) throw new Error("No officeId provided"); const office = await this.officesService.getByUid(officeId, { address: true }); if (!office) { this.httpNotFoundRequest(response, "office not found"); return; } const fileUrl = await this.officeRibService.createOrUpdate(officeId, req.file); if (!fileUrl || !req.file.originalname) throw new Error("Error while uploading file"); office.rib_url = fileUrl; office.rib_name = req.file.originalname; const officeEntity = OfficeResource.hydrate(office, { strategy: "excludeAll", }); //call service to get prisma entity const officeEntityUpdated = await this.officesService.update(officeId, officeEntity); //Hydrate ressource with prisma entity const officeUpdated = OfficeResource.hydrate(officeEntityUpdated, { strategy: "excludeAll", }); //success this.httpCreated(response, officeUpdated); } catch (error) { this.httpInternalError(response, error); return; } } @Delete("/api/v1/notary/office/rib", [authHandler]) protected async delete(req: Request, response: Response) { try { const officeId: string = req.body.user.office_Id; if (!officeId) throw new Error("No officeId provided"); const office = await this.officesService.getByUid(officeId, { address: true }); if (!office) { this.httpNotFoundRequest(response, "office not found"); return; } const fileName = office.rib_name; await this.officeRibService.delete(officeId, fileName!); office.rib_url = null; office.rib_name = null; const officeEntity = OfficeResource.hydrate(office, { strategy: "excludeAll", }); //call service to get prisma entity const officeEntityUpdated = await this.officesService.update(officeId, officeEntity); //Hydrate ressource with prisma entity const officeUpdated = OfficeResource.hydrate(officeEntityUpdated, { strategy: "excludeAll", }); //success this.httpCreated(response, officeUpdated); } catch (error) { this.httpInternalError(response, error); return; } } }