refactor: branch bank delete handle

This commit is contained in:
Methapon Metanipat 2024-09-05 11:07:10 +07:00
parent 5d32e2dca5
commit 3c3ee87277

View file

@ -91,6 +91,7 @@ type BranchUpdate = {
headOfficeId?: string | null; headOfficeId?: string | null;
bank?: { bank?: {
id?: string;
bankName: string; bankName: string;
bankBranch: string; bankBranch: string;
accountName: string; accountName: string;
@ -491,43 +492,73 @@ export class BranchController extends Controller {
); );
} }
const record = await prisma.branch.update({ return await prisma.$transaction(async (tx) => {
include: { province: true, district: true, subDistrict: true }, const listDeleted = bank
data: { ? await tx.branchBank.findMany({
...rest, where: { id: { not: { in: bank.flatMap((v) => (!!v.id ? v.id : [])) } }, branchId },
statusOrder: +(rest.status === "INACTIVE"), })
isHeadOffice: headOfficeId !== undefined ? headOfficeId === null : undefined, : [];
bank: bank ? { deleteMany: {}, createMany: { data: bank } } : 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,
},
headOffice: {
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 },
});
return record; await minio.removeObjects(
MINIO_BUCKET,
listDeleted.map((v) => fileLocation.branch.bank(v.branchId, v.id)),
);
return await prisma.branch.update({
include: { province: true, district: true, subDistrict: true },
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
isHeadOffice: headOfficeId !== undefined ? headOfficeId === null : undefined,
bank: bank
? {
deleteMany: { id: { in: listDeleted.map((v) => v.id) } },
upsert: bank.map((v) => ({
where: {
id: v.id || "",
},
create: {
...v,
createdByUserId: req.user.sub,
updatedByUserId: req.user.sub,
id: undefined,
},
update: {
...v,
updatedByUserId: req.user.sub,
},
})),
}
: 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,
},
headOffice: {
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 },
});
});
} }
@Delete("{branchId}") @Delete("{branchId}")