feat: double check permission customer branch
This commit is contained in:
parent
023ef7cdae
commit
195c889881
1 changed files with 47 additions and 1 deletions
|
|
@ -473,12 +473,58 @@ export class CustomerBranchController extends Controller {
|
||||||
@Body() body: CustomerBranchUpdate,
|
@Body() body: CustomerBranchUpdate,
|
||||||
@Path() branchId: string,
|
@Path() branchId: string,
|
||||||
) {
|
) {
|
||||||
const branch = await prisma.customerBranch.findUnique({ where: { id: branchId } });
|
const branch = await prisma.customerBranch.findUnique({
|
||||||
|
where: { id: branchId },
|
||||||
|
include: {
|
||||||
|
customer: {
|
||||||
|
include: {
|
||||||
|
registeredBranch: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
where: { userId: req.user.sub },
|
||||||
|
},
|
||||||
|
headOffice: {
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
where: { userId: req.user.sub },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (!branch) {
|
if (!branch) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isSystem(req.user)) {
|
||||||
|
const _branch = branch.customer.registeredBranch;
|
||||||
|
const affilationBranch = _branch && _branch.user.length !== 0;
|
||||||
|
const affilationHeadBranch =
|
||||||
|
_branch && _branch.headOffice && _branch.headOffice.user.length !== 0;
|
||||||
|
if (!globalAllow(req.user)) {
|
||||||
|
if (!affilationBranch) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.FORBIDDEN,
|
||||||
|
"You do not have permission to perform this action.",
|
||||||
|
"noPermission",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!affilationBranch && !affilationHeadBranch) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.FORBIDDEN,
|
||||||
|
"You do not have permission to perform this action.",
|
||||||
|
"noPermission",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!body.customerId) body.customerId = branch.customerId;
|
if (!body.customerId) body.customerId = branch.customerId;
|
||||||
|
|
||||||
if (body.provinceId || body.districtId || body.subDistrictId || body.customerId) {
|
if (body.provinceId || body.districtId || body.subDistrictId || body.customerId) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue