feat: new gen branch code

This commit is contained in:
Methapon2001 2024-08-07 14:49:02 +07:00
parent 2ed718ccd0
commit c92ae50f82

View file

@ -28,6 +28,7 @@ const MINIO_BUCKET = process.env.MINIO_BUCKET;
type BranchCreate = {
status?: Status;
code: string;
taxNo: string;
nameEN: string;
name: string;
@ -293,27 +294,22 @@ export class BranchController extends Controller {
"relationHQNotFound",
);
const { provinceId, districtId, subDistrictId, headOfficeId, bank, contact, ...rest } = body;
const year = new Date().getFullYear();
const { provinceId, districtId, subDistrictId, headOfficeId, bank, contact, code, ...rest } =
body;
const record = await prisma.$transaction(
async (tx) => {
const last = await tx.runningNo.upsert({
where: {
key: !headOfficeId ? `HQ${year.toString().slice(2)}` : `BR${head?.code.slice(2, 5)}`,
key: `MAIN_BRANCH_${code}`,
},
create: {
key: !headOfficeId ? `HQ${year.toString().slice(2)}` : `BR${head?.code.slice(2, 5)}`,
key: `MAIN_BRANCH_${code}`,
value: 1,
},
update: { value: { increment: 1 } },
});
const code = !headOfficeId
? `HQ${year.toString().slice(2)}${last.value}`
: `BR${head?.code.slice(2, 5)}${last.value.toString().padStart(2, "0")}`;
return await tx.branch.create({
include: {
province: true,
@ -325,7 +321,7 @@ export class BranchController extends Controller {
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
code,
code: `${code?.toLocaleUpperCase()}${`${last.value - 1}`.padStart(6, "0")}`,
bank: bank ? { createMany: { data: bank } } : undefined,
isHeadOffice: !headOfficeId,
province: { connect: provinceId ? { id: provinceId } : undefined },