diff --git a/src/controllers/customer-controller.ts b/src/controllers/customer-controller.ts index 931cf4e..047a711 100644 --- a/src/controllers/customer-controller.ts +++ b/src/controllers/customer-controller.ts @@ -18,7 +18,7 @@ import prisma from "../db"; import minio from "../services/minio"; import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; -import { CustomerBranchCreate, CustomerBranchUpdate } from "./customer-branch-controller"; +import { CustomerBranchCreate } from "./customer-branch-controller"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); @@ -41,17 +41,13 @@ export type CustomerUpdate = { customerName?: string; customerNameEN?: string; taxNo?: string | null; - customerBranch?: (Omit & { id: string })[]; + customerBranch?: (Omit & { id?: string })[]; }; function imageLocation(id: string) { return `customer/${id}/profile-image`; } -function attachmentLocation(customerId: string, branchId: string) { - return `customer/${customerId}/branch/${branchId}`; -} - @Route("api/v1/customer") @Tags("Customer") @Security("keycloak") @@ -306,35 +302,78 @@ export class CustomerController extends Controller { const { customerBranch, ...payload } = body; - const record = await prisma.customer.update({ - include: { - branch: { - include: { - province: true, - district: true, - subDistrict: true, - }, - }, - }, - where: { id: customerId }, - data: { - ...payload, - branch: - (customerBranch && { - deleteMany: { - id: { notIn: customerBranch.map((v) => v.id) || [] }, - }, - updateMany: - customerBranch.map((v) => ({ - where: { id: v.id }, - data: { ...v, updateBy: req.user.name }, - })) || [], - }) || - undefined, - updateBy: req.user.name, + const relation = await prisma.customerBranch.findMany({ + where: { + customerId, }, }); + const record = await prisma.customer + .update({ + include: { + branch: { + include: { + province: true, + district: true, + subDistrict: true, + }, + }, + }, + where: { id: customerId }, + data: { + ...payload, + branch: + (customerBranch && { + deleteMany: { + id: { + notIn: customerBranch.map((v) => v.id).filter((v): v is string => !!v) || [], + }, + }, + upsert: customerBranch.map((v, i) => ({ + where: { id: v.id || "" }, + create: { + ...v, + branchNo: `${i + 1}`, + createdBy: req.user.name, + updateBy: req.user.name, + id: undefined, + }, + update: { + ...v, + branchNo: `${i + 1}`, + updateBy: req.user.name, + }, + })), + }) || + undefined, + updateBy: req.user.name, + }, + }) + .then((v) => { + if (customerBranch) { + relation + .filter((a) => !customerBranch?.find((b) => b.id === a.id)) + .forEach((deleted) => { + new Promise((resolve, reject) => { + const item: string[] = []; + + const stream = minio.listObjectsV2(MINIO_BUCKET, `customer/${deleted.id}`); + + stream.on("data", (v) => v && v.name && item.push(v.name)); + stream.on("end", () => resolve(item)); + stream.on("error", () => reject(new Error("MinIO error."))); + }).then((list) => { + list.map(async (v) => { + await minio.removeObject(MINIO_BUCKET, v, { + forceDelete: true, + }); + }); + }); + }); + } + return v; + }); + return Object.assign(record, { imageUrl: await minio.presignedGetObject( MINIO_BUCKET,