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/SuperAdmin"; 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 let query; if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); } if (req.query["search"] && typeof req.query["search"] === "string") { const filter = req.query["search"]; query = { where: { contact: { OR: [ { first_name: { contains: filter, mode: "insensitive" } }, { last_name: { contains: filter, mode: "insensitive" } }, ], }, }, }; } //call service to get prisma entity const usersEntities = await this.usersService.get(query); //Hydrate ressource with prisma entity const users = User.hydrateArray(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(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(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, {role: true, votes: true}); if (!userFound) { this.httpNotFoundRequest(response, "user not found"); return; } //init IUser resource with request body values const userEntity = User.hydrate(req.body); const userFoundEntity = User.hydrate(userFound, { strategy: "excludeAll" }); //validate user await validateOrReject(userEntity, { groups: ["updateUser"] }); //call service to get prisma entity const userEntityUpdated = await this.usersService.update(uid, userFoundEntity); //Hydrate ressource with prisma entity const user = User.hydrate(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 query; if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); } const userEntity = await this.usersService.getByUid(uid, query); if (!userEntity) { this.httpNotFoundRequest(response, "user not found"); return; } //Hydrate ressource with prisma entity const user = User.hydrate(userEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, user); } catch (error) { this.httpInternalError(response, error); return; } } }