refactor: accept branchNo instead of auto

This commit is contained in:
Methapon2001 2024-07-03 17:05:31 +07:00
parent 9a310420e5
commit edb9e34a2b

View file

@ -49,6 +49,7 @@ export type CustomerCreate = {
legalPersonNo: string; legalPersonNo: string;
branchNo: number;
taxNo: string | null; taxNo: string | null;
name: string; name: string;
nameEN: string; nameEN: string;
@ -95,6 +96,7 @@ export type CustomerUpdate = {
legalPersonNo: string; legalPersonNo: string;
branchNo: number;
taxNo: string | null; taxNo: string | null;
name: string; name: string;
nameEN: string; nameEN: string;
@ -331,10 +333,9 @@ export class CustomerController extends Controller {
branch: { branch: {
createMany: { createMany: {
data: data:
customerBranch?.map((v, i) => ({ customerBranch?.map((v) => ({
...v, ...v,
branchNo: i + 1, code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}-${v.branchNo.toString().padStart(2, "0")}`,
code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}-${(i + 1).toString().padStart(2, "0")}`,
createdByUserId: req.user.sub, createdByUserId: req.user.sub,
updatedByUserId: req.user.sub, updatedByUserId: req.user.sub,
})) || [], })) || [],
@ -438,6 +439,17 @@ export class CustomerController extends Controller {
); );
} }
if (
customerBranch &&
relation.find((a) => !customerBranch.find((b) => a.id !== b.id && a.branchNo === b.branchNo))
) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Branch cannot have same number.",
"oneOrMoreBranchNoExist",
);
}
const record = await prisma.customer const record = await prisma.customer
.update({ .update({
include: { include: {
@ -463,20 +475,19 @@ export class CustomerController extends Controller {
}, },
status: Status.CREATED, status: Status.CREATED,
}, },
upsert: customerBranch.map((v, i) => ({ upsert: customerBranch.map((v) => ({
where: { id: v.id || "" }, where: { id: v.id || "" },
create: { create: {
...v, ...v,
branchNo: i + 1, code: `${customer.code}-${v.branchNo.toString().padStart(2, "0")}`,
code: `${customer.code}-${(i + 1).toString().padStart(2, "0")}`,
createdByUserId: req.user.sub, createdByUserId: req.user.sub,
updatedByUserId: req.user.sub, updatedByUserId: req.user.sub,
id: undefined, id: undefined,
}, },
update: { update: {
...v, ...v,
branchNo: i + 1, code: undefined,
code: `${customer.code}-${(i + 1).toString().padStart(2, "0")}`, branchNo: undefined,
updatedByUserId: req.user.sub, updatedByUserId: req.user.sub,
}, },
})), })),