refactor: relation helper function

This commit is contained in:
Methapon Metanipat 2024-09-10 16:31:07 +07:00
parent 273026d735
commit acdbef485a

View file

@ -25,6 +25,7 @@ import {
createPermCheck,
createPermCondition,
} from "../services/permission";
import { connectOrDisconnect, connectOrNot } from "../utils/relation";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
@ -501,9 +502,9 @@ export class EmployeeController extends Controller {
employeeOtherInfo: {
create: employeeOtherInfo,
},
province: { connect: provinceId ? { id: provinceId } : undefined },
district: { connect: districtId ? { id: districtId } : undefined },
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
province: connectOrNot(provinceId),
district: connectOrNot(districtId),
subDistrict: connectOrNot(subDistrictId),
customerBranch: { connect: { id: customerBranchId } },
createdBy: { connect: { id: req.user.sub } },
updatedBy: { connect: { id: req.user.sub } },
@ -677,7 +678,7 @@ export class EmployeeController extends Controller {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
code,
customerBranch: { connect: customerBranchId ? { id: customerBranchId } : undefined },
customerBranch: connectOrNot(customerBranchId),
employeeWork: employeeWork
? {
deleteMany: {
@ -724,23 +725,10 @@ export class EmployeeController extends Controller {
})),
}
: undefined,
employeeOtherInfo: employeeOtherInfo
? {
update: 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,
},
employeeOtherInfo: employeeOtherInfo ? { update: employeeOtherInfo } : undefined,
province: connectOrDisconnect(provinceId),
district: connectOrDisconnect(districtId),
subDistrict: connectOrDisconnect(subDistrictId),
createdBy: { connect: { id: req.user.sub } },
updatedBy: { connect: { id: req.user.sub } },
},