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 ObjectHydrate from "@Common/helpers/ObjectHydrate"; import { validateOrReject } from "class-validator"; import User from "le-coffre-resources/dist/Notary"; import { Users } from "@prisma/client"; @Controller() @Service() export default class UsersController extends ApiController { constructor(private usersService: UsersService) { super(); } /** * @description Get all users */ @Get("/api/v1/super-admin/users") 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 usersEntity: Users[] = await this.usersService.get(query); //Hydrate ressource with prisma entity const users = User.map(User, usersEntity, {excludeExtraneousValues: true}) //success this.httpSuccess(response, users); } catch (error) { this.httpBadRequest(response, error); return; } } /** * @description Create a new user */ @Post("/api/v1/super-admin/users") protected async getAddresses(req: Request, response: Response) { try { //init IUser resource with request body values const userEntity = new User(); ObjectHydrate.hydrate(userEntity, req.body); //validate user await validateOrReject(userEntity, { groups: ["create"] }); //call service to get prisma entity const prismaEntityCreated = await this.usersService.create(userEntity); //Hydrate ressource with prisma entity const userEntityCreated = ObjectHydrate.hydrate(new User(), prismaEntityCreated, { strategy: "exposeAll", }); //success this.httpSuccess(response, userEntityCreated); } catch (error) { this.httpBadRequest(response, error); return; } } /** * @description Modify a specific user by uid */ @Put("/api/v1/super-admin/users/:uid") protected async put(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { throw new Error("No uid provided"); } //init IUser resource with request body values const userEntity = new User(); ObjectHydrate.hydrate(userEntity, req.body); //validate user await validateOrReject(userEntity, { groups: ["update"] }); //call service to get prisma entity const prismaEntityUpdated = await this.usersService.update(uid, userEntity); //Hydrate ressource with prisma entity const userEntityUpdated = ObjectHydrate.hydrate(new User(), prismaEntityUpdated, { strategy: "exposeAll", }); //success this.httpSuccess(response, userEntityUpdated); } catch (error) { this.httpBadRequest(response, error); return; } } /** * @description Get a specific user by uid */ @Get("/api/v1/super-admin/users/:uid") protected async getOneByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { throw new Error("No uid provided"); } let userEntity: Users; //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); } //Hydrate ressource with prisma entity const user = ObjectHydrate.hydrate(new User(), userEntity, { strategy: "exposeAll" }); //success this.httpSuccess(response, user); } catch (error) { this.httpBadRequest(response, error); return; } } }