feat: can create branch when create customer
This commit is contained in:
parent
503505757d
commit
e38e4c71d0
2 changed files with 122 additions and 5 deletions
|
|
@ -29,7 +29,7 @@ function imageLocation(id: string) {
|
||||||
return `employee/profile-img-${id}`;
|
return `employee/profile-img-${id}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
type CustomerBranchCreate = {
|
export type CustomerBranchCreate = {
|
||||||
customerId: string;
|
customerId: string;
|
||||||
|
|
||||||
status?: Status;
|
status?: Status;
|
||||||
|
|
@ -56,7 +56,7 @@ type CustomerBranchCreate = {
|
||||||
provinceId?: string | null;
|
provinceId?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
type CustomerBranchUpdate = {
|
export type CustomerBranchUpdate = {
|
||||||
customerId?: string;
|
customerId?: string;
|
||||||
|
|
||||||
status?: "ACTIVE" | "INACTIVE";
|
status?: "ACTIVE" | "INACTIVE";
|
||||||
|
|
|
||||||
|
|
@ -18,6 +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";
|
||||||
|
|
||||||
if (!process.env.MINIO_BUCKET) {
|
if (!process.env.MINIO_BUCKET) {
|
||||||
throw Error("Require MinIO bucket.");
|
throw Error("Require MinIO bucket.");
|
||||||
|
|
@ -30,6 +31,7 @@ export type CustomerCreate = {
|
||||||
customerType: CustomerType;
|
customerType: CustomerType;
|
||||||
customerName: string;
|
customerName: string;
|
||||||
customerNameEN: string;
|
customerNameEN: string;
|
||||||
|
customerBranch?: Omit<CustomerBranchCreate, "customerId">[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CustomerUpdate = {
|
export type CustomerUpdate = {
|
||||||
|
|
@ -37,6 +39,7 @@ export type CustomerUpdate = {
|
||||||
customerType?: CustomerType;
|
customerType?: CustomerType;
|
||||||
customerName?: string;
|
customerName?: string;
|
||||||
customerNameEN?: string;
|
customerNameEN?: string;
|
||||||
|
customerBranch?: (Omit<CustomerBranchUpdate, "customerId"> & { id: string })[];
|
||||||
};
|
};
|
||||||
|
|
||||||
function imageLocation(id: string) {
|
function imageLocation(id: string) {
|
||||||
|
|
@ -103,10 +106,74 @@ export class CustomerController extends Controller {
|
||||||
|
|
||||||
const code = `${body.customerType}${(+(last?.code.slice(-6) || 0) + 1).toString().padStart(6, "0")}`;
|
const code = `${body.customerType}${(+(last?.code.slice(-6) || 0) + 1).toString().padStart(6, "0")}`;
|
||||||
|
|
||||||
|
const { customerBranch, ...payload } = body;
|
||||||
|
|
||||||
|
const provinceId = body.customerBranch?.reduce<string[]>((acc, cur) => {
|
||||||
|
if (cur.provinceId && !acc.includes(cur.provinceId)) return acc.concat(cur.provinceId);
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
const districtId = body.customerBranch?.reduce<string[]>((acc, cur) => {
|
||||||
|
if (cur.districtId && !acc.includes(cur.districtId)) return acc.concat(cur.districtId);
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
const subDistrictId = body.customerBranch?.reduce<string[]>((acc, cur) => {
|
||||||
|
if (cur.subDistrictId && !acc.includes(cur.subDistrictId))
|
||||||
|
return acc.concat(cur.subDistrictId);
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const [province, district, subDistrict] = await prisma.$transaction([
|
||||||
|
prisma.province.findMany({ where: { id: { in: provinceId } } }),
|
||||||
|
prisma.district.findMany({ where: { id: { in: districtId } } }),
|
||||||
|
prisma.subDistrict.findMany({ where: { id: { in: subDistrictId } } }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (provinceId && province.length !== provinceId?.length) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Some province cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (districtId && district.length !== districtId?.length) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Some district cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (subDistrictId && subDistrict.length !== subDistrictId?.length) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Some sub district cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const record = await prisma.customer.create({
|
const record = await prisma.customer.create({
|
||||||
|
include: {
|
||||||
|
branch: {
|
||||||
|
include: {
|
||||||
|
province: true,
|
||||||
|
district: true,
|
||||||
|
subDistrict: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
data: {
|
data: {
|
||||||
...body,
|
...payload,
|
||||||
code,
|
code,
|
||||||
|
branch: {
|
||||||
|
createMany: {
|
||||||
|
data:
|
||||||
|
customerBranch?.map((v, i) => ({
|
||||||
|
...v,
|
||||||
|
branchNo: `${i + 1}`,
|
||||||
|
createdBy: req.user.name,
|
||||||
|
updateBy: req.user.name,
|
||||||
|
})) || [],
|
||||||
|
},
|
||||||
|
},
|
||||||
createdBy: req.user.name,
|
createdBy: req.user.name,
|
||||||
updateBy: req.user.name,
|
updateBy: req.user.name,
|
||||||
},
|
},
|
||||||
|
|
@ -138,11 +205,61 @@ export class CustomerController extends Controller {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "data_not_found");
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "data_not_found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const provinceId = body.customerBranch?.reduce<string[]>((acc, cur) => {
|
||||||
|
if (cur.provinceId && !acc.includes(cur.provinceId)) return acc.concat(cur.provinceId);
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
const districtId = body.customerBranch?.reduce<string[]>((acc, cur) => {
|
||||||
|
if (cur.districtId && !acc.includes(cur.districtId)) return acc.concat(cur.districtId);
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
const subDistrictId = body.customerBranch?.reduce<string[]>((acc, cur) => {
|
||||||
|
if (cur.subDistrictId && !acc.includes(cur.subDistrictId))
|
||||||
|
return acc.concat(cur.subDistrictId);
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const [province, district, subDistrict] = await prisma.$transaction([
|
||||||
|
prisma.province.findMany({ where: { id: { in: provinceId } } }),
|
||||||
|
prisma.district.findMany({ where: { id: { in: districtId } } }),
|
||||||
|
prisma.subDistrict.findMany({ where: { id: { in: subDistrictId } } }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (provinceId && province.length !== provinceId?.length) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Some province cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (districtId && district.length !== districtId?.length) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Some district cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (subDistrictId && subDistrict.length !== subDistrictId?.length) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Some sub district cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { customerBranch, ...payload } = body;
|
||||||
|
|
||||||
const record = await prisma.customer.update({
|
const record = await prisma.customer.update({
|
||||||
where: { id: customerId },
|
where: { id: customerId },
|
||||||
data: {
|
data: {
|
||||||
...body,
|
...payload,
|
||||||
createdBy: req.user.name,
|
branch: {
|
||||||
|
updateMany:
|
||||||
|
customerBranch?.map((v) => ({
|
||||||
|
where: { id: v.id },
|
||||||
|
data: { ...v, updateBy: req.user.name },
|
||||||
|
})) || [],
|
||||||
|
},
|
||||||
updateBy: req.user.name,
|
updateBy: req.user.name,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue