lecoffre-back/src/app/api/super-admin/DocumentsController.ts
2023-06-26 10:45:29 +02:00

182 lines
5.3 KiB
TypeScript

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/super-admin/DocumentsService/DocumentsService";
import { Documents } from "@prisma/client";
import { Document } from "le-coffre-resources/dist/SuperAdmin";
import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler";
@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/super-admin/documents", [authHandler,ruleHandler])
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 documentEntities: Documents[] = await this.documentsService.get(query);
//Hydrate ressource with prisma entity
const documents = Document.hydrateArray<Document>(documentEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, documents);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Create a new document
* @returns IDocument created
*/
@Post("/api/v1/super-admin/documents", [authHandler,ruleHandler])
protected async post(req: Request, response: Response) {
try {
//init Document resource with request body values
const documentEntity = Document.hydrate<Document>(req.body);
//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<Document>(documentEntityCreated, {
strategy: "excludeAll",
});
//success
this.httpCreated(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Update a specific document
*/
@Put("/api/v1/super-admin/documents/:uid", [authHandler,ruleHandler])
protected async update(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const documentFound = await this.documentsService.getByUid(uid);
if (!documentFound) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//init Document resource with request body values
const documentEntity = Document.hydrate<Document>(req.body);
//validate document
await validateOrReject(documentEntity, { groups: ["updateDocument"] });
//call service to get prisma entity
const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason);
//Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
//success
this.httpSuccess(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Delete a specific document
*/
@Delete("/api/v1/super-admin/documents/:uid", [authHandler,ruleHandler])
protected async delete(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const documentFound = await this.documentsService.getByUid(uid);
if (!documentFound) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//call service to get prisma entity
const documentEntity: Documents = await this.documentsService.delete(uid);
//Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific document by uid
*/
@Get("/api/v1/super-admin/documents/:uid", [authHandler,ruleHandler])
protected async getOneByUid(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
let documentEntity: Documents | null;
//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);
}
if (!documentEntity) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}