feat: employee code gen
This commit is contained in:
parent
a3e72cbb1b
commit
a6910de555
1 changed files with 67 additions and 47 deletions
|
|
@ -163,57 +163,77 @@ 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({
|
||||||
prisma.customerBranch.findFirst({ where: { id: body.customerBranchId || undefined } }),
|
where: { id: body.customerBranchId || undefined },
|
||||||
]);
|
include: {
|
||||||
if (body.provinceId && !province)
|
customer: true,
|
||||||
throw new HttpError(
|
},
|
||||||
HttpStatus.BAD_REQUEST,
|
}),
|
||||||
"Province cannot be found.",
|
]);
|
||||||
"missing_or_invalid_parameter",
|
if (body.provinceId && !province)
|
||||||
);
|
throw new HttpError(
|
||||||
if (body.districtId && !district)
|
HttpStatus.BAD_REQUEST,
|
||||||
throw new HttpError(
|
"Province cannot be found.",
|
||||||
HttpStatus.BAD_REQUEST,
|
"missing_or_invalid_parameter",
|
||||||
"District cannot be found.",
|
);
|
||||||
"missing_or_invalid_parameter",
|
if (body.districtId && !district)
|
||||||
);
|
throw new HttpError(
|
||||||
if (body.subDistrictId && !subDistrict)
|
HttpStatus.BAD_REQUEST,
|
||||||
throw new HttpError(
|
"District cannot be found.",
|
||||||
HttpStatus.BAD_REQUEST,
|
"missing_or_invalid_parameter",
|
||||||
"Sub-district cannot be found.",
|
);
|
||||||
"missing_or_invalid_parameter",
|
if (body.subDistrictId && !subDistrict)
|
||||||
);
|
throw new HttpError(
|
||||||
if (body.customerBranchId && !customerBranch)
|
HttpStatus.BAD_REQUEST,
|
||||||
throw new HttpError(
|
"Sub-district cannot be found.",
|
||||||
HttpStatus.BAD_REQUEST,
|
"missing_or_invalid_parameter",
|
||||||
"Customer Branch 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 { provinceId, districtId, subDistrictId, customerBranchId, ...rest } = body;
|
||||||
|
|
||||||
const record = await prisma.employee.create({
|
const record = await prisma.$transaction(
|
||||||
include: {
|
async (tx) => {
|
||||||
province: true,
|
const last = await tx.runningNo.upsert({
|
||||||
district: true,
|
where: {
|
||||||
subDistrict: true,
|
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: {
|
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
|
||||||
...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,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
await prisma.customerBranch.updateMany({
|
await prisma.customerBranch.updateMany({
|
||||||
where: { id: customerBranchId, status: Status.CREATED },
|
where: { id: customerBranchId, status: Status.CREATED },
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue