99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Get } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import UsersService from "@Services/admin/UsersService/UsersService";
|
|
import { Service } from "typedi";
|
|
import User from "le-coffre-resources/dist/Admin";
|
|
import { Prisma } from "@prisma/client";
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import ruleHandler from "@App/middlewares/RulesHandler";
|
|
import userHandler from "@App/middlewares/OfficeMembershipHandlers/UserHandler";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class UsersController extends ApiController {
|
|
constructor(private usersService: UsersService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all users
|
|
*/
|
|
@Get("/api/v1/admin/users", [authHandler, ruleHandler])
|
|
protected async get(req: Request, response: Response) {
|
|
try {
|
|
//get query
|
|
let query: Prisma.UsersFindManyArgs = {};
|
|
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" } },
|
|
],
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
const officeId: string = req.body.user.office_Id;
|
|
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId };
|
|
if (!query.where) query.where = { office_membership: officeWhereInput };
|
|
query.where.office_membership = officeWhereInput;
|
|
|
|
//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 Get a specific user by uid
|
|
*/
|
|
@Get("/api/v1/admin/users/:uid", [authHandler, ruleHandler, userHandler])
|
|
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 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<User>(userEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, user);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|