feat!: code gen customer

This commit is contained in:
Methapon2001 2024-08-09 09:44:51 +07:00
parent bcd765c71a
commit e0b3ed6383
2 changed files with 34 additions and 7 deletions

View file

@ -305,6 +305,17 @@ export class CustomerBranchController extends Controller {
);
}
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 } },
});
return await tx.customerBranch.create({
include: {
province: true,
@ -316,7 +327,7 @@ export class CustomerBranchController extends Controller {
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
code: `${customer.code}-${rest.branchNo.toString().padStart(2, "0")}`,
code: `${customer.code.slice(0, -6)}${`${last.value - 1}`.padStart(6, "0")}`,
customer: { connect: { id: customerId } },
province: { connect: provinceId ? { id: provinceId } : undefined },
district: { connect: districtId ? { id: districtId } : undefined },

View file

@ -37,6 +37,8 @@ const MANAGE_ROLES = [
export type CustomerCreate = {
registeredBranchId?: string;
code: string;
status?: Status;
personName: string;
personNameEN?: string;
@ -312,15 +314,29 @@ export class CustomerController extends Controller {
async (tx) => {
const last = await tx.runningNo.upsert({
where: {
key: `CUSTOMER_${body.customerType}`,
key: `CUSTOMER_${body.code.toLocaleUpperCase()}`,
},
create: {
key: `CUSTOMER_${body.customerType}`,
key: `CUSTOMER_${body.code.toLocaleUpperCase()}`,
value: 1,
},
update: { value: { increment: 1 } },
update: { value: { increment: (body.customerBranch?.length || 0) + 1 } },
});
body.code = body.code.toLocaleUpperCase();
const exist = await tx.customer.findFirst({
where: { code: `${body.code}000000` },
});
if (exist) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Customer with same code already exists.",
"sameCustomerCodeExists",
);
}
return await prisma.customer.create({
include: {
branch: {
@ -336,13 +352,13 @@ export class CustomerController extends Controller {
data: {
...payload,
statusOrder: +(payload.status === "INACTIVE"),
code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}`,
code: `${body.code}000000`,
branch: {
createMany: {
data:
customerBranch?.map((v) => ({
customerBranch?.map((v, i) => ({
...v,
code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}-${v.branchNo.toString().padStart(2, "0")}`,
code: `${body.code}${`${last.value - customerBranch?.length + i + 2}`.padStart(6, "0")}`,
createdByUserId: req.user.sub,
updatedByUserId: req.user.sub,
})) || [],