feat: scope permission in separated customer branch endpoint
This commit is contained in:
parent
1ac03f3f02
commit
390b27716b
1 changed files with 26 additions and 5 deletions
|
|
@ -24,6 +24,15 @@ if (!process.env.MINIO_BUCKET) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
||||||
|
const MANAGE_ROLES = [
|
||||||
|
"system",
|
||||||
|
"head_of_admin",
|
||||||
|
"admin",
|
||||||
|
"branch_admin",
|
||||||
|
"branch_manager",
|
||||||
|
"head_of_sale",
|
||||||
|
"sale",
|
||||||
|
];
|
||||||
|
|
||||||
function imageLocation(id: string) {
|
function imageLocation(id: string) {
|
||||||
return `employee/profile-img-${id}`;
|
return `employee/profile-img-${id}`;
|
||||||
|
|
@ -40,6 +49,7 @@ export type CustomerBranchCreate = {
|
||||||
|
|
||||||
legalPersonNo: string;
|
legalPersonNo: string;
|
||||||
|
|
||||||
|
branchNo: number;
|
||||||
taxNo: string | null;
|
taxNo: string | null;
|
||||||
name: string;
|
name: string;
|
||||||
nameEN: string;
|
nameEN: string;
|
||||||
|
|
@ -105,9 +115,9 @@ export type CustomerBranchUpdate = {
|
||||||
|
|
||||||
@Route("api/v1/customer-branch")
|
@Route("api/v1/customer-branch")
|
||||||
@Tags("Customer Branch")
|
@Tags("Customer Branch")
|
||||||
@Security("keycloak")
|
|
||||||
export class CustomerBranchController extends Controller {
|
export class CustomerBranchController extends Controller {
|
||||||
@Get()
|
@Get()
|
||||||
|
@Security("keycloak")
|
||||||
async list(
|
async list(
|
||||||
@Query() zipCode?: string,
|
@Query() zipCode?: string,
|
||||||
@Query() customerId?: string,
|
@Query() customerId?: string,
|
||||||
|
|
@ -173,6 +183,7 @@ export class CustomerBranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("{branchId}")
|
@Get("{branchId}")
|
||||||
|
@Security("keycloak")
|
||||||
async getById(@Path() branchId: string) {
|
async getById(@Path() branchId: string) {
|
||||||
const record = await prisma.customerBranch.findFirst({
|
const record = await prisma.customerBranch.findFirst({
|
||||||
include: {
|
include: {
|
||||||
|
|
@ -193,6 +204,7 @@ export class CustomerBranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("{branchId}/employee")
|
@Get("{branchId}/employee")
|
||||||
|
@Security("keycloak")
|
||||||
async listEmployee(
|
async listEmployee(
|
||||||
@Path() branchId: string,
|
@Path() branchId: string,
|
||||||
@Query() zipCode?: string,
|
@Query() zipCode?: string,
|
||||||
|
|
@ -245,6 +257,7 @@ export class CustomerBranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
|
@Security("keycloak", MANAGE_ROLES)
|
||||||
async create(@Request() req: RequestWithUser, @Body() body: CustomerBranchCreate) {
|
async create(@Request() req: RequestWithUser, @Body() body: CustomerBranchCreate) {
|
||||||
const [province, district, subDistrict, customer] = await prisma.$transaction([
|
const [province, district, subDistrict, customer] = await prisma.$transaction([
|
||||||
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||||
|
|
@ -281,9 +294,16 @@ export class CustomerBranchController extends Controller {
|
||||||
|
|
||||||
const record = await prisma.$transaction(
|
const record = await prisma.$transaction(
|
||||||
async (tx) => {
|
async (tx) => {
|
||||||
const count = await tx.customerBranch.count({
|
const conflict = await tx.customerBranch.findFirst({
|
||||||
where: { customerId },
|
where: { customerId, branchNo: rest.branchNo },
|
||||||
});
|
});
|
||||||
|
if (conflict) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Branch with current no already exists.",
|
||||||
|
"branchSameNoExist",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return await tx.customerBranch.create({
|
return await tx.customerBranch.create({
|
||||||
include: {
|
include: {
|
||||||
|
|
@ -296,8 +316,7 @@ export class CustomerBranchController extends Controller {
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
statusOrder: +(rest.status === "INACTIVE"),
|
statusOrder: +(rest.status === "INACTIVE"),
|
||||||
branchNo: count + 1,
|
code: `${customer.code}-${rest.branchNo.toString().padStart(2, "0")}`,
|
||||||
code: `${customer.code}-${(count + 1).toString().padStart(2, "0")}`,
|
|
||||||
customer: { connect: { id: customerId } },
|
customer: { connect: { id: customerId } },
|
||||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||||
district: { connect: districtId ? { id: districtId } : undefined },
|
district: { connect: districtId ? { id: districtId } : undefined },
|
||||||
|
|
@ -321,6 +340,7 @@ export class CustomerBranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("{branchId}")
|
@Put("{branchId}")
|
||||||
|
@Security("keycloak", MANAGE_ROLES)
|
||||||
async editById(
|
async editById(
|
||||||
@Request() req: RequestWithUser,
|
@Request() req: RequestWithUser,
|
||||||
@Body() body: CustomerBranchUpdate,
|
@Body() body: CustomerBranchUpdate,
|
||||||
|
|
@ -400,6 +420,7 @@ export class CustomerBranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("{branchId}")
|
@Delete("{branchId}")
|
||||||
|
@Security("keycloak", MANAGE_ROLES)
|
||||||
async delete(@Path() branchId: string) {
|
async delete(@Path() branchId: string) {
|
||||||
const record = await prisma.customerBranch.findFirst({
|
const record = await prisma.customerBranch.findFirst({
|
||||||
where: { id: branchId },
|
where: { id: branchId },
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue