feat: update query to also return branch
This commit is contained in:
parent
0635ef062d
commit
b455985ba1
1 changed files with 45 additions and 11 deletions
|
|
@ -55,6 +55,7 @@ export class CustomerController extends Controller {
|
||||||
@Query() query: string = "",
|
@Query() query: string = "",
|
||||||
@Query() page: number = 1,
|
@Query() page: number = 1,
|
||||||
@Query() pageSize: number = 30,
|
@Query() pageSize: number = 30,
|
||||||
|
@Query() includeBranch: boolean = false,
|
||||||
) {
|
) {
|
||||||
const where = {
|
const where = {
|
||||||
OR: [{ customerName: { contains: query } }, { customerNameEN: { contains: query } }],
|
OR: [{ customerName: { contains: query } }, { customerNameEN: { contains: query } }],
|
||||||
|
|
@ -62,6 +63,17 @@ export class CustomerController extends Controller {
|
||||||
|
|
||||||
const [result, total] = await prisma.$transaction([
|
const [result, total] = await prisma.$transaction([
|
||||||
prisma.customer.findMany({
|
prisma.customer.findMany({
|
||||||
|
include: {
|
||||||
|
branch: includeBranch
|
||||||
|
? {
|
||||||
|
include: {
|
||||||
|
province: true,
|
||||||
|
district: true,
|
||||||
|
subDistrict: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
},
|
||||||
orderBy: { createdAt: "asc" },
|
orderBy: { createdAt: "asc" },
|
||||||
where,
|
where,
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
|
|
@ -85,7 +97,18 @@ export class CustomerController extends Controller {
|
||||||
|
|
||||||
@Get("{customerId}")
|
@Get("{customerId}")
|
||||||
async getById(@Path() customerId: string) {
|
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)
|
if (!record)
|
||||||
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");
|
||||||
return Object.assign(record, {
|
return Object.assign(record, {
|
||||||
|
|
@ -250,19 +273,30 @@ export class CustomerController extends Controller {
|
||||||
const { customerBranch, ...payload } = body;
|
const { customerBranch, ...payload } = body;
|
||||||
|
|
||||||
const record = await prisma.customer.update({
|
const record = await prisma.customer.update({
|
||||||
|
include: {
|
||||||
|
branch: {
|
||||||
|
include: {
|
||||||
|
province: true,
|
||||||
|
district: true,
|
||||||
|
subDistrict: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
where: { id: customerId },
|
where: { id: customerId },
|
||||||
data: {
|
data: {
|
||||||
...payload,
|
...payload,
|
||||||
branch: {
|
branch:
|
||||||
deleteMany: {
|
(customerBranch && {
|
||||||
id: { notIn: customerBranch?.map((v) => v.id) || [] },
|
deleteMany: {
|
||||||
},
|
id: { notIn: customerBranch.map((v) => v.id) || [] },
|
||||||
updateMany:
|
},
|
||||||
customerBranch?.map((v) => ({
|
updateMany:
|
||||||
where: { id: v.id },
|
customerBranch.map((v) => ({
|
||||||
data: { ...v, updateBy: req.user.name },
|
where: { id: v.id },
|
||||||
})) || [],
|
data: { ...v, updateBy: req.user.name },
|
||||||
},
|
})) || [],
|
||||||
|
}) ||
|
||||||
|
undefined,
|
||||||
updateBy: req.user.name,
|
updateBy: req.user.name,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue