feat: rewrite filter for product again

This commit is contained in:
caturbgs 2021-12-22 14:19:58 +07:00
parent ff2205ea03
commit d9ca0888e7
3 changed files with 22 additions and 17 deletions

View File

@ -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 const baseQuery = this.productSubCategoriesRepository
.createQueryBuilder('product_sub_categories') .createQueryBuilder('product_sub_categories')
.leftJoinAndSelect('product_sub_categories.category', 'category'); .leftJoinAndSelect('product_sub_categories.category', 'category');
if (category.length > 0) { if (category && filterCategories.length > 0) {
baseQuery.where({ baseQuery.where({
category: In(category), category: In(filterCategories),
}); });
} }

View File

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

View File

@ -47,7 +47,14 @@ export class ProductService {
return this.productRepository.findOneOrFail(result.identifiers[0].id); 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) { // if (supplier.length > 0) {
// const dataSupplier = await this.supplierService.findByActiveAll(); // const dataSupplier = await this.supplierService.findByActiveAll();
// supplier = dataSupplier.map((item) => item.id); // supplier = dataSupplier.map((item) => item.id);
@ -78,15 +85,15 @@ export class ProductService {
// .addSelect('current_price.price') // .addSelect('current_price.price')
// .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_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)', { 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)', { baseQuery.where('supplier.id IN (:...supplierId)', {
supplierId: supplier, supplierId: filterSupplier,
}); });
} }