From 00415ac863c8705556b44def500fde8efeff26a9 Mon Sep 17 00:00:00 2001 From: Vins Date: Wed, 4 Dec 2024 15:23:08 +0100 Subject: [PATCH] Add other doc --- src/app/api/customer/DocumentsController.ts | 5 +++-- src/app/api/customer/NotesController.ts | 5 +++-- .../CustomerHandler/NoteHandler.ts | 20 +++++++++++++++++++ .../DocumentsService/DocumentsService.ts | 5 +++-- 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/app/middlewares/CustomerHandler/NoteHandler.ts diff --git a/src/app/api/customer/DocumentsController.ts b/src/app/api/customer/DocumentsController.ts index 0b3f2baf..5a9dcad0 100644 --- a/src/app/api/customer/DocumentsController.ts +++ b/src/app/api/customer/DocumentsController.ts @@ -103,7 +103,7 @@ export default class DocumentsController extends ApiController { protected async post(req: Request, response: Response) { try { //init Document resource with request body values - const documentEntity = Document.hydrate(req.body); + const documentEntity = Document.hydrate(req.body); const email = req.body.user.email; if (!documentEntity.folder?.uid) { @@ -114,6 +114,7 @@ export default class DocumentsController extends ApiController { const folder = await this.officeFoldersService.getByUid(documentEntity.folder.uid, { folder_anchor: true, customers: { include: { contact: true } }, + office: true, }); if (!folder) { this.httpBadRequest(response, "Folder not found"); @@ -138,7 +139,7 @@ export default class DocumentsController extends ApiController { } //call service to get prisma entity - const documentEntityCreated = await this.documentsService.create(documentEntity); + const documentEntityCreated = await this.documentsService.create(documentEntity, folder.office_uid); //Hydrate ressource with prisma entity const document = Document.hydrate(documentEntityCreated, { diff --git a/src/app/api/customer/NotesController.ts b/src/app/api/customer/NotesController.ts index 11d59973..927466bb 100644 --- a/src/app/api/customer/NotesController.ts +++ b/src/app/api/customer/NotesController.ts @@ -6,6 +6,7 @@ import { Prisma } from "@prisma/client"; import authHandler from "@App/middlewares/AuthHandler"; import Note from "le-coffre-resources/dist/Customer/Note"; import NotesService from "@Services/customer/NotesService/NotesService"; +import noteHandler from "@App/middlewares/CustomerHandler/NoteHandler"; @Controller() @Service() @@ -128,7 +129,7 @@ export default class NotesController extends ApiController { /** * @description Create a new note */ - @Post("/api/v1/customer/notes", [authHandler]) + @Post("/api/v1/customer/notes", [authHandler, noteHandler]) protected async post(req: Request, response: Response) { try { //init OfficeFolder resource with request body values @@ -157,7 +158,7 @@ export default class NotesController extends ApiController { /** * @description Modify a specific note by uid */ - @Put("/api/v1/customer/notes/:uid", [authHandler]) + @Put("/api/v1/customer/notes/:uid", [authHandler, noteHandler]) protected async put(req: Request, response: Response) { try { const uid = req.params["uid"]; diff --git a/src/app/middlewares/CustomerHandler/NoteHandler.ts b/src/app/middlewares/CustomerHandler/NoteHandler.ts new file mode 100644 index 00000000..55aaefd9 --- /dev/null +++ b/src/app/middlewares/CustomerHandler/NoteHandler.ts @@ -0,0 +1,20 @@ +import HttpCodes from "@Common/system/controller-pattern/HttpCodes"; +import { NextFunction, Request, Response } from "express"; + +export default async function noteHandler(req: Request, response: Response, next: NextFunction) { + try { + + const content = req.body.content; + + if (content && content.length > 250) { + response.status(HttpCodes.VALIDATION_ERROR).send([{ property: "content", constraints: { content: "La note dépasse 250 caractères" } }]); + return; + } + + next(); + } catch (error) { + console.error(error); + response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error"); + return; + } +} diff --git a/src/services/customer/DocumentsService/DocumentsService.ts b/src/services/customer/DocumentsService/DocumentsService.ts index e999dc46..2da31752 100644 --- a/src/services/customer/DocumentsService/DocumentsService.ts +++ b/src/services/customer/DocumentsService/DocumentsService.ts @@ -23,8 +23,9 @@ export default class DocumentsService extends BaseService { * @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: "Autres documents" } }); + public async create(document: Document, office_uid: string): Promise { + + const otherDocumentType = await this.documentTypeService.get({ where: { name: "Autres documents", office_uid: office_uid } }); if(otherDocumentType.length < 1) throw new Error("Autres documents document type not found");