148 lines
4.0 KiB
TypeScript
148 lines
4.0 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Get, Post, Put } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import UsersService from "@Services/super-admin/UsersService/UsersService";
|
|
import { Service } from "typedi";
|
|
import { validateOrReject } from "class-validator";
|
|
import User from "le-coffre-resources/dist/Notary";
|
|
import { Users } from "@prisma/client";
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import ruleHandler from "@App/middlewares/RulesHandler";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class UsersController extends ApiController {
|
|
constructor(private usersService: UsersService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all users
|
|
*/
|
|
@Get("/api/v1/super-admin/users", [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 usersEntities = await this.usersService.get(query);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const users = User.hydrateArray<User>(usersEntities, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, users);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Create a new user
|
|
*/
|
|
@Post("/api/v1/super-admin/users", [authHandler, ruleHandler])
|
|
protected async create(req: Request, response: Response) {
|
|
try {
|
|
//init IUser resource with request body values
|
|
const userEntity = User.hydrate<User>(req.body);
|
|
|
|
//validate user
|
|
await validateOrReject(userEntity, { groups: ["createUser"] });
|
|
|
|
//call service to get prisma entity
|
|
const userEntityCreated = await this.usersService.create(userEntity);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const user = User.hydrate<User>(userEntityCreated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpCreated(response, user);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Modify a specific user by uid
|
|
*/
|
|
@Put("/api/v1/super-admin/users/: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 userFound = await this.usersService.getByUid(uid);
|
|
|
|
if (!userFound) {
|
|
this.httpNotFoundRequest(response, "user not found");
|
|
return;
|
|
}
|
|
|
|
//init IUser resource with request body values
|
|
const userEntity = User.hydrate<User>(req.body);
|
|
|
|
//validate user
|
|
await validateOrReject(userEntity, { groups: ["update"] });
|
|
|
|
//call service to get prisma entity
|
|
const userEntityUpdated = await this.usersService.update(uid, userEntity);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const user = User.hydrate<User>(userEntityUpdated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpSuccess(response, user);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get a specific user by uid
|
|
*/
|
|
@Get("/api/v1/super-admin/users/: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 userEntity: Users | null;
|
|
//get query
|
|
if (req.query["q"]) {
|
|
const query = JSON.parse(req.query["q"] as string);
|
|
userEntity = await this.usersService.getByUid(uid, query);
|
|
} else {
|
|
//call service to get prisma entity
|
|
userEntity = await this.usersService.getByUid(uid);
|
|
}
|
|
|
|
if (!userEntity) {
|
|
this.httpNotFoundRequest(response, "user not found");
|
|
return;
|
|
}
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const user = User.hydrate<User>(userEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, user);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|