feat: auto gen branch code

This commit is contained in:
Methapon2001 2024-04-09 11:45:29 +07:00
parent 120e2942b8
commit 3abff0594a

View file

@ -20,7 +20,6 @@ import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user";
type BranchCreate = {
code?: string;
status?: Status;
taxNo: string;
nameEN: string;
@ -40,7 +39,6 @@ type BranchCreate = {
};
type BranchUpdate = {
code?: string;
status?: "ACTIVE" | "INACTIVE";
taxNo?: string;
nameEN?: string;
@ -157,41 +155,50 @@ export class BranchController extends Controller {
@Post()
async createBranch(@Request() req: RequestWithUser, @Body() body: BranchCreate) {
if (body.provinceId || body.districtId || body.subDistrictId || body.headOfficeId) {
const [province, district, subDistrict, branch] = 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.branch.findFirst({ where: { id: body.headOfficeId || undefined } }),
]);
if (body.provinceId && !province)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Province cannot be found.",
"missing_or_invalid_parameter",
);
if (body.districtId && !district)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"District cannot be found.",
"missing_or_invalid_parameter",
);
if (body.subDistrictId && !subDistrict)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Sub-district cannot be found.",
"missing_or_invalid_parameter",
);
if (body.headOfficeId && !branch)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Head branch cannot be found.",
"missing_or_invalid_parameter",
);
}
const [province, district, subDistrict, head] = 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.branch.findFirst({ where: { id: body.headOfficeId || undefined } }),
]);
if (body.provinceId && !province)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Province cannot be found.",
"missing_or_invalid_parameter",
);
if (body.districtId && !district)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"District cannot be found.",
"missing_or_invalid_parameter",
);
if (body.subDistrictId && !subDistrict)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Sub-district cannot be found.",
"missing_or_invalid_parameter",
);
if (body.headOfficeId && !head)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Head branch cannot be found.",
"missing_or_invalid_parameter",
);
const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body;
const year = new Date().getFullYear();
const last = await prisma.branch.findFirst({
orderBy: { createdAt: "desc" },
where: { headOfficeId: headOfficeId ?? null },
});
const code = !headOfficeId
? `HQ${year.toString().slice(2)}${+(last?.code.slice(-1) || 0) + 1}`
: `BR${head?.code.slice(2, 5)}${(+(last?.code.slice(-2) || 0) + 1).toString().padStart(2, "0")}`;
const record = await prisma.branch.create({
include: {
province: true,
@ -200,6 +207,7 @@ export class BranchController extends Controller {
},
data: {
...rest,
code,
isHeadOffice: !headOfficeId,
province: { connect: provinceId ? { id: provinceId } : undefined },
district: { connect: districtId ? { id: districtId } : undefined },