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