78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Get } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import RulesService from "@Services/notary/RulesService/RulesService";
|
|
import { Service } from "typedi";
|
|
import { Rule } from "le-coffre-resources/dist/Notary";
|
|
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/notary/rules", [authHandler, ruleHandler])
|
|
protected async get(req: Request, response: Response) {
|
|
try {
|
|
//get query
|
|
let query;
|
|
if (req.query["q"]) {
|
|
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<Rule>(rulesEntities, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, rules);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get a specific rule by uid
|
|
*/
|
|
@Get("/api/v1/notary/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;
|
|
}
|
|
//get query
|
|
let query;
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
}
|
|
|
|
const ruleEntity = await this.rulesService.getByUid(uid, query);
|
|
|
|
if (!ruleEntity) {
|
|
this.httpNotFoundRequest(response, "rule not found");
|
|
return;
|
|
}
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const rule = Rule.hydrate<Rule>(ruleEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, rule);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|