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) { async findAll(page, category: []) {
let filterCategories = [];
if (category !== 'null') {
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 (filterCategories.length > 0) { if (category.length > 0) {
baseQuery.where({ baseQuery.where({
category: In(filterCategories), category: In(category),
}); });
} }

View File

@ -75,7 +75,11 @@ 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(page, supplier, subcategory); const data = await this.productService.findAll(
page,
JSON.parse(supplier),
JSON.parse(subcategory),
);
return { return {
...data, ...data,
@ -155,7 +159,10 @@ 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(page, category); const data = await this.productSubCategoriesService.findAll(
page,
JSON.parse(category),
);
return { return {
...data, ...data,

View File

@ -47,16 +47,7 @@ export class ProductService {
return this.productRepository.findOneOrFail(result.identifiers[0].id); return this.productRepository.findOneOrFail(result.identifiers[0].id);
} }
async findAll(page: number, supplier: string, subCategories: string) { async findAll(page: number, supplier: [], subCategories: []) {
let filterSupplier = [];
let filterSubCategories = [];
if (supplier !== 'null') {
filterSupplier = supplier.split(',').map((data) => data.trim());
}
if (subCategories !== 'null') {
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);
@ -87,15 +78,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 (filterSubCategories.length > 0) { if (subCategories.length > 0) {
baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { 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)', { baseQuery.where('supplier.id IN (:...supplierId)', {
supplierId: filterSupplier, supplierId: supplier,
}); });
} }