feat!: code gen customer
This commit is contained in:
parent
bcd765c71a
commit
e0b3ed6383
2 changed files with 34 additions and 7 deletions
|
|
@ -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({
|
return await tx.customerBranch.create({
|
||||||
include: {
|
include: {
|
||||||
province: true,
|
province: true,
|
||||||
|
|
@ -316,7 +327,7 @@ export class CustomerBranchController extends Controller {
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
statusOrder: +(rest.status === "INACTIVE"),
|
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 } },
|
customer: { connect: { id: customerId } },
|
||||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||||
district: { connect: districtId ? { id: districtId } : undefined },
|
district: { connect: districtId ? { id: districtId } : undefined },
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,8 @@ const MANAGE_ROLES = [
|
||||||
export type CustomerCreate = {
|
export type CustomerCreate = {
|
||||||
registeredBranchId?: string;
|
registeredBranchId?: string;
|
||||||
|
|
||||||
|
code: string;
|
||||||
|
|
||||||
status?: Status;
|
status?: Status;
|
||||||
personName: string;
|
personName: string;
|
||||||
personNameEN?: string;
|
personNameEN?: string;
|
||||||
|
|
@ -312,15 +314,29 @@ export class CustomerController extends Controller {
|
||||||
async (tx) => {
|
async (tx) => {
|
||||||
const last = await tx.runningNo.upsert({
|
const last = await tx.runningNo.upsert({
|
||||||
where: {
|
where: {
|
||||||
key: `CUSTOMER_${body.customerType}`,
|
key: `CUSTOMER_${body.code.toLocaleUpperCase()}`,
|
||||||
},
|
},
|
||||||
create: {
|
create: {
|
||||||
key: `CUSTOMER_${body.customerType}`,
|
key: `CUSTOMER_${body.code.toLocaleUpperCase()}`,
|
||||||
value: 1,
|
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({
|
return await prisma.customer.create({
|
||||||
include: {
|
include: {
|
||||||
branch: {
|
branch: {
|
||||||
|
|
@ -336,13 +352,13 @@ export class CustomerController extends Controller {
|
||||||
data: {
|
data: {
|
||||||
...payload,
|
...payload,
|
||||||
statusOrder: +(payload.status === "INACTIVE"),
|
statusOrder: +(payload.status === "INACTIVE"),
|
||||||
code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}`,
|
code: `${body.code}000000`,
|
||||||
branch: {
|
branch: {
|
||||||
createMany: {
|
createMany: {
|
||||||
data:
|
data:
|
||||||
customerBranch?.map((v) => ({
|
customerBranch?.map((v, i) => ({
|
||||||
...v,
|
...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,
|
createdByUserId: req.user.sub,
|
||||||
updatedByUserId: req.user.sub,
|
updatedByUserId: req.user.sub,
|
||||||
})) || [],
|
})) || [],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue