From a6910de55518a28d9368e3c3c78a8437c2045587 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 6 Jun 2024 13:47:13 +0700 Subject: [PATCH] feat: employee code gen --- src/controllers/employee-controller.ts | 114 +++++++++++++++---------- 1 file changed, 67 insertions(+), 47 deletions(-) diff --git a/src/controllers/employee-controller.ts b/src/controllers/employee-controller.ts index fbb6aa0..bcb12a2 100644 --- a/src/controllers/employee-controller.ts +++ b/src/controllers/employee-controller.ts @@ -163,57 +163,77 @@ export class EmployeeController extends Controller { @Post() 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([ - prisma.province.findFirst({ where: { id: body.provinceId || undefined } }), - prisma.district.findFirst({ where: { id: body.districtId || undefined } }), - prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }), - prisma.customerBranch.findFirst({ where: { id: body.customerBranchId || undefined } }), - ]); - if (body.provinceId && !province) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Province cannot be found.", - "missing_or_invalid_parameter", - ); - if (body.districtId && !district) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "District cannot be found.", - "missing_or_invalid_parameter", - ); - if (body.subDistrictId && !subDistrict) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Sub-district cannot be found.", - "missing_or_invalid_parameter", - ); - if (body.customerBranchId && !customerBranch) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Customer Branch cannot be found.", - "missing_or_invalid_parameter", - ); - } + const [province, district, subDistrict, customerBranch] = await prisma.$transaction([ + prisma.province.findFirst({ where: { id: body.provinceId || undefined } }), + prisma.district.findFirst({ where: { id: body.districtId || undefined } }), + prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }), + prisma.customerBranch.findFirst({ + where: { id: body.customerBranchId || undefined }, + include: { + customer: true, + }, + }), + ]); + if (body.provinceId && !province) + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Province cannot be found.", + "missing_or_invalid_parameter", + ); + if (body.districtId && !district) + throw new HttpError( + HttpStatus.BAD_REQUEST, + "District cannot be found.", + "missing_or_invalid_parameter", + ); + if (body.subDistrictId && !subDistrict) + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Sub-district cannot be found.", + "missing_or_invalid_parameter", + ); + if (!customerBranch) + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Customer Branch cannot be found.", + "missing_or_invalid_parameter", + ); const { provinceId, districtId, subDistrictId, customerBranchId, ...rest } = body; - const record = await prisma.employee.create({ - include: { - province: true, - district: true, - subDistrict: true, + 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: { + province: true, + district: true, + subDistrict: true, + }, + data: { + ...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 }, + district: { connect: districtId ? { id: districtId } : undefined }, + subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined }, + customerBranch: { connect: { id: customerBranchId } }, + createdBy: req.user.name, + updateBy: req.user.name, + }, + }); }, - data: { - ...rest, - province: { connect: provinceId ? { id: provinceId } : undefined }, - district: { connect: districtId ? { id: districtId } : undefined }, - subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined }, - customerBranch: { connect: { id: customerBranchId } }, - createdBy: req.user.name, - updateBy: req.user.name, - }, - }); + { isolationLevel: Prisma.TransactionIsolationLevel.Serializable }, + ); await prisma.customerBranch.updateMany({ where: { id: customerBranchId, status: Status.CREATED },