diff --git a/src/app/api/customer/DocumentsController.ts b/src/app/api/customer/DocumentsController.ts new file mode 100644 index 00000000..2ed3aa64 --- /dev/null +++ b/src/app/api/customer/DocumentsController.ts @@ -0,0 +1,161 @@ +import { Response, Request } from "express"; +import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import DocumentsService from "@Services/customer/DocumentsService/DocumentsService"; +import { Documents } from "@prisma/client"; +import ObjectHydrate from "@Common/helpers/ObjectHydrate"; +import { Document } from "le-coffre-resources/dist/Customer"; +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") + protected async get(req: Request, response: Response) { + try { + //get query + const query = JSON.parse(req.query["q"] as string); + + + //call service to get prisma entity + const prismaEntity: Documents[] = await this.documentsService.get(query); + + //Hydrate ressource with prisma entity + const documents = ObjectHydrate.map(Document, prismaEntity, { strategy: "exposeAll" }); + + //success + this.httpSuccess(response, documents); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + + /** + * @description Create a new document + * @returns IDocument created + */ + @Post("/api/v1/customer/documents") + protected async post(req: Request, response: Response) { + try { + //init Document resource with request body values + const documentEntity = new Document(); + ObjectHydrate.hydrate(documentEntity, req.body); + + //validate document + await validateOrReject(documentEntity, { groups: ["create"] }); + + //call service to get prisma entity + const prismaEntityCreated = await this.documentsService.create(documentEntity); + + //Hydrate ressource with prisma entity + const documentEntityCreated = ObjectHydrate.hydrate(new Document(), prismaEntityCreated, { + strategy: "exposeAll", + }); + + //success + this.httpSuccess(response, documentEntityCreated); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + + /** + * @description Update a specific document + */ + @Put("/api/v1/customer/documents/:uid") + protected async update(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + throw new Error("No uid provided"); + } + + //init Document resource with request body values + const documentEntity = new Document(); + ObjectHydrate.hydrate(documentEntity, req.body); + + //validate document + await validateOrReject(documentEntity, { groups: ["create"] }); + + //call service to get prisma entity + const prismaEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity); + + //Hydrate ressource with prisma entity + const document = ObjectHydrate.hydrate(new Document(), prismaEntityUpdated, { strategy: "exposeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + + /** + * @description Delete a specific document + */ + @Delete("/api/v1/customer/documents/:uid") + protected async delete(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + throw new Error("No uid provided"); + } + + //call service to get prisma entity + const documentEntity: Documents = await this.documentsService.delete(uid); + + //Hydrate ressource with prisma entity + const document = ObjectHydrate.hydrate(new Document(), documentEntity, { strategy: "exposeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } + + /** + * @description Get a specific document by uid + */ + @Get("/api/v1/customer/documents/:uid") + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + throw new Error("No uid provided"); + } + + let documentEntity: Documents; + //get query + if (req.query["q"]) { + const query = JSON.parse(req.query["q"] as string); + documentEntity = await this.documentsService.getByUid(uid, query); + } else { + //call service to get prisma entity + documentEntity = await this.documentsService.getByUid(uid); + } + + //Hydrate ressource with prisma entity + const document = ObjectHydrate.hydrate(new Document(), documentEntity, { strategy: "exposeAll" }); + + //success + this.httpSuccess(response, document); + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } +} diff --git a/src/app/index.ts b/src/app/index.ts index d81c7281..01e173ed 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -10,6 +10,9 @@ import DocumentsController from "./api/super-admin/DocumentsController"; import DocumentTypesController from "./api/super-admin/DocumentTypesController"; import IdNotUserInfoController from "./api/idnot-user/UserInfoController"; +import DocumentsControllerCustomer from "./api/customer/DocumentsController"; + + /** * @description This allow to declare all controllers used in the application */ @@ -25,5 +28,7 @@ export default { Container.get(DocumentsController); Container.get(DocumentTypesController); Container.get(IdNotUserInfoController); + + Container.get(DocumentsControllerCustomer); }, }; diff --git a/src/services/customer/DocumentsService/DocumentsService.ts b/src/services/customer/DocumentsService/DocumentsService.ts new file mode 100644 index 00000000..b5d470e0 --- /dev/null +++ b/src/services/customer/DocumentsService/DocumentsService.ts @@ -0,0 +1,60 @@ +import { Documents, Prisma } from "@prisma/client"; +import { Document } from "le-coffre-resources/dist/Customer"; +import DocumentsRepository from "@Repositories/DocumentsRepository"; +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; + +@Service() +export default class DocumentsService extends BaseService { + constructor(private documentsRepository: DocumentsRepository) { + super(); + } + + /** + * @description : Get all documents + * @throws {Error} If documents cannot be get + */ + public async get(query: any) { + return this.documentsRepository.findMany(query); + } + + /** + * @description : Create a new document + * @throws {Error} If document cannot be created + */ + public async create(document: Document): Promise { + return this.documentsRepository.create(document); + } + + /** + * @description : Create new documents + * @throws {Error} If documents or one of them cannot be created + */ + public async createMany(documents: Document[]): Promise { + return this.documentsRepository.createMany(documents); + } + + /** + * @description : Modify a document + * @throws {Error} If document cannot be modified + */ + public async update(uid: string, document: Document): Promise { + return this.documentsRepository.update(uid, document); + } + + /** + * @description : Delete a document + * @throws {Error} If document cannot be deleted + */ + public async delete(uid: string): Promise { + return this.documentsRepository.delete(uid); + } + + /** + * @description : Get a document by uid + * @throws {Error} If document cannot be get by uid + */ + public async getByUid(uid: string, query?: any) { + return this.documentsRepository.findOneByUid(uid, query); + } +}