feat: employee with relation on create / update

This commit is contained in:
Methapon2001 2024-06-10 13:11:29 +07:00
parent 5f20d8b052
commit a6d8f1c7f6

View file

@ -58,6 +58,38 @@ type EmployeeCreate = {
subDistrictId?: string | null;
districtId?: string | null;
provinceId?: string | null;
employeeWork?: {
ownerName: string;
positionName: string;
jobType: string;
workplace: string;
workPermitNo: string;
workPermitIssuDate: Date;
workPermitExpireDate: Date;
workEndDate: Date;
}[];
employeeCheckup?: {
checkupType: string;
checkupResult: string;
provinceId?: string | null;
hospitalName: string;
remark: string;
medicalBenefitScheme: string;
insuranceCompany: string;
coverageStartDate: Date;
coverageExpireDate: Date;
}[];
employeeOtherInfo: {
citizenId: string;
fatherFullName: string;
motherFullName: string;
birthPlace: string;
};
};
type EmployeeUpdate = {
@ -88,6 +120,40 @@ type EmployeeUpdate = {
subDistrictId?: string | null;
districtId?: string | null;
provinceId?: string | null;
employeeWork?: {
id?: string;
ownerName: string;
positionName: string;
jobType: string;
workplace: string;
workPermitNo: string;
workPermitIssuDate: Date;
workPermitExpireDate: Date;
workEndDate: Date;
}[];
employeeCheckup?: {
id?: string;
checkupType: string;
checkupResult: string;
provinceId?: string | null;
hospitalName: string;
remark: string;
medicalBenefitScheme: string;
insuranceCompany: string;
coverageStartDate: Date;
coverageExpireDate: Date;
}[];
employeeOtherInfo: {
citizenId: string;
fatherFullName: string;
motherFullName: string;
birthPlace: string;
};
};
@Route("api/v1/employee")
@ -199,7 +265,34 @@ export class EmployeeController extends Controller {
"missing_or_invalid_parameter",
);
const { provinceId, districtId, subDistrictId, customerBranchId, ...rest } = body;
const {
provinceId,
districtId,
subDistrictId,
customerBranchId,
employeeWork,
employeeCheckup,
employeeOtherInfo,
...rest
} = body;
const listProvinceId = employeeCheckup?.reduce<string[]>((acc, cur) => {
if (cur.provinceId && !acc.includes(cur.provinceId)) return acc.concat(cur.provinceId);
return acc;
}, []);
if (listProvinceId) {
const [listProvince] = await prisma.$transaction([
prisma.province.findMany({ where: { id: { in: listProvinceId } } }),
]);
if (listProvince.length !== listProvinceId.length) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Some province cannot be found.",
"missing_or_invalid_parameter",
);
}
}
const record = await prisma.$transaction(
async (tx) => {
@ -219,10 +312,30 @@ export class EmployeeController extends Controller {
province: true,
district: true,
subDistrict: true,
employeeOtherInfo: true,
employeeCheckup: {
include: {
province: true,
},
},
employeeWork: true,
},
data: {
...rest,
code: `${customerBranch.customer.code}-${customerBranch.branchNo}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}${last.value.toString().padStart(4, "0")}`,
employeeWork: {
createMany: {
data: employeeWork || [],
},
},
employeeCheckup: {
createMany: {
data: employeeCheckup || [],
},
},
employeeOtherInfo: {
create: employeeOtherInfo,
},
province: { connect: provinceId ? { id: provinceId } : undefined },
district: { connect: districtId ? { id: districtId } : undefined },
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
@ -295,7 +408,34 @@ export class EmployeeController extends Controller {
);
}
const { provinceId, districtId, subDistrictId, customerBranchId, ...rest } = body;
const {
provinceId,
districtId,
subDistrictId,
customerBranchId,
employeeWork,
employeeCheckup,
employeeOtherInfo,
...rest
} = body;
const listProvinceId = employeeCheckup?.reduce<string[]>((acc, cur) => {
if (cur.provinceId && !acc.includes(cur.provinceId)) return acc.concat(cur.provinceId);
return acc;
}, []);
if (listProvinceId) {
const [listProvince] = await prisma.$transaction([
prisma.province.findMany({ where: { id: { in: listProvinceId } } }),
]);
if (listProvince.length !== listProvinceId.length) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Some province cannot be found.",
"missing_or_invalid_parameter",
);
}
}
const record = await prisma.employee.update({
where: { id: employeeId },
@ -303,10 +443,68 @@ export class EmployeeController extends Controller {
province: true,
district: true,
subDistrict: true,
employeeOtherInfo: true,
employeeCheckup: {
include: {
province: true,
},
},
employeeWork: true,
},
data: {
...rest,
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,
@ -340,7 +538,7 @@ export class EmployeeController extends Controller {
if (record.status !== Status.CREATED) {
throw new HttpError(
HttpStatus.FORBIDDEN,
"Emplyee is in used.",
"Employee is in used.",
"missing_or_invalid_parameter",
);
}