From 8afdac3228506dca366ca0b46164b696a441f0ab Mon Sep 17 00:00:00 2001 From: Vins Date: Wed, 6 Sep 2023 18:07:30 +0200 Subject: [PATCH] Done --- src/app/api/customer/DocumentsController.ts | 34 ++++++++++- src/app/api/customer/FilesController.ts | 56 ++++++++++++++++++- .../repositories/DocumentsRepository.ts | 2 +- .../DocumentsService/DocumentsService.ts | 18 +++++- 4 files changed, 105 insertions(+), 5 deletions(-) diff --git a/src/app/api/customer/DocumentsController.ts b/src/app/api/customer/DocumentsController.ts index fdfb16f2..5593e310 100644 --- a/src/app/api/customer/DocumentsController.ts +++ b/src/app/api/customer/DocumentsController.ts @@ -1,5 +1,5 @@ import { Response, Request } from "express"; -import { Controller, Get } from "@ControllerPattern/index"; +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"; @@ -7,6 +7,7 @@ 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() @@ -85,4 +86,35 @@ export default class DocumentsController extends ApiController { 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; + } + } } diff --git a/src/app/api/customer/FilesController.ts b/src/app/api/customer/FilesController.ts index 76dd93ee..d3eb0338 100644 --- a/src/app/api/customer/FilesController.ts +++ b/src/app/api/customer/FilesController.ts @@ -10,11 +10,14 @@ import { validateOrReject } from "class-validator"; import DocumentsService from "@Services/customer/DocumentsService/DocumentsService"; import authHandler from "@App/middlewares/AuthHandler"; import fileHandler from "@App/middlewares/CustomerHandler/FileHandler"; +import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService"; +import { DocumentType } from "le-coffre-resources/dist/SuperAdmin"; +import ObjectHydrate from "@Common/helpers/ObjectHydrate"; @Controller() @Service() export default class FilesController extends ApiController { - constructor(private filesService: FilesService, private documentService: DocumentsService) { + constructor(private filesService: FilesService, private documentService: DocumentsService, private documentTypesService : DocumentTypesService) { super(); } @@ -87,9 +90,10 @@ export default class FilesController extends ApiController { //init File resource with request body values const fileEntity = File.hydrate(JSON.parse(req.body["q"])); + console.log(fileEntity); //validate File - await validateOrReject(fileEntity, { groups: ["createFile"] }); + // await validateOrReject(fileEntity, { groups: ["createFile"] }); //call service to get prisma entity const fileEntityCreated = await this.filesService.create(fileEntity, req.file); @@ -224,4 +228,52 @@ export default class FilesController extends ApiController { return; } } + + /** + * @description Create a new File + * @returns File created + */ + @Post("/api/v1/customer/addPersonalFile", [authHandler, fileHandler]) + protected async addPersonalFile(req: Request, response: Response) { + try { + //get file + if (!req.file) throw new Error("No file provided"); + + //init File resource with request body values + const fileEntity = File.hydrate(JSON.parse(req.body["q"])); + + const documentTypeEntities = await this.documentTypesService.get({ where: { name: "Other"} }); + const documentTypeEntity = documentTypeEntities[0]; + const documentType = ObjectHydrate.hydrate(new DocumentType(), documentTypeEntity!, { strategy: "excludeAll" }); + + const documentEntity = Document.hydrate({document_type: documentType}); + await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false }); + const documentEntityCreated = await this.documentService.create(documentEntity); + + const document = Document.hydrate(documentEntityCreated, { + strategy: "excludeAll", + }); + + fileEntity.document = document; + + const fileEntityCreated = await this.filesService.create(fileEntity, req.file); + + const documentToUpdate = Document.hydrate(document!); + + documentToUpdate!.document_status = "DEPOSITED"; + await this.documentService.update(document!.uid!, documentToUpdate); + + //Hydrate ressource with prisma entity + const fileEntityHydrated = File.hydrate(fileEntityCreated, { + strategy: "excludeAll", + }); + + //success + this.httpCreated(response, fileEntityHydrated); + + } catch (error) { + this.httpBadRequest(response, error); + return; + } + } } diff --git a/src/common/repositories/DocumentsRepository.ts b/src/common/repositories/DocumentsRepository.ts index c5110e6d..6a86cdd8 100644 --- a/src/common/repositories/DocumentsRepository.ts +++ b/src/common/repositories/DocumentsRepository.ts @@ -28,7 +28,7 @@ export default class DocumentsRepository extends BaseRepository { /** * @description : Create a document */ - public async create(document: Document): Promise { + public async create(document: DocumentCustomer): Promise { const createArgs: Prisma.DocumentsCreateArgs = { data: { folder: { diff --git a/src/services/customer/DocumentsService/DocumentsService.ts b/src/services/customer/DocumentsService/DocumentsService.ts index 26c45c6a..fb6d925c 100644 --- a/src/services/customer/DocumentsService/DocumentsService.ts +++ b/src/services/customer/DocumentsService/DocumentsService.ts @@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/Customer"; import DocumentsRepository from "@Repositories/DocumentsRepository"; import BaseService from "@Services/BaseService"; import { Service } from "typedi"; +import DocumentTypesService from "@Services/notary/DocumentTypesService/DocumentTypesService"; @Service() export default class DocumentsService extends BaseService { - constructor(private documentsRepository: DocumentsRepository) { + constructor(private documentsRepository: DocumentsRepository, private documentTypeService: DocumentTypesService) { super(); } @@ -18,6 +19,21 @@ export default class DocumentsService extends BaseService { return this.documentsRepository.findMany(query); } + /** + * @description : Create a new document + * @throws {Error} If document cannot be created + */ + public async create(document: Document): Promise { + const otherDocumentType = await this.documentTypeService.get({ where: { name: "Other" } }); + + if(otherDocumentType.length < 1) throw new Error("Other document type not found"); + + document.document_type = otherDocumentType[0]; + document.document_status = "DEPOSITED"; + + return this.documentsRepository.create(document); + } + /** * @description : Modify a document * @throws {Error} If document cannot be modified