feat: create / update customer branch

This commit is contained in:
Methapon Metanipat 2024-08-20 16:17:41 +07:00
parent c5317d60f1
commit 8d446113da

View file

@ -42,30 +42,35 @@ function attachmentLocation(customerId: string, branchId: string) {
return `customer/${customerId}/branch/${branchId}`;
}
export type CustomerBranchCreate = {
export type CustomerBranchCreate = (
| {
// NOTE: About (Natural Person)
citizenId: string;
}
| {
// NOTE: About (Legal Entity)
legalPersonNo: string;
registerName: string;
registerNameEN: string;
registerDate: Date;
authorizedCapital: string;
}
) & {
customerId: string;
status?: Status;
legalPersonNo: string;
branchNo: number;
taxNo: string | null;
name: string;
nameEN: string;
addressEN: string;
workplace: string;
workplaceEN: string;
address: string;
zipCode: string;
addressEN: string;
email: string;
telephoneNo: string;
registerName: string;
registerDate: Date;
authorizedCapital: string;
employmentOffice: string;
bussinessType: string;
bussinessTypeEN: string;
businessType: string;
businessTypeEN: string;
jobPosition: string;
jobPositionEN: string;
jobDescription: string;
@ -78,26 +83,32 @@ export type CustomerBranchCreate = {
provinceId?: string | null;
};
export type CustomerBranchUpdate = {
export type CustomerBranchUpdate = (
| {
// NOTE: About (Natural Person)
citizenId: string;
}
| {
// NOTE: About (Legal Entity)
legalPersonNo: string;
registerName?: string;
registerNameEN?: string;
registerDate?: Date;
authorizedCapital?: string;
}
) & {
customerId?: string;
status?: "ACTIVE" | "INACTIVE";
legalPersonNo?: string;
workplace: string;
workplaceEN: string;
address: string;
addressEN: string;
taxNo?: string | null;
name?: string;
nameEN?: string;
addressEN?: string;
address?: string;
zipCode?: string;
email?: string;
telephoneNo?: string;
registerName?: string;
registerDate?: Date;
authorizedCapital?: string;
employmentOffice?: string;
bussinessType?: string;
bussinessTypeEN?: string;
@ -305,37 +316,42 @@ export class CustomerBranchController extends Controller {
const record = await prisma.$transaction(
async (tx) => {
// 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,
// district: true,
// subDistrict: true,
// createdBy: true,
// updatedBy: true,
// },
// data: {
// ...rest,
// statusOrder: +(rest.status === "INACTIVE"),
// 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 },
// subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
// createdBy: { connect: { id: req.user.sub } },
// updatedBy: { connect: { id: req.user.sub } },
// },
// });
let runningKey = "";
if ("citizenId" in body) {
runningKey = `CUSTOMER_BRANCH_${body.citizenId}`;
} else {
runningKey = `CUSTOMER_BRANCH_${body.legalPersonNo}`;
}
const last = await tx.runningNo.upsert({
where: { key: runningKey },
create: {
key: runningKey,
value: 1,
},
update: { value: { increment: 1 } },
});
return await tx.customerBranch.create({
include: {
province: true,
district: true,
subDistrict: true,
createdBy: true,
updatedBy: true,
},
data: {
...rest,
code: `${"citizenId" in body ? body.citizenId : body.legalPersonNo}-${last.value - 1}`,
customer: { connect: { id: customerId } },
province: { connect: provinceId ? { id: provinceId } : undefined },
district: { connect: districtId ? { id: districtId } : undefined },
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
createdBy: { connect: { id: req.user.sub } },
updatedBy: { connect: { id: req.user.sub } },
},
});
},
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
);
@ -391,41 +407,36 @@ export class CustomerBranchController extends Controller {
);
}
// const { provinceId, districtId, subDistrictId, customerId, ...rest } = body;
//
//
// const record = await prisma.customerBranch.update({
// where: { id: branchId },
// include: {
// province: true,
// district: true,
// subDistrict: true,
// createdBy: true,
// updatedBy: true,
// },
// data: {
// ...rest,
// statusOrder: +(rest.status === "INACTIVE"),
// customer: { connect: customerId ? { id: customerId } : undefined },
// province: {
// connect: provinceId ? { id: provinceId } : undefined,
// disconnect: provinceId === null || undefined,
// },
// district: {
// connect: districtId ? { id: districtId } : undefined,
// disconnect: districtId === null || undefined,
// },
// subDistrict: {
// connect: subDistrictId ? { id: subDistrictId } : undefined,
// disconnect: subDistrictId === null || undefined,
// },
// updatedBy: { connect: { id: req.user.sub } },
// },
// });
//
// this.setStatus(HttpStatus.CREATED);
//
// return record;
const { provinceId, districtId, subDistrictId, customerId, ...rest } = body;
return await prisma.customerBranch.update({
where: { id: branchId },
include: {
province: true,
district: true,
subDistrict: true,
createdBy: true,
updatedBy: true,
},
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
customer: { connect: customerId ? { id: customerId } : undefined },
province: {
connect: provinceId ? { id: provinceId } : undefined,
disconnect: provinceId === null || undefined,
},
district: {
connect: districtId ? { id: districtId } : undefined,
disconnect: districtId === null || undefined,
},
subDistrict: {
connect: subDistrictId ? { id: subDistrictId } : undefined,
disconnect: subDistrictId === null || undefined,
},
updatedBy: { connect: { id: req.user.sub } },
},
});
}
@Delete("{branchId}")