2024-04-05 11:37:40 +07:00
|
|
|
import { Prisma, Status } from "@prisma/client";
|
|
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import { RequestWithUser } from "../interfaces/user";
|
|
|
|
|
import prisma from "../db";
|
|
|
|
|
import HttpStatus from "../interfaces/http-status";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import minio from "../services/minio";
|
|
|
|
|
|
|
|
|
|
if (!process.env.MINIO_BUCKET) {
|
|
|
|
|
throw Error("Require MinIO bucket.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
2024-07-03 17:51:48 +07:00
|
|
|
const MANAGE_ROLES = [
|
|
|
|
|
"system",
|
|
|
|
|
"head_of_admin",
|
|
|
|
|
"admin",
|
|
|
|
|
"branch_admin",
|
|
|
|
|
"branch_manager",
|
|
|
|
|
"head_of_sale",
|
|
|
|
|
"sale",
|
|
|
|
|
];
|
2024-04-05 11:37:40 +07:00
|
|
|
|
|
|
|
|
function imageLocation(id: string) {
|
|
|
|
|
return `employee/profile-img-${id}`;
|
|
|
|
|
}
|
2024-04-05 11:42:03 +07:00
|
|
|
|
2024-06-07 10:44:40 +07:00
|
|
|
function attachmentLocation(customerId: string, branchId: string) {
|
|
|
|
|
return `customer/${customerId}/branch/${branchId}`;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 16:17:50 +07:00
|
|
|
export type CustomerBranchCreate = {
|
2024-04-05 11:42:03 +07:00
|
|
|
customerId: string;
|
|
|
|
|
|
|
|
|
|
status?: Status;
|
|
|
|
|
|
|
|
|
|
legalPersonNo: string;
|
|
|
|
|
|
2024-07-03 17:51:48 +07:00
|
|
|
branchNo: number;
|
2024-04-24 10:02:37 +07:00
|
|
|
taxNo: string | null;
|
2024-04-05 11:42:03 +07:00
|
|
|
name: string;
|
|
|
|
|
nameEN: string;
|
|
|
|
|
addressEN: string;
|
|
|
|
|
address: string;
|
|
|
|
|
zipCode: string;
|
|
|
|
|
email: string;
|
|
|
|
|
telephoneNo: string;
|
|
|
|
|
|
|
|
|
|
registerName: string;
|
|
|
|
|
registerDate: Date;
|
|
|
|
|
authorizedCapital: string;
|
|
|
|
|
|
2024-04-23 18:09:08 +07:00
|
|
|
employmentOffice: string;
|
|
|
|
|
bussinessType: string;
|
2024-04-24 10:02:37 +07:00
|
|
|
bussinessTypeEN: string;
|
2024-04-23 18:09:08 +07:00
|
|
|
jobPosition: string;
|
2024-04-24 10:02:37 +07:00
|
|
|
jobPositionEN: string;
|
2024-04-23 18:09:08 +07:00
|
|
|
jobDescription: string;
|
|
|
|
|
saleEmployee: string;
|
2024-06-07 13:54:02 +07:00
|
|
|
payDate: Date;
|
2024-06-07 14:02:31 +07:00
|
|
|
wageRate: number;
|
2024-04-23 18:09:08 +07:00
|
|
|
|
2024-04-05 11:42:03 +07:00
|
|
|
subDistrictId?: string | null;
|
|
|
|
|
districtId?: string | null;
|
|
|
|
|
provinceId?: string | null;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-22 16:17:50 +07:00
|
|
|
export type CustomerBranchUpdate = {
|
2024-04-05 11:42:03 +07:00
|
|
|
customerId?: string;
|
|
|
|
|
|
|
|
|
|
status?: "ACTIVE" | "INACTIVE";
|
|
|
|
|
|
|
|
|
|
legalPersonNo?: string;
|
|
|
|
|
|
2024-04-24 10:02:37 +07:00
|
|
|
taxNo?: string | null;
|
2024-04-05 11:42:03 +07:00
|
|
|
name?: string;
|
|
|
|
|
nameEN?: string;
|
|
|
|
|
addressEN?: string;
|
|
|
|
|
address?: string;
|
|
|
|
|
zipCode?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
telephoneNo?: string;
|
|
|
|
|
|
|
|
|
|
registerName?: string;
|
|
|
|
|
registerDate?: Date;
|
|
|
|
|
authorizedCapital?: string;
|
|
|
|
|
|
2024-04-23 18:09:08 +07:00
|
|
|
employmentOffice?: string;
|
|
|
|
|
bussinessType?: string;
|
2024-04-24 10:02:37 +07:00
|
|
|
bussinessTypeEN?: string;
|
2024-04-23 18:09:08 +07:00
|
|
|
jobPosition?: string;
|
2024-04-24 10:02:37 +07:00
|
|
|
jobPositionEN?: string;
|
2024-04-23 18:09:08 +07:00
|
|
|
jobDescription?: string;
|
|
|
|
|
saleEmployee?: string;
|
2024-06-07 13:54:02 +07:00
|
|
|
payDate?: Date;
|
2024-06-10 15:24:06 +07:00
|
|
|
wageRate?: number;
|
2024-04-23 18:09:08 +07:00
|
|
|
|
2024-04-05 11:42:03 +07:00
|
|
|
subDistrictId?: string | null;
|
|
|
|
|
districtId?: string | null;
|
|
|
|
|
provinceId?: string | null;
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-06 09:42:02 +07:00
|
|
|
@Route("api/v1/customer-branch")
|
2024-04-05 11:37:40 +07:00
|
|
|
@Tags("Customer Branch")
|
|
|
|
|
export class CustomerBranchController extends Controller {
|
2024-04-05 14:55:24 +07:00
|
|
|
@Get()
|
2024-07-03 17:51:48 +07:00
|
|
|
@Security("keycloak")
|
2024-04-05 14:55:24 +07:00
|
|
|
async list(
|
|
|
|
|
@Query() zipCode?: string,
|
2024-06-06 17:09:47 +07:00
|
|
|
@Query() customerId?: string,
|
2024-06-13 17:21:22 +07:00
|
|
|
@Query() status?: Status,
|
2024-06-10 09:41:24 +07:00
|
|
|
@Query() includeCustomer?: boolean,
|
2024-04-05 14:55:24 +07:00
|
|
|
@Query() query: string = "",
|
|
|
|
|
@Query() page: number = 1,
|
|
|
|
|
@Query() pageSize: number = 30,
|
|
|
|
|
) {
|
2024-06-14 11:54:13 +07:00
|
|
|
const filterStatus = (val?: Status) => {
|
|
|
|
|
if (!val) return {};
|
|
|
|
|
|
|
|
|
|
return val !== Status.CREATED && val !== Status.ACTIVE
|
|
|
|
|
? { status: val }
|
|
|
|
|
: { OR: [{ status: Status.CREATED }, { status: Status.ACTIVE }] };
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-05 14:55:24 +07:00
|
|
|
const where = {
|
|
|
|
|
OR: [
|
2024-06-14 11:54:13 +07:00
|
|
|
{ nameEN: { contains: query }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ name: { contains: query }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ email: { contains: query }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ code: { contains: query }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ address: { contains: query }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ addressEN: { contains: query }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ province: { name: { contains: query } }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ province: { nameEN: { contains: query } }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ district: { name: { contains: query } }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ district: { nameEN: { contains: query } }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ subDistrict: { name: { contains: query } }, zipCode, ...filterStatus(status) },
|
|
|
|
|
{ subDistrict: { nameEN: { contains: query } }, zipCode, ...filterStatus(status) },
|
2024-06-13 11:41:02 +07:00
|
|
|
{
|
|
|
|
|
customer: {
|
|
|
|
|
OR: [{ customerName: { contains: query } }, { customerNameEN: { contains: query } }],
|
|
|
|
|
},
|
2024-06-13 17:21:22 +07:00
|
|
|
zipCode,
|
|
|
|
|
status,
|
2024-06-13 11:41:02 +07:00
|
|
|
},
|
2024-04-05 14:55:24 +07:00
|
|
|
],
|
2024-06-06 17:09:47 +07:00
|
|
|
AND: { customerId },
|
|
|
|
|
} satisfies Prisma.CustomerBranchWhereInput;
|
2024-04-05 14:55:24 +07:00
|
|
|
|
|
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.customerBranch.findMany({
|
2024-06-24 13:20:59 +07:00
|
|
|
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
|
2024-04-05 14:55:24 +07:00
|
|
|
include: {
|
2024-06-10 09:41:24 +07:00
|
|
|
customer: includeCustomer,
|
2024-04-05 14:55:24 +07:00
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-06-12 14:08:18 +07:00
|
|
|
_count: true,
|
2024-04-05 14:55:24 +07:00
|
|
|
},
|
|
|
|
|
where,
|
|
|
|
|
take: pageSize,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
}),
|
|
|
|
|
prisma.customerBranch.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return { result, page, pageSize, total };
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-05 15:09:44 +07:00
|
|
|
@Get("{branchId}")
|
2024-07-03 17:51:48 +07:00
|
|
|
@Security("keycloak")
|
2024-04-05 15:09:44 +07:00
|
|
|
async getById(@Path() branchId: string) {
|
|
|
|
|
const record = await prisma.customerBranch.findFirst({
|
|
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-04-05 15:09:44 +07:00
|
|
|
},
|
|
|
|
|
where: { id: branchId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!record) {
|
2024-06-14 05:58:14 +00:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
2024-04-05 15:09:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-05 15:07:29 +07:00
|
|
|
@Get("{branchId}/employee")
|
2024-07-03 17:51:48 +07:00
|
|
|
@Security("keycloak")
|
2024-04-05 15:07:29 +07:00
|
|
|
async listEmployee(
|
|
|
|
|
@Path() branchId: string,
|
|
|
|
|
@Query() zipCode?: string,
|
|
|
|
|
@Query() query: string = "",
|
|
|
|
|
@Query() page: number = 1,
|
|
|
|
|
@Query() pageSize: number = 30,
|
|
|
|
|
) {
|
|
|
|
|
const where = {
|
|
|
|
|
AND: { customerBranchId: branchId },
|
|
|
|
|
OR: [
|
|
|
|
|
{ firstName: { contains: query }, zipCode },
|
|
|
|
|
{ firstNameEN: { contains: query }, zipCode },
|
|
|
|
|
{ lastName: { contains: query }, zipCode },
|
|
|
|
|
{ lastNameEN: { contains: query }, zipCode },
|
|
|
|
|
],
|
|
|
|
|
} satisfies Prisma.EmployeeWhereInput;
|
|
|
|
|
|
|
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.employee.findMany({
|
2024-04-09 08:51:22 +07:00
|
|
|
orderBy: { createdAt: "asc" },
|
2024-04-05 15:07:29 +07:00
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-04-05 15:07:29 +07:00
|
|
|
},
|
|
|
|
|
where,
|
|
|
|
|
take: pageSize,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
}),
|
|
|
|
|
prisma.employee.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
result: await Promise.all(
|
|
|
|
|
result.map(async (v) => ({
|
|
|
|
|
...v,
|
|
|
|
|
profileImageUrl: await minio.presignedGetObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(v.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
})),
|
|
|
|
|
),
|
|
|
|
|
page,
|
|
|
|
|
pageSize,
|
|
|
|
|
total,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-05 13:51:36 +07:00
|
|
|
@Post()
|
2024-07-03 17:51:48 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
2024-04-05 13:51:36 +07:00
|
|
|
async create(@Request() req: RequestWithUser, @Body() body: CustomerBranchCreate) {
|
2024-06-11 15:11:08 +07:00
|
|
|
const [province, district, subDistrict, customer] = await prisma.$transaction([
|
|
|
|
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
|
|
|
|
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
|
|
|
|
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
|
|
|
|
prisma.customer.findFirst({ where: { id: body.customerId || undefined } }),
|
|
|
|
|
]);
|
|
|
|
|
if (body.provinceId && !province)
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Province cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"relationProvinceNotFound",
|
2024-06-11 15:11:08 +07:00
|
|
|
);
|
|
|
|
|
if (body.districtId && !district)
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"District cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"relationDistrictNotFound",
|
2024-06-11 15:11:08 +07:00
|
|
|
);
|
|
|
|
|
if (body.subDistrictId && !subDistrict)
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Sub-district cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"relationSubDistrictNotFound",
|
2024-06-11 15:11:08 +07:00
|
|
|
);
|
|
|
|
|
if (!customer)
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Customer cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"relationCustomerNotFound",
|
2024-06-11 15:11:08 +07:00
|
|
|
);
|
2024-04-05 13:51:36 +07:00
|
|
|
|
|
|
|
|
const { provinceId, districtId, subDistrictId, customerId, ...rest } = body;
|
|
|
|
|
|
2024-04-23 18:09:08 +07:00
|
|
|
const record = await prisma.$transaction(
|
|
|
|
|
async (tx) => {
|
2024-07-03 17:51:48 +07:00
|
|
|
const conflict = await tx.customerBranch.findFirst({
|
|
|
|
|
where: { customerId, branchNo: rest.branchNo },
|
2024-04-23 18:09:08 +07:00
|
|
|
});
|
2024-07-03 17:51:48 +07:00
|
|
|
if (conflict) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Branch with current no already exists.",
|
|
|
|
|
"branchSameNoExist",
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-04-23 18:09:08 +07:00
|
|
|
|
2024-08-09 09:44:51 +07:00
|
|
|
const last = await tx.runningNo.upsert({
|
|
|
|
|
where: {
|
|
|
|
|
key: `CUSTOMER_${customer.code.slice(0, -6)}`,
|
|
|
|
|
},
|
|
|
|
|
create: {
|
|
|
|
|
key: `CUSTOMER_${customer.code.slice(0, -6)}`,
|
|
|
|
|
value: 1,
|
|
|
|
|
},
|
|
|
|
|
update: { value: { increment: 1 } },
|
|
|
|
|
});
|
|
|
|
|
|
2024-04-23 18:09:08 +07:00
|
|
|
return await tx.customerBranch.create({
|
|
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-04-23 18:09:08 +07:00
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
...rest,
|
2024-06-24 13:14:44 +07:00
|
|
|
statusOrder: +(rest.status === "INACTIVE"),
|
2024-08-09 09:44:51 +07:00
|
|
|
code: `${customer.code.slice(0, -6)}${`${last.value - 1}`.padStart(6, "0")}`,
|
2024-04-23 18:09:08 +07:00
|
|
|
customer: { connect: { id: customerId } },
|
|
|
|
|
province: { connect: provinceId ? { id: provinceId } : undefined },
|
|
|
|
|
district: { connect: districtId ? { id: districtId } : undefined },
|
|
|
|
|
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
|
2024-07-01 13:24:02 +07:00
|
|
|
createdBy: { connect: { id: req.user.sub } },
|
|
|
|
|
updatedBy: { connect: { id: req.user.sub } },
|
2024-04-23 18:09:08 +07:00
|
|
|
},
|
|
|
|
|
});
|
2024-04-05 13:51:36 +07:00
|
|
|
},
|
2024-04-23 18:09:08 +07:00
|
|
|
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
|
|
|
|
|
);
|
2024-04-05 13:51:36 +07:00
|
|
|
|
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
|
|
|
|
|
|
return record;
|
|
|
|
|
}
|
2024-04-05 14:15:38 +07:00
|
|
|
|
|
|
|
|
@Put("{branchId}")
|
2024-07-03 17:51:48 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
2024-04-05 14:15:38 +07:00
|
|
|
async editById(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: CustomerBranchUpdate,
|
|
|
|
|
@Path() branchId: string,
|
|
|
|
|
) {
|
|
|
|
|
if (body.provinceId || body.districtId || body.subDistrictId || body.customerId) {
|
|
|
|
|
const [province, district, subDistrict, customer] = await prisma.$transaction([
|
|
|
|
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
|
|
|
|
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
|
|
|
|
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
|
|
|
|
prisma.customer.findFirst({ where: { id: body.customerId || undefined } }),
|
|
|
|
|
]);
|
|
|
|
|
if (body.provinceId && !province)
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Province cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"relationProvinceNotFound",
|
2024-04-05 14:15:38 +07:00
|
|
|
);
|
|
|
|
|
if (body.districtId && !district)
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"District cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"relationDistrictNotFound",
|
2024-04-05 14:15:38 +07:00
|
|
|
);
|
|
|
|
|
if (body.subDistrictId && !subDistrict)
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Sub-district cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"relationSubDistrictNotFound",
|
2024-04-05 14:15:38 +07:00
|
|
|
);
|
|
|
|
|
if (body.customerId && !customer)
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Customer cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"relationCustomerNotFound",
|
2024-04-05 14:15:38 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { provinceId, districtId, subDistrictId, customerId, ...rest } = body;
|
|
|
|
|
|
2024-04-05 14:43:21 +07:00
|
|
|
if (!(await prisma.customerBranch.findUnique({ where: { id: branchId } }))) {
|
2024-06-14 05:58:14 +00:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
2024-04-05 14:43:21 +07:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 14:15:38 +07:00
|
|
|
const record = await prisma.customerBranch.update({
|
|
|
|
|
where: { id: branchId },
|
|
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-04-05 14:15:38 +07:00
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
...rest,
|
2024-06-24 13:14:44 +07:00
|
|
|
statusOrder: +(rest.status === "INACTIVE"),
|
2024-04-05 14:15:38 +07:00
|
|
|
customer: { connect: customerId ? { id: customerId } : undefined },
|
|
|
|
|
province: {
|
|
|
|
|
connect: provinceId ? { id: provinceId } : undefined,
|
|
|
|
|
disconnect: provinceId === null || undefined,
|
|
|
|
|
},
|
|
|
|
|
district: {
|
|
|
|
|
connect: districtId ? { id: districtId } : undefined,
|
|
|
|
|
disconnect: districtId === null || undefined,
|
|
|
|
|
},
|
|
|
|
|
subDistrict: {
|
|
|
|
|
connect: subDistrictId ? { id: subDistrictId } : undefined,
|
|
|
|
|
disconnect: subDistrictId === null || undefined,
|
|
|
|
|
},
|
2024-07-01 13:24:02 +07:00
|
|
|
updatedBy: { connect: { id: req.user.sub } },
|
2024-04-05 14:15:38 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
|
|
|
|
|
|
return record;
|
|
|
|
|
}
|
2024-04-05 15:13:33 +07:00
|
|
|
|
|
|
|
|
@Delete("{branchId}")
|
2024-07-03 17:51:48 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
2024-04-05 15:13:33 +07:00
|
|
|
async delete(@Path() branchId: string) {
|
2024-06-07 10:44:40 +07:00
|
|
|
const record = await prisma.customerBranch.findFirst({
|
|
|
|
|
where: { id: branchId },
|
|
|
|
|
});
|
2024-04-05 15:13:33 +07:00
|
|
|
|
|
|
|
|
if (!record) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.NOT_FOUND,
|
|
|
|
|
"Customer branch cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"customerBranchNotFound",
|
2024-04-05 15:13:33 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (record.status !== Status.CREATED) {
|
2024-06-24 13:14:44 +07:00
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.FORBIDDEN,
|
|
|
|
|
"Customer branch is in used.",
|
|
|
|
|
"customerBranchInUsed",
|
|
|
|
|
);
|
2024-04-05 15:13:33 +07:00
|
|
|
}
|
|
|
|
|
|
2024-07-01 14:38:07 +07:00
|
|
|
return await prisma.customerBranch
|
|
|
|
|
.delete({
|
|
|
|
|
include: { createdBy: true, updatedBy: true },
|
|
|
|
|
where: { id: branchId },
|
|
|
|
|
})
|
|
|
|
|
.then((v) => {
|
|
|
|
|
new Promise<string[]>((resolve, reject) => {
|
|
|
|
|
const item: string[] = [];
|
2024-06-07 10:44:40 +07:00
|
|
|
|
2024-07-01 14:38:07 +07:00
|
|
|
const stream = minio.listObjectsV2(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
`${attachmentLocation(record.customerId, branchId)}/`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
stream.on("data", (v) => v && v.name && item.push(v.name));
|
|
|
|
|
stream.on("end", () => resolve(item));
|
|
|
|
|
stream.on("error", () => reject(new Error("MinIO error.")));
|
|
|
|
|
}).then((list) => {
|
|
|
|
|
list.map(async (v) => {
|
2024-08-02 14:50:23 +07:00
|
|
|
await minio.removeObject(MINIO_BUCKET, v, { forceDelete: true });
|
2024-06-07 10:44:40 +07:00
|
|
|
});
|
|
|
|
|
});
|
2024-07-01 14:38:07 +07:00
|
|
|
return v;
|
2024-06-07 10:44:40 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Route("api/v1/customer-branch/{branchId}/attachment")
|
|
|
|
|
@Tags("Customer Branch")
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
export class CustomerAttachmentController extends Controller {
|
|
|
|
|
@Get()
|
|
|
|
|
async listAttachment(@Path() branchId: string) {
|
|
|
|
|
const record = await prisma.customerBranch.findFirst({
|
|
|
|
|
where: { id: branchId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!record) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.NOT_FOUND,
|
|
|
|
|
"Customer branch cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"customerBranchNotFound",
|
2024-06-07 10:44:40 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const list = await new Promise<string[]>((resolve, reject) => {
|
|
|
|
|
const item: string[] = [];
|
|
|
|
|
|
|
|
|
|
const stream = minio.listObjectsV2(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
`${attachmentLocation(record.customerId, branchId)}/`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
stream.on("data", (v) => v && v.name && item.push(v.name));
|
|
|
|
|
stream.on("end", () => resolve(item));
|
|
|
|
|
stream.on("error", () => reject(new Error("MinIO error.")));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await Promise.all(
|
|
|
|
|
list.map(async (v) => ({
|
|
|
|
|
name: v.split("/").at(-1) as string,
|
|
|
|
|
url: await minio.presignedGetObject(MINIO_BUCKET, v, 12 * 60 * 60),
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
|
|
|
|
async addAttachment(@Path() branchId: string, @Body() payload: { file: string[] }) {
|
|
|
|
|
const record = await prisma.customerBranch.findFirst({
|
|
|
|
|
where: { id: branchId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!record) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.NOT_FOUND,
|
|
|
|
|
"Customer branch cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"customerBranchNotFound",
|
2024-06-07 10:44:40 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await Promise.all(
|
|
|
|
|
payload.file.map(async (v) => ({
|
|
|
|
|
name: v,
|
|
|
|
|
url: await minio.presignedGetObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
`${attachmentLocation(record.customerId, branchId)}/${v}`,
|
|
|
|
|
),
|
|
|
|
|
uploadUrl: await minio.presignedPutObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
`${attachmentLocation(record.customerId, branchId)}/${v}`,
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete()
|
|
|
|
|
async deleteAttachment(@Path() branchId: string, @Body() payload: { file: string[] }) {
|
|
|
|
|
const record = await prisma.customerBranch.findFirst({
|
|
|
|
|
where: { id: branchId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!record) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.NOT_FOUND,
|
|
|
|
|
"Customer branch cannot be found.",
|
2024-06-14 05:58:14 +00:00
|
|
|
"customerBranchNotFound",
|
2024-06-07 10:44:40 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
|
payload.file.map(async (v) => {
|
|
|
|
|
await minio.removeObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
`${attachmentLocation(record.customerId, branchId)}/${v}`,
|
|
|
|
|
{
|
|
|
|
|
forceDelete: true,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
);
|
2024-04-05 15:13:33 +07:00
|
|
|
}
|
2024-04-05 11:37:40 +07:00
|
|
|
}
|