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;
branchNo: number;
taxNo: string | null;
name: string;
nameEN: string;
@ -95,6 +96,7 @@ export type CustomerUpdate = {
legalPersonNo: string;
branchNo: number;
taxNo: string | null;
name: string;
nameEN: string;
@ -331,10 +333,9 @@ export class CustomerController extends Controller {
branch: {
createMany: {
data:
customerBranch?.map((v, i) => ({
customerBranch?.map((v) => ({
...v,
branchNo: i + 1,
code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}-${(i + 1).toString().padStart(2, "0")}`,
code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}-${v.branchNo.toString().padStart(2, "0")}`,
createdByUserId: 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
.update({
include: {
@ -463,20 +475,19 @@ export class CustomerController extends Controller {
},
status: Status.CREATED,
},
upsert: customerBranch.map((v, i) => ({
upsert: customerBranch.map((v) => ({
where: { id: v.id || "" },
create: {
...v,
branchNo: i + 1,
code: `${customer.code}-${(i + 1).toString().padStart(2, "0")}`,
code: `${customer.code}-${v.branchNo.toString().padStart(2, "0")}`,
createdByUserId: req.user.sub,
updatedByUserId: req.user.sub,
id: undefined,
},
update: {
...v,
branchNo: i + 1,
code: `${customer.code}-${(i + 1).toString().padStart(2, "0")}`,
code: undefined,
branchNo: undefined,
updatedByUserId: req.user.sub,
},
})),