39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import HttpCodes from "@Common/system/controller-pattern/HttpCodes";
|
|
import { NextFunction, Request, Response } from "express";
|
|
import Container from "typedi";
|
|
import UsersService from "@Services/super-admin/UsersService/UsersService";
|
|
|
|
export default async function userHandler(req: Request, response: Response, next: NextFunction) {
|
|
try {
|
|
const officeId = req.body.user.office_Id;
|
|
const uid = req.path && req.path.split("/")[5];
|
|
const office = req.body.office_membership;
|
|
|
|
if (office && office.uid != officeId) {
|
|
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office");
|
|
return;
|
|
}
|
|
|
|
if (uid) {
|
|
const userService = Container.get(UsersService);
|
|
const user = await userService.getByUidWithOffice(uid!);
|
|
|
|
if (!user) {
|
|
response.status(HttpCodes.NOT_FOUND).send("User not found");
|
|
return;
|
|
}
|
|
|
|
if (user.office_membership.uid != officeId) {
|
|
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office");
|
|
return;
|
|
}
|
|
}
|
|
|
|
next();
|
|
} catch (error) {
|
|
console.error(error);
|
|
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
|
|
return;
|
|
}
|
|
}
|