refactor: use nested relation instead
This commit is contained in:
parent
c7f7713cbd
commit
8892e237ec
1 changed files with 13 additions and 20 deletions
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue