feat: feat update employee code

This commit is contained in:
Methapon2001 2024-06-13 16:45:08 +07:00
parent aec90e4644
commit c302eafb83

View file

@ -298,10 +298,8 @@ export class EmployeeController extends Controller {
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,
},
where: { id: body.customerBranchId },
include: { customer: true },
}),
]);
if (body.provinceId && !province)
@ -450,12 +448,15 @@ export class EmployeeController extends Controller {
@Body() body: EmployeeUpdate,
@Path() employeeId: string,
) {
if (body.provinceId || body.districtId || body.subDistrictId || body.customerBranchId) {
const [province, district, subDistrict, customerBranch] = await prisma.$transaction([
const [province, district, subDistrict, customerBranch, employee] = 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 } }),
prisma.customerBranch.findFirst({
where: { id: body.customerBranchId || undefined },
include: { customer: true },
}),
prisma.employee.findFirst({ where: { id: employeeId } }),
]);
if (body.provinceId && !province)
throw new HttpError(
@ -481,6 +482,8 @@ export class EmployeeController extends Controller {
"Customer cannot be found.",
"missing_or_invalid_parameter",
);
if (!employee) {
throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "data_not_found");
}
const {
@ -512,7 +515,24 @@ export class EmployeeController extends Controller {
}
}
const record = await prisma.employee.update({
const record = await prisma.$transaction(async (tx) => {
let code: string | undefined;
if (customerBranch && customerBranch.id !== customerBranchId) {
const last = await tx.runningNo.upsert({
where: {
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
},
create: {
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
value: 1,
},
update: { value: { increment: 1 } },
});
code = `${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}${last.value.toString().padStart(4, "0")}`;
}
return await prisma.employee.update({
where: { id: employeeId },
include: {
province: true,
@ -528,6 +548,7 @@ export class EmployeeController extends Controller {
},
data: {
...rest,
code,
customerBranch: { connect: customerBranchId ? { id: customerBranchId } : undefined },
employeeWork: employeeWork
? {
@ -596,8 +617,7 @@ export class EmployeeController extends Controller {
updateBy: req.user.name,
},
});
this.setStatus(HttpStatus.CREATED);
});
return Object.assign(record, {
profileImageUrl: await presignedGetObjectIfExist(