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.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({ prisma.customerBranch.findFirst({
where: { id: body.customerBranchId || undefined }, where: { id: body.customerBranchId },
include: { include: { customer: true },
customer: true,
},
}), }),
]); ]);
if (body.provinceId && !province) if (body.provinceId && !province)
@ -450,37 +448,42 @@ export class EmployeeController extends Controller {
@Body() body: EmployeeUpdate, @Body() body: EmployeeUpdate,
@Path() employeeId: string, @Path() employeeId: string,
) { ) {
if (body.provinceId || body.districtId || body.subDistrictId || body.customerBranchId) { const [province, district, subDistrict, customerBranch, employee] = 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({
prisma.customerBranch.findFirst({ where: { id: body.customerBranchId || undefined } }), where: { id: body.customerBranchId || undefined },
]); include: { customer: true },
if (body.provinceId && !province) }),
throw new HttpError( prisma.employee.findFirst({ where: { id: employeeId } }),
HttpStatus.BAD_REQUEST, ]);
"Province cannot be found.", if (body.provinceId && !province)
"missing_or_invalid_parameter", throw new HttpError(
); HttpStatus.BAD_REQUEST,
if (body.districtId && !district) "Province cannot be found.",
throw new HttpError( "missing_or_invalid_parameter",
HttpStatus.BAD_REQUEST, );
"District cannot be found.", if (body.districtId && !district)
"missing_or_invalid_parameter", throw new HttpError(
); HttpStatus.BAD_REQUEST,
if (body.subDistrictId && !subDistrict) "District cannot be found.",
throw new HttpError( "missing_or_invalid_parameter",
HttpStatus.BAD_REQUEST, );
"Sub-district cannot be found.", if (body.subDistrictId && !subDistrict)
"missing_or_invalid_parameter", throw new HttpError(
); HttpStatus.BAD_REQUEST,
if (body.customerBranchId && !customerBranch) "Sub-district cannot be found.",
throw new HttpError( "missing_or_invalid_parameter",
HttpStatus.BAD_REQUEST, );
"Customer cannot be found.", if (body.customerBranchId && !customerBranch)
"missing_or_invalid_parameter", throw new HttpError(
); HttpStatus.BAD_REQUEST,
"Customer cannot be found.",
"missing_or_invalid_parameter",
);
if (!employee) {
throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "data_not_found");
} }
const { const {
@ -512,93 +515,110 @@ export class EmployeeController extends Controller {
} }
} }
const record = await prisma.employee.update({ const record = await prisma.$transaction(async (tx) => {
where: { id: employeeId }, let code: string | undefined;
include: {
province: true, if (customerBranch && customerBranch.id !== customerBranchId) {
district: true, const last = await tx.runningNo.upsert({
subDistrict: true, where: {
employeeOtherInfo: true, key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
employeeCheckup: {
include: {
province: true,
}, },
}, create: {
employeeWork: true, key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
}, value: 1,
data: { },
...rest, update: { value: { increment: 1 } },
customerBranch: { connect: customerBranchId ? { id: customerBranchId } : undefined }, });
employeeWork: employeeWork 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")}`;
? { }
deleteMany: {
id: {
notIn: employeeWork.map((v) => v.id).filter((v): v is string => !!v) || [],
},
},
upsert: employeeWork.map((v) => ({
where: { id: v.id || "" },
create: {
...v,
createdBy: req.user.name,
updateBy: req.user.name,
id: undefined,
},
update: {
...v,
updateBy: req.user.name,
},
})),
}
: undefined,
employeeCheckup: employeeCheckup return await prisma.employee.update({
? { where: { id: employeeId },
deleteMany: { include: {
id: { province: true,
notIn: employeeCheckup.map((v) => v.id).filter((v): v is string => !!v) || [], district: true,
}, subDistrict: true,
}, employeeOtherInfo: true,
upsert: employeeCheckup.map((v) => ({ employeeCheckup: {
where: { id: v.id || "" }, include: {
create: { province: true,
...v, },
createdBy: req.user.name, },
updateBy: req.user.name, employeeWork: true,
id: undefined,
},
update: {
...v,
updateBy: req.user.name,
},
})),
}
: undefined,
employeeOtherInfo: employeeOtherInfo
? {
deleteMany: {},
create: employeeOtherInfo,
}
: undefined,
province: {
connect: provinceId ? { id: provinceId } : undefined,
disconnect: provinceId === null || undefined,
}, },
district: { data: {
connect: districtId ? { id: districtId } : undefined, ...rest,
disconnect: districtId === null || undefined, code,
customerBranch: { connect: customerBranchId ? { id: customerBranchId } : undefined },
employeeWork: employeeWork
? {
deleteMany: {
id: {
notIn: employeeWork.map((v) => v.id).filter((v): v is string => !!v) || [],
},
},
upsert: employeeWork.map((v) => ({
where: { id: v.id || "" },
create: {
...v,
createdBy: req.user.name,
updateBy: req.user.name,
id: undefined,
},
update: {
...v,
updateBy: req.user.name,
},
})),
}
: undefined,
employeeCheckup: employeeCheckup
? {
deleteMany: {
id: {
notIn: employeeCheckup.map((v) => v.id).filter((v): v is string => !!v) || [],
},
},
upsert: employeeCheckup.map((v) => ({
where: { id: v.id || "" },
create: {
...v,
createdBy: req.user.name,
updateBy: req.user.name,
id: undefined,
},
update: {
...v,
updateBy: req.user.name,
},
})),
}
: undefined,
employeeOtherInfo: employeeOtherInfo
? {
deleteMany: {},
create: employeeOtherInfo,
}
: 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,
},
createdBy: req.user.name,
updateBy: req.user.name,
}, },
subDistrict: { });
connect: subDistrictId ? { id: subDistrictId } : undefined,
disconnect: subDistrictId === null || undefined,
},
createdBy: req.user.name,
updateBy: req.user.name,
},
}); });
this.setStatus(HttpStatus.CREATED);
return Object.assign(record, { return Object.assign(record, {
profileImageUrl: await presignedGetObjectIfExist( profileImageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET, MINIO_BUCKET,