feat: update query to also return branch

This commit is contained in:
Methapon2001 2024-04-22 17:16:30 +07:00
parent 0635ef062d
commit b455985ba1

View file

@ -55,6 +55,7 @@ export class CustomerController extends Controller {
@Query() query: string = "",
@Query() page: number = 1,
@Query() pageSize: number = 30,
@Query() includeBranch: boolean = false,
) {
const where = {
OR: [{ customerName: { contains: query } }, { customerNameEN: { contains: query } }],
@ -62,6 +63,17 @@ export class CustomerController extends Controller {
const [result, total] = await prisma.$transaction([
prisma.customer.findMany({
include: {
branch: includeBranch
? {
include: {
province: true,
district: true,
subDistrict: true,
},
}
: undefined,
},
orderBy: { createdAt: "asc" },
where,
take: pageSize,
@ -85,7 +97,18 @@ export class CustomerController extends Controller {
@Get("{customerId}")
async getById(@Path() customerId: string) {
const record = await prisma.customer.findFirst({ where: { id: customerId } });
const record = await prisma.customer.findFirst({
include: {
branch: {
include: {
province: true,
district: true,
subDistrict: true,
},
},
},
where: { id: customerId },
});
if (!record)
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "data_not_found");
return Object.assign(record, {
@ -250,19 +273,30 @@ 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: {
deleteMany: {
id: { notIn: customerBranch?.map((v) => v.id) || [] },
},
updateMany:
customerBranch?.map((v) => ({
where: { id: v.id },
data: { ...v, updateBy: req.user.name },
})) || [],
},
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,
},
});