diff --git a/src/app/api/customer/OfficeRibController.ts b/src/app/api/customer/OfficeRibController.ts new file mode 100644 index 00000000..f11538c8 --- /dev/null +++ b/src/app/api/customer/OfficeRibController.ts @@ -0,0 +1,43 @@ +import { Response, Request } from "express"; +import { Controller, Get } 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/customer/OfficesService/OfficesService"; + +@Controller() +@Service() +export default class OfficeRibController extends ApiController { + constructor(private officeRibService: OfficerRibService, private officesService: OfficesService) { + super(); + } + + @Get("/api/v1/customer/office/:uid/rib", [authHandler]) + protected async getRibStream(req: Request, response: Response) { + const officeId = req.params["uid"]; + 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); + } + } +} diff --git a/src/app/api/notary/BucketController.ts b/src/app/api/notary/OfficeRibController.ts similarity index 72% rename from src/app/api/notary/BucketController.ts rename to src/app/api/notary/OfficeRibController.ts index 555e6d5d..d7330ad3 100644 --- a/src/app/api/notary/BucketController.ts +++ b/src/app/api/notary/OfficeRibController.ts @@ -3,41 +3,38 @@ import { Controller, Delete, Get, Post } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; -import BucketService from "@Services/common/BucketService/BucketService"; +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 BucketController extends ApiController { - constructor(private bucketService: BucketService, private officesService: OfficesService) { +export default class OfficeRibController extends ApiController { + constructor(private officeRibService: OfficerRibService, private officesService: OfficesService) { super(); } - @Get("/api/v1/notary/bucket/:uid", [authHandler]) + @Get("/api/v1/notary/office/rib", [authHandler]) protected async getRibStream(req: Request, response: Response) { - const uid = req.params["uid"]; - if (!uid) { - this.httpBadRequest(response, "No uid provided"); - return; - } + const officeId: string = req.body.user.office_Id; + if (!officeId) throw new Error("No officeId provided"); - const office = await this.officesService.getByUid(uid, {address: true}); - - if(!office) { + 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) { + if (!fileName) { this.httpNotFoundRequest(response, "No file found"); return; } try { - const file = await this.bucketService.getByUid(uid, fileName!); + const file = await this.officeRibService.getByUid(officeId, fileName!); response.attachment(fileName!); response.send(file.Body); } catch (error) { @@ -45,22 +42,22 @@ export default class BucketController extends ApiController { } } - @Post("/api/v1/notary/bucket", [authHandler]) + @Post("/api/v1/notary/office/rib", [authHandler]) protected async post(req: Request, response: Response) { try { - const officeId: string = req.body.user.office_Id + 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}); + const office = await this.officesService.getByUid(officeId, { address: true }); if (!office) { this.httpNotFoundRequest(response, "office not found"); return; } - const fileUrl = await this.bucketService.createOrUpdate(officeId, req.file); + const fileUrl = await this.officeRibService.createOrUpdate(officeId, req.file); - if(!fileUrl || !req.file.originalname ) throw new Error("Error while uploading file"); + if (!fileUrl || !req.file.originalname) throw new Error("Error while uploading file"); office.rib_url = fileUrl; office.rib_name = req.file.originalname; @@ -84,15 +81,13 @@ export default class BucketController extends ApiController { } } - @Delete("/api/v1/notary/bucket/:uid", [authHandler]) + @Delete("/api/v1/notary/office/rib", [authHandler]) protected async delete(req: Request, response: Response) { try { - const officeId: string = req.body.user.office_Id + 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}); - console.log(office); - + const office = await this.officesService.getByUid(officeId, { address: true }); if (!office) { this.httpNotFoundRequest(response, "office not found"); @@ -101,7 +96,7 @@ export default class BucketController extends ApiController { const fileName = office.rib_name; - await this.bucketService.delete(officeId, fileName!); + await this.officeRibService.delete(officeId, fileName!); office.rib_url = null; office.rib_name = null; diff --git a/src/app/api/notary/OfficesController.ts b/src/app/api/notary/OfficesController.ts index d738ca99..d7dac131 100644 --- a/src/app/api/notary/OfficesController.ts +++ b/src/app/api/notary/OfficesController.ts @@ -7,6 +7,7 @@ import { Offices, Prisma } from "@prisma/client"; import { Office as OfficeResource } from "le-coffre-resources/dist/Notary"; import ruleHandler from "@App/middlewares/RulesHandler"; import authHandler from "@App/middlewares/AuthHandler"; +import uuidMatcher from "@App/uuidMatcher"; @Controller() @Service() @@ -49,8 +50,9 @@ export default class OfficesController extends ApiController { /** * @description Get a specific office by uid + * `/:id(${uuidMatcher})` */ - @Get("/api/v1/notary/offices/:uid", [authHandler, ruleHandler]) + @Get(`/api/v1/notary/offices/:uid(${uuidMatcher})`, [authHandler, ruleHandler]) protected async getOneByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; diff --git a/src/app/index.ts b/src/app/index.ts index 3646fda0..510d903d 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -46,7 +46,8 @@ import DocumentControllerId360 from "./api/id360/DocumentController"; import CustomerControllerId360 from "./api/id360/CustomerController"; import UserNotificationController from "./api/notary/UserNotificationController"; import AuthController from "./api/customer/AuthController"; -import BucketController from "./api/notary/BucketController"; +import NotaryOfficeRibController from "./api/notary/OfficeRibController"; +import CustomerOfficeRibController from "./api/customer/OfficeRibController"; /** * @description This allow to declare all controllers used in the application @@ -101,6 +102,7 @@ export default { Container.get(DocumentControllerId360); Container.get(CustomerControllerId360); Container.get(AuthController); - Container.get(BucketController); + Container.get(NotaryOfficeRibController); + Container.get(CustomerOfficeRibController); }, }; diff --git a/src/app/uuidMatcher.ts b/src/app/uuidMatcher.ts new file mode 100644 index 00000000..558fc1f3 --- /dev/null +++ b/src/app/uuidMatcher.ts @@ -0,0 +1,2 @@ +const uuidMatcher = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}"; +export default uuidMatcher; \ No newline at end of file diff --git a/src/services/common/BucketService/BucketService.ts b/src/services/common/OfficeRibService/OfficeRibService.ts similarity index 95% rename from src/services/common/BucketService/BucketService.ts rename to src/services/common/OfficeRibService/OfficeRibService.ts index ff29737a..535125bc 100644 --- a/src/services/common/BucketService/BucketService.ts +++ b/src/services/common/OfficeRibService/OfficeRibService.ts @@ -14,18 +14,15 @@ const s3 = new AWS.S3({ }); @Service() -export default class BucketService extends BaseService { +export default class OfficerRibService extends BaseService { constructor(private variables: BackendVariables) { super(); } - public async download() { - - } - public async getByUid(uid: string, fileName: string) { const key = path.join(this.variables.ENV, uid, fileName); + return new Promise(async (resolve, reject) => { s3.getObject( { diff --git a/src/services/customer/OfficesService/OfficesService.ts b/src/services/customer/OfficesService/OfficesService.ts new file mode 100644 index 00000000..8c7091d2 --- /dev/null +++ b/src/services/customer/OfficesService/OfficesService.ts @@ -0,0 +1,28 @@ +import {Prisma } from "@prisma/client"; +import OfficesRepository from "@Repositories/OfficesRepository"; +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; + + +@Service() +export default class OfficesService extends BaseService { + constructor(private officeRepository: OfficesRepository) { + super(); + } + + /** + * @description : Get all offices + * @throws {Error} If offices cannot be get + */ + public async get(query: Prisma.OfficesFindManyArgs) { + return this.officeRepository.findMany(query); + } + + /** + * @description : Get a office by uid + * @throws {Error} If office cannot be get + */ + public async getByUid(uid: string, query?: Prisma.OfficesInclude) { + return this.officeRepository.findOneByUid(uid, query); + } +}