feat: customer branch citizen
This commit is contained in:
parent
b12babdc09
commit
a0168ee4fb
4 changed files with 254 additions and 0 deletions
161
src/controllers/03-customer-branch-citizen-controller.ts
Normal file
161
src/controllers/03-customer-branch-citizen-controller.ts
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Middlewares,
|
||||
Path,
|
||||
Post,
|
||||
Put,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
} from "tsoa";
|
||||
import { RequestWithUser } from "../interfaces/user";
|
||||
import prisma from "../db";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import { connectOrDisconnect, connectOrNot } from "../utils/relation";
|
||||
import { notFoundError, relationError } from "../utils/error";
|
||||
import { permissionCheck } from "../middlewares/customer-branch";
|
||||
|
||||
const MANAGE_ROLES = [
|
||||
"system",
|
||||
"head_of_admin",
|
||||
"admin",
|
||||
"head_of_account",
|
||||
"account",
|
||||
"head_of_sale",
|
||||
"sale",
|
||||
];
|
||||
|
||||
function globalAllow(user: RequestWithUser["user"]) {
|
||||
const allowList = ["system", "head_of_admin", "admin", "head_of_account", "head_of_sale"];
|
||||
return allowList.some((v) => user.roles?.includes(v));
|
||||
}
|
||||
|
||||
type CustomerBranchCitizenPayload = {
|
||||
namePrefix?: string;
|
||||
firstName: string;
|
||||
firstNameEN?: string;
|
||||
middleName?: string;
|
||||
middleNameEN?: string;
|
||||
lastName: string;
|
||||
lastNameEN?: string;
|
||||
issueDate: Date;
|
||||
expireDate: Date;
|
||||
nationality: string;
|
||||
religion: string;
|
||||
gender: string;
|
||||
address?: string;
|
||||
addressEN?: string;
|
||||
soi?: string;
|
||||
soiEN?: string;
|
||||
moo?: string;
|
||||
mooEN?: string;
|
||||
street?: string;
|
||||
streetEN?: string;
|
||||
provinceId?: string;
|
||||
districtId?: string;
|
||||
subDistrictId?: string;
|
||||
};
|
||||
|
||||
@Route("api/v1/customer-branch/{branchId}/citizen")
|
||||
@Tags("Customer Branch Citizen")
|
||||
@Middlewares(permissionCheck(globalAllow))
|
||||
export class CustomerBranchCitizenController extends Controller {
|
||||
@Get()
|
||||
@Security("keycloak")
|
||||
async list(@Path() branchId: string) {
|
||||
return prisma.customerBranchCitizen.findMany({
|
||||
orderBy: { createdAt: "asc" },
|
||||
where: { customerBranchId: branchId },
|
||||
});
|
||||
}
|
||||
|
||||
@Get("{citizenId}")
|
||||
@Security("keycloak")
|
||||
async getById(@Path() branchId: string, @Path() citizenId: string) {
|
||||
const record = await prisma.customerBranchCitizen.findFirst({
|
||||
where: { id: citizenId, customerBranchId: branchId },
|
||||
});
|
||||
if (!record) throw notFoundError("Citizen");
|
||||
return record;
|
||||
}
|
||||
|
||||
@Post()
|
||||
@Security("keycloak", MANAGE_ROLES)
|
||||
async create(@Path() branchId: string, @Body() body: CustomerBranchCitizenPayload) {
|
||||
const { provinceId, districtId, subDistrictId, ...rest } = body;
|
||||
const [province, district, subDistrict] = await prisma.$transaction([
|
||||
prisma.province.findUnique({ where: { id: body.provinceId ?? undefined } }),
|
||||
prisma.district.findUnique({ where: { id: body.districtId ?? undefined } }),
|
||||
prisma.subDistrict.findUnique({ where: { id: body.subDistrictId ?? undefined } }),
|
||||
]);
|
||||
if (body.provinceId && !province) throw relationError("Province");
|
||||
if (body.districtId && !district) throw relationError("District");
|
||||
if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict");
|
||||
const record = await prisma.customerBranchCitizen.create({
|
||||
data: {
|
||||
...rest,
|
||||
province: connectOrNot(provinceId),
|
||||
district: connectOrNot(districtId),
|
||||
subDistrict: connectOrNot(subDistrictId),
|
||||
customerBranch: { connect: { id: branchId } },
|
||||
},
|
||||
});
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
@Put("{citizenId}")
|
||||
@Security("keycloak", MANAGE_ROLES)
|
||||
async editById(
|
||||
@Path() branchId: string,
|
||||
@Path() citizenId: string,
|
||||
@Body() body: CustomerBranchCitizenPayload,
|
||||
) {
|
||||
const { provinceId, districtId, subDistrictId, ...rest } = body;
|
||||
const [province, district, subDistrict, citizen] = await prisma.$transaction([
|
||||
prisma.province.findUnique({ where: { id: body.provinceId ?? undefined } }),
|
||||
prisma.district.findUnique({ where: { id: body.districtId ?? undefined } }),
|
||||
prisma.subDistrict.findUnique({ where: { id: body.subDistrictId ?? undefined } }),
|
||||
prisma.customerBranchCitizen.findUnique({
|
||||
where: { id: citizenId, customerBranchId: branchId },
|
||||
}),
|
||||
]);
|
||||
if (body.provinceId && !province) throw relationError("Province");
|
||||
if (body.districtId && !district) throw relationError("District");
|
||||
if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict");
|
||||
if (!citizen) throw notFoundError("Citizen");
|
||||
|
||||
const record = await prisma.customerBranchCitizen.update({
|
||||
where: { id: citizenId, customerBranchId: branchId },
|
||||
data: {
|
||||
...rest,
|
||||
province: connectOrDisconnect(provinceId),
|
||||
district: connectOrDisconnect(districtId),
|
||||
subDistrict: connectOrDisconnect(subDistrictId),
|
||||
},
|
||||
});
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
@Delete("{citizenId}")
|
||||
@Security("keycloak", MANAGE_ROLES)
|
||||
async deleteById(@Path() branchId: string, @Path() citizenId: string) {
|
||||
const record = await prisma.customerBranchCitizen.findFirst({
|
||||
where: { id: citizenId, customerBranchId: branchId },
|
||||
});
|
||||
|
||||
if (!record) throw notFoundError("Citizen");
|
||||
|
||||
return await prisma.customerBranchCitizen.delete({
|
||||
where: { id: citizenId, customerBranchId: branchId },
|
||||
});
|
||||
}
|
||||
}
|
||||
33
src/middlewares/customer-branch.ts
Normal file
33
src/middlewares/customer-branch.ts
Normal 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();
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue