import { Response, Request } from "express"; import { Controller, Get, Post, Put } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import RulesService from "@Services/super-admin/RulesService/RulesService"; import { Service } from "typedi"; import { validateOrReject } from "class-validator"; import { Rule } from "le-coffre-resources/dist/Notary"; import { Rules } from "@prisma/client"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; @Controller() @Service() export default class RulesController extends ApiController { constructor(private rulesService: RulesService) { super(); } /** * @description Get all rules */ @Get("/api/v1/super-admin/rules", [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 rulesEntities = await this.rulesService.get(query); //Hydrate ressource with prisma entity const rules = Rule.hydrateArray(rulesEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, rules); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Create a new rule */ @Post("/api/v1/super-admin/rules", [authHandler, ruleHandler]) protected async getAddresses(req: Request, response: Response) { try { //init IRule resource with request body values const ruleEntity = Rule.hydrate(req.body); //validate rule await validateOrReject(ruleEntity, { groups: ["createRule"] }); //call service to get prisma entity const ruleEntityCreated = await this.rulesService.create(ruleEntity); //Hydrate ressource with prisma entity const rule = Rule.hydrate(ruleEntityCreated, { strategy: "excludeAll", }); //success this.httpCreated(response, rule); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Modify a specific rule by uid */ @Put("/api/v1/super-admin/rules/:uid", [authHandler, ruleHandler]) protected async put(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "No uid provided"); return; } const ruleFound = await this.rulesService.getByUid(uid); if (!ruleFound) { this.httpNotFoundRequest(response, "rule not found"); return; } //init IRule resource with request body values const ruleEntity = Rule.hydrate(req.body); //validate rule await validateOrReject(ruleEntity, { groups: ["updateRule"] }); //call service to get prisma entity const ruleEntityUpdated = await this.rulesService.update(ruleEntity); //Hydrate ressource with prisma entity const rule = Rule.hydrate(ruleEntityUpdated, { strategy: "excludeAll", }); //success this.httpSuccess(response, rule); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Get a specific rule by uid */ @Get("/api/v1/super-admin/rules/: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 ruleEntity: Rules | null; //get query if (req.query["q"]) { const query = JSON.parse(req.query["q"] as string); ruleEntity = await this.rulesService.getByUid(uid, query); } else { //call service to get prisma entity ruleEntity = await this.rulesService.getByUid(uid); } if (!ruleEntity) { this.httpNotFoundRequest(response, "rule not found"); return; } //Hydrate ressource with prisma entity const rule = Rule.hydrate(ruleEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, rule); } catch (error) { this.httpInternalError(response, error); return; } } }