146 lines
3.8 KiB
TypeScript
146 lines
3.8 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Get, Post, Put } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import RolesService from "@Services/super-admin/RolesService/RolesService";
|
|
import { Service } from "typedi";
|
|
import { validateOrReject } from "class-validator";
|
|
import { Role } from "le-coffre-resources/dist/Notary";
|
|
import { Roles } from "@prisma/client";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class RolesController extends ApiController {
|
|
constructor(private rolesService: RolesService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all roles
|
|
*/
|
|
@Get("/api/v1/super-admin/roles")
|
|
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 rolesEntities = await this.rolesService.get(query);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const roles = Role.hydrateArray<Role>(rolesEntities, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, roles);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Create a new role
|
|
*/
|
|
@Post("/api/v1/super-admin/roles")
|
|
protected async getAddresses(req: Request, response: Response) {
|
|
try {
|
|
//init IRole resource with request body values
|
|
const roleEntity = Role.hydrate<Role>(req.body);
|
|
|
|
//validate role
|
|
await validateOrReject(roleEntity, { groups: ["createRole"] });
|
|
|
|
//call service to get prisma entity
|
|
const roleEntityCreated = await this.rolesService.create(roleEntity);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const role = Role.hydrate<Role>(roleEntityCreated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpCreated(response, role);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Modify a specific role by uid
|
|
*/
|
|
@Put("/api/v1/super-admin/roles/:uid")
|
|
protected async put(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
const roleFound = await this.rolesService.getByUid(uid);
|
|
|
|
if (!roleFound) {
|
|
this.httpNotFoundRequest(response, "role not found");
|
|
return;
|
|
}
|
|
|
|
//init IRole resource with request body values
|
|
const roleEntity = Role.hydrate<Role>(req.body);
|
|
|
|
//validate role
|
|
await validateOrReject(roleEntity, { groups: ["updateRole"] });
|
|
|
|
//call service to get prisma entity
|
|
const roleEntityUpdated = await this.rolesService.update(roleEntity);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const role = Role.hydrate<Role>(roleEntityUpdated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpSuccess(response, role);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get a specific role by uid
|
|
*/
|
|
@Get("/api/v1/super-admin/roles/:uid")
|
|
protected async getOneByUid(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
let roleEntity: Roles | null;
|
|
//get query
|
|
if (req.query["q"]) {
|
|
const query = JSON.parse(req.query["q"] as string);
|
|
roleEntity = await this.rolesService.getByUid(uid, query);
|
|
} else {
|
|
//call service to get prisma entity
|
|
roleEntity = await this.rolesService.getByUid(uid);
|
|
}
|
|
|
|
if (!roleEntity) {
|
|
this.httpNotFoundRequest(response, "role not found");
|
|
return;
|
|
}
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const role = Role.hydrate<Role>(roleEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, role);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|