feat: rewrite filter for product

This commit is contained in:
caturbgs 2021-12-22 14:03:57 +07:00
parent 41856f7a02
commit ff2205ea03
3 changed files with 17 additions and 25 deletions

View File

@ -44,20 +44,14 @@ export class ProductSubCategoriesService {
);
}
async findAll(page, category) {
let filterCategories = [];
if (category !== 'null') {
filterCategories = category.split(',').map((data) => data.trim());
}
async findAll(page, category: []) {
const baseQuery = this.productSubCategoriesRepository
.createQueryBuilder('product_sub_categories')
.leftJoinAndSelect('product_sub_categories.category', 'category');
if (filterCategories.length > 0) {
if (category.length > 0) {
baseQuery.where({
category: In(filterCategories),
category: In(category),
});
}

View File

@ -75,7 +75,11 @@ export class ProductController {
@Query('sub-category') subcategory: string,
@Query('supplier') supplier: string,
) {
const data = await this.productService.findAll(page, supplier, subcategory);
const data = await this.productService.findAll(
page,
JSON.parse(supplier),
JSON.parse(subcategory),
);
return {
...data,
@ -155,7 +159,10 @@ export class ProductController {
@Query('page') page: number,
@Query('category') category: string,
) {
const data = await this.productSubCategoriesService.findAll(page, category);
const data = await this.productSubCategoriesService.findAll(
page,
JSON.parse(category),
);
return {
...data,

View File

@ -47,16 +47,7 @@ export class ProductService {
return this.productRepository.findOneOrFail(result.identifiers[0].id);
}
async findAll(page: number, supplier: string, subCategories: string) {
let filterSupplier = [];
let filterSubCategories = [];
if (supplier !== 'null') {
filterSupplier = supplier.split(',').map((data) => data.trim());
}
if (subCategories !== 'null') {
filterSubCategories = subCategories.split(',').map((data) => data.trim());
}
async findAll(page: number, supplier: [], subCategories: []) {
// if (supplier.length > 0) {
// const dataSupplier = await this.supplierService.findByActiveAll();
// supplier = dataSupplier.map((item) => item.id);
@ -87,15 +78,15 @@ export class ProductService {
// .addSelect('current_price.price')
// .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price');
if (filterSubCategories.length > 0) {
if (subCategories.length > 0) {
baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', {
subCategoryId: filterSubCategories,
subCategoryId: subCategories,
});
}
if (filterSupplier.length > 0) {
if (supplier.length > 0) {
baseQuery.where('supplier.id IN (:...supplierId)', {
supplierId: filterSupplier,
supplierId: supplier,
});
}