refactor: branch bank delete handle
This commit is contained in:
parent
5d32e2dca5
commit
3c3ee87277
1 changed files with 67 additions and 36 deletions
|
|
@ -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,13 +492,44 @@ export class BranchController extends Controller {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const record = await prisma.branch.update({
|
return await prisma.$transaction(async (tx) => {
|
||||||
|
const listDeleted = bank
|
||||||
|
? await tx.branchBank.findMany({
|
||||||
|
where: { id: { not: { in: bank.flatMap((v) => (!!v.id ? v.id : [])) } }, branchId },
|
||||||
|
})
|
||||||
|
: [];
|
||||||
|
|
||||||
|
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 },
|
include: { province: true, district: true, subDistrict: true },
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
statusOrder: +(rest.status === "INACTIVE"),
|
statusOrder: +(rest.status === "INACTIVE"),
|
||||||
isHeadOffice: headOfficeId !== undefined ? headOfficeId === null : undefined,
|
isHeadOffice: headOfficeId !== undefined ? headOfficeId === null : undefined,
|
||||||
bank: bank ? { deleteMany: {}, createMany: { data: bank } } : 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: {
|
province: {
|
||||||
connect: provinceId ? { id: provinceId } : undefined,
|
connect: provinceId ? { id: provinceId } : undefined,
|
||||||
disconnect: provinceId === null || undefined,
|
disconnect: provinceId === null || undefined,
|
||||||
|
|
@ -526,8 +558,7 @@ export class BranchController extends Controller {
|
||||||
},
|
},
|
||||||
where: { id: branchId },
|
where: { id: branchId },
|
||||||
});
|
});
|
||||||
|
});
|
||||||
return record;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("{branchId}")
|
@Delete("{branchId}")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue