feat: update customer branch
This commit is contained in:
parent
fea4bd874f
commit
e5dcc86747
1 changed files with 71 additions and 32 deletions
|
|
@ -18,7 +18,7 @@ import prisma from "../db";
|
||||||
import minio from "../services/minio";
|
import minio from "../services/minio";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
import { CustomerBranchCreate, CustomerBranchUpdate } from "./customer-branch-controller";
|
import { CustomerBranchCreate } from "./customer-branch-controller";
|
||||||
|
|
||||||
if (!process.env.MINIO_BUCKET) {
|
if (!process.env.MINIO_BUCKET) {
|
||||||
throw Error("Require MinIO bucket.");
|
throw Error("Require MinIO bucket.");
|
||||||
|
|
@ -41,17 +41,13 @@ export type CustomerUpdate = {
|
||||||
customerName?: string;
|
customerName?: string;
|
||||||
customerNameEN?: string;
|
customerNameEN?: string;
|
||||||
taxNo?: string | null;
|
taxNo?: string | null;
|
||||||
customerBranch?: (Omit<CustomerBranchUpdate, "customerId"> & { id: string })[];
|
customerBranch?: (Omit<CustomerBranchCreate, "customerId"> & { id?: string })[];
|
||||||
};
|
};
|
||||||
|
|
||||||
function imageLocation(id: string) {
|
function imageLocation(id: string) {
|
||||||
return `customer/${id}/profile-image`;
|
return `customer/${id}/profile-image`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function attachmentLocation(customerId: string, branchId: string) {
|
|
||||||
return `customer/${customerId}/branch/${branchId}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Route("api/v1/customer")
|
@Route("api/v1/customer")
|
||||||
@Tags("Customer")
|
@Tags("Customer")
|
||||||
@Security("keycloak")
|
@Security("keycloak")
|
||||||
|
|
@ -306,35 +302,78 @@ export class CustomerController extends Controller {
|
||||||
|
|
||||||
const { customerBranch, ...payload } = body;
|
const { customerBranch, ...payload } = body;
|
||||||
|
|
||||||
const record = await prisma.customer.update({
|
const relation = await prisma.customerBranch.findMany({
|
||||||
include: {
|
where: {
|
||||||
branch: {
|
customerId,
|
||||||
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 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<string[]>((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, {
|
return Object.assign(record, {
|
||||||
imageUrl: await minio.presignedGetObject(
|
imageUrl: await minio.presignedGetObject(
|
||||||
MINIO_BUCKET,
|
MINIO_BUCKET,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue