feat: feat update employee code
This commit is contained in:
parent
aec90e4644
commit
c302eafb83
1 changed files with 136 additions and 116 deletions
|
|
@ -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,12 +448,15 @@ 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({ 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)
|
if (body.provinceId && !province)
|
||||||
throw new HttpError(
|
throw new HttpError(
|
||||||
|
|
@ -481,6 +482,8 @@ export class EmployeeController extends Controller {
|
||||||
"Customer cannot be found.",
|
"Customer cannot be found.",
|
||||||
"missing_or_invalid_parameter",
|
"missing_or_invalid_parameter",
|
||||||
);
|
);
|
||||||
|
if (!employee) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "data_not_found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
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 },
|
where: { id: employeeId },
|
||||||
include: {
|
include: {
|
||||||
province: true,
|
province: true,
|
||||||
|
|
@ -528,6 +548,7 @@ export class EmployeeController extends Controller {
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
|
code,
|
||||||
customerBranch: { connect: customerBranchId ? { id: customerBranchId } : undefined },
|
customerBranch: { connect: customerBranchId ? { id: customerBranchId } : undefined },
|
||||||
employeeWork: employeeWork
|
employeeWork: employeeWork
|
||||||
? {
|
? {
|
||||||
|
|
@ -596,8 +617,7 @@ export class EmployeeController extends Controller {
|
||||||
updateBy: 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(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue