lecoffre-back/src/app/api/customer/NotesController.ts
2024-12-04 15:23:08 +01:00

204 lines
5.7 KiB
TypeScript

import { Response, Request } from "express";
import { Controller, Get, Post, Put } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi";
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()
export default class NotesController extends ApiController {
constructor(private notesService: NotesService) {
super();
}
/**
* @description Get all Notes
* @returns Note[] list of Notes
*/
@Get("/api/v1/customer/notes", [authHandler])
protected async get(req: Request, response: Response) {
try {
//get query
let query: Prisma.NotesFindManyArgs = {};
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
if (query.where?.uid) {
this.httpBadRequest(response, "You can't filter by uid");
return;
}
}
//call service to get prisma entity
const noteEntities = await this.notesService.get(query);
//Hydrate ressource with prisma entity
const notes = Note.hydrateArray<Note>(noteEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, notes);
} catch (error) {
this.httpBadRequest(response, error);
return;
}
}
/**
* @description Get a specific customer note
*/
@Get("/api/v1/customer/notes/customer/:customer-uid/folders/:folder-uid/", [authHandler])
protected async getCustomerNoteByFolderUid(req: Request, response: Response) {
try {
const customerUid = req.params["customer-uid"];
const folderUid = req.params["folder-uid"];
if (!customerUid || !folderUid) {
this.httpBadRequest(response, "No uid provided");
return;
}
//get query
const query: Prisma.NotesFindManyArgs = {
where: {
customer: {
uid: customerUid
},
folder: {
uid: folderUid
}
}
};
//call service to get prisma entity
const noteEntities = await this.notesService.get(query);
if(noteEntities.length === 0) {
this.httpNotFoundRequest(response, "No notes found");
return;
}
//Hydrate ressource with prisma entity
const notes = Note.hydrateArray<Note>(noteEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, notes);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific note by uid
*/
@Get("/api/v1/customer/notes/:uid", [authHandler])
protected async getOneByUid(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
//get query
let query;
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
const noteEntity = await this.notesService.getByUid(uid, query);
if (!noteEntity) {
this.httpNotFoundRequest(response, "note not found");
return;
}
//Hydrate ressource with prisma entity
const note = Note.hydrate<Note>(noteEntity);
//success
this.httpSuccess(response, note);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Create a new note
*/
@Post("/api/v1/customer/notes", [authHandler, noteHandler])
protected async post(req: Request, response: Response) {
try {
//init OfficeFolder resource with request body values
const noteRessource = Note.hydrate<Note>(req.body);
// noteRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false });
//call service to get prisma entity
try {
const noteEntity = await this.notesService.create(noteRessource);
//Hydrate ressource with prisma entity
const note = Note.hydrate<Note>(noteEntity, {
strategy: "excludeAll",
});
//success
this.httpCreated(response, note);
} catch (error) {
this.httpValidationError(response, error);
return;
}
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Modify a specific note by uid
*/
@Put("/api/v1/customer/notes/:uid", [authHandler, noteHandler])
protected async put(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const noteFound = await this.notesService.getByUid(uid);
if (!noteFound) {
this.httpNotFoundRequest(response, "office folder not found");
return;
}
//init OfficeFolder resource with request body values
const noteEntity = Note.hydrate<Note>(req.body);
//validate folder
//await validateOrReject(noteEntity, { groups: ["updateFolder"], forbidUnknownValues: false });
//call service to get prisma entity
try {
const noteEntityUpdated = await this.notesService.update(uid, noteEntity);
//Hydrate ressource with prisma entity
const note = Note.hydrate<Note>(noteEntityUpdated, {
strategy: "excludeAll",
});
//success
this.httpSuccess(response, note);
} catch (error) {
this.httpValidationError(response, error);
return;
}
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}