feat: customer branch citizen

This commit is contained in:
Methapon Metanipat 2024-09-17 10:57:42 +07:00
parent b12babdc09
commit a0168ee4fb
4 changed files with 254 additions and 0 deletions

View file

@ -0,0 +1,33 @@
import express from "express";
import { RequestWithUser } from "../interfaces/user";
import prisma from "../db";
import { branchRelationPermInclude, createPermCheck } from "../services/permission";
import { notFoundError } from "../utils/error";
export function permissionCheck(globalAllow: (user: RequestWithUser["user"]) => boolean) {
const checker = createPermCheck(globalAllow);
return async (req: RequestWithUser, _res: express.Response, next: express.NextFunction) => {
if ("employeeId" in req.params && typeof req.params.employeeId === "string") {
const id = req.params.customerBranchId;
const employee = await prisma.customerBranch.findFirst({
where: { id },
include: {
customer: {
include: {
registeredBranch: {
include: branchRelationPermInclude(req.user),
},
},
},
},
});
if (!employee) throw notFoundError("Customer Branch");
await checker(req.user, employee.customer.registeredBranch);
}
next();
};
}