From d9ca0888e7d44f9564decc7a01d36303afc91f64 Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 14:19:58 +0700 Subject: [PATCH] feat: rewrite filter for product again --- src/product/product-sub-categories.service.ts | 11 ++++++++--- src/product/product.controller.ts | 11 ++--------- src/product/product.service.ts | 17 ++++++++++++----- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/product/product-sub-categories.service.ts b/src/product/product-sub-categories.service.ts index be2c2e7..6ad2644 100644 --- a/src/product/product-sub-categories.service.ts +++ b/src/product/product-sub-categories.service.ts @@ -44,14 +44,19 @@ export class ProductSubCategoriesService { ); } - async findAll(page, category: []) { + async findAll(page, category: string) { + let filterCategories; + if (category) { + filterCategories = category.split(',').map((data) => data.trim()); + } + const baseQuery = this.productSubCategoriesRepository .createQueryBuilder('product_sub_categories') .leftJoinAndSelect('product_sub_categories.category', 'category'); - if (category.length > 0) { + if (category && filterCategories.length > 0) { baseQuery.where({ - category: In(category), + category: In(filterCategories), }); } diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 0539d81..52d3c0e 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -75,11 +75,7 @@ export class ProductController { @Query('sub-category') subcategory: string, @Query('supplier') supplier: string, ) { - const data = await this.productService.findAll( - page, - JSON.parse(supplier), - JSON.parse(subcategory), - ); + const data = await this.productService.findAll(page, supplier, subcategory); return { ...data, @@ -159,10 +155,7 @@ export class ProductController { @Query('page') page: number, @Query('category') category: string, ) { - const data = await this.productSubCategoriesService.findAll( - page, - JSON.parse(category), - ); + const data = await this.productSubCategoriesService.findAll(page, category); return { ...data, diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 2a0cb36..7642e6b 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -47,7 +47,14 @@ export class ProductService { return this.productRepository.findOneOrFail(result.identifiers[0].id); } - async findAll(page: number, supplier: [], subCategories: []) { + async findAll(page: number, supplier: string, subCategories: string) { + let filterSupplier, filterSubCategories; + if (supplier) { + filterSupplier = supplier.split(',').map((data) => data.trim()); + } + if (subCategories) { + filterSubCategories = subCategories.split(',').map((data) => data.trim()); + } // if (supplier.length > 0) { // const dataSupplier = await this.supplierService.findByActiveAll(); // supplier = dataSupplier.map((item) => item.id); @@ -78,15 +85,15 @@ export class ProductService { // .addSelect('current_price.price') // .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); - if (subCategories.length > 0) { + if (subCategories && filterSubCategories.length > 0) { baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { - subCategoryId: subCategories, + subCategoryId: filterSubCategories, }); } - if (supplier.length > 0) { + if (supplier && filterSupplier.length > 0) { baseQuery.where('supplier.id IN (:...supplierId)', { - supplierId: supplier, + supplierId: filterSupplier, }); }