feat: employee code gen

This commit is contained in:
Methapon2001 2024-06-06 13:47:13 +07:00
parent a3e72cbb1b
commit a6910de555

View file

@ -163,12 +163,16 @@ export class EmployeeController extends Controller {
@Post() @Post()
async create(@Request() req: RequestWithUser, @Body() body: EmployeeCreate) { async create(@Request() req: RequestWithUser, @Body() body: EmployeeCreate) {
if (body.provinceId || body.districtId || body.subDistrictId || body.customerBranchId) {
const [province, district, subDistrict, customerBranch] = await prisma.$transaction([ const [province, district, subDistrict, customerBranch] = await prisma.$transaction([
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }), prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
prisma.district.findFirst({ where: { id: body.districtId || undefined } }), prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }), prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
prisma.customerBranch.findFirst({ where: { id: body.customerBranchId || undefined } }), prisma.customerBranch.findFirst({
where: { id: body.customerBranchId || undefined },
include: {
customer: true,
},
}),
]); ]);
if (body.provinceId && !province) if (body.provinceId && !province)
throw new HttpError( throw new HttpError(
@ -188,17 +192,29 @@ export class EmployeeController extends Controller {
"Sub-district cannot be found.", "Sub-district cannot be found.",
"missing_or_invalid_parameter", "missing_or_invalid_parameter",
); );
if (body.customerBranchId && !customerBranch) if (!customerBranch)
throw new HttpError( throw new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
"Customer Branch cannot be found.", "Customer Branch cannot be found.",
"missing_or_invalid_parameter", "missing_or_invalid_parameter",
); );
}
const { provinceId, districtId, subDistrictId, customerBranchId, ...rest } = body; const { provinceId, districtId, subDistrictId, customerBranchId, ...rest } = body;
const record = await prisma.employee.create({ const record = await prisma.$transaction(
async (tx) => {
const last = await tx.runningNo.upsert({
where: {
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
},
create: {
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
value: 1,
},
update: { value: { increment: 1 } },
});
return await prisma.employee.create({
include: { include: {
province: true, province: true,
district: true, district: true,
@ -206,6 +222,7 @@ export class EmployeeController extends Controller {
}, },
data: { data: {
...rest, ...rest,
code: `${customerBranch.customer.code}-${customerBranch.branchNo}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}${last.value.toString().padStart(4, "0")}`,
province: { connect: provinceId ? { id: provinceId } : undefined }, province: { connect: provinceId ? { id: provinceId } : undefined },
district: { connect: districtId ? { id: districtId } : undefined }, district: { connect: districtId ? { id: districtId } : undefined },
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined }, subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
@ -214,6 +231,9 @@ export class EmployeeController extends Controller {
updateBy: req.user.name, updateBy: req.user.name,
}, },
}); });
},
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
);
await prisma.customerBranch.updateMany({ await prisma.customerBranch.updateMany({
where: { id: customerBranchId, status: Status.CREATED }, where: { id: customerBranchId, status: Status.CREATED },