refactor: use nested relation instead

This commit is contained in:
Methapon Metanipat 2024-08-20 17:47:06 +07:00
parent c7f7713cbd
commit 8892e237ec

View file

@ -352,6 +352,11 @@ export class BranchController extends Controller {
code: `${code?.toLocaleUpperCase()}${`${last.value - 1}`.padStart(6, "0")}`,
bank: bank ? { createMany: { data: bank } } : undefined,
isHeadOffice: !headOfficeId,
contact: {
create: (typeof contact === "string" ? [contact] : contact)?.map((v) => ({
telephoneNo: v,
})),
},
province: { connect: provinceId ? { id: provinceId } : undefined },
district: { connect: districtId ? { id: districtId } : undefined },
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
@ -373,15 +378,6 @@ export class BranchController extends Controller {
this.setStatus(HttpStatus.CREATED);
if (record && contact) {
await prisma.branchContact.createMany({
data:
typeof contact === "string"
? [{ telephoneNo: contact, branchId: record.id }]
: contact.map((v) => ({ telephoneNo: v, branchId: record.id })),
});
}
return Object.assign(record, {
contact: await prisma.branchContact.findMany({ where: { branchId: record.id } }),
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)),
@ -493,22 +489,19 @@ export class BranchController extends Controller {
connect: headOfficeId ? { id: headOfficeId } : undefined,
disconnect: headOfficeId === null || undefined,
},
contact: contact
? {
deleteMany: {},
create: (typeof contact === "string" ? [contact] : contact)?.map((v) => ({
telephoneNo: v,
})),
}
: undefined,
updatedBy: { connect: { id: req.user.sub } },
},
where: { id: branchId },
});
if (record && contact !== undefined) {
await prisma.branchContact.deleteMany({ where: { branchId } });
contact &&
(await prisma.branchContact.createMany({
data:
typeof contact === "string"
? [{ telephoneNo: contact, branchId }]
: contact.map((v) => ({ telephoneNo: v, branchId })),
}));
}
return Object.assign(record, {
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)),
imageUploadUrl: await minio.presignedPutObject(