From ad81712bfa65414212345e9455850ad38f5f082a Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 13:35:59 +0700 Subject: [PATCH] feat: add filter on endpoint product all --- src/product/product.controller.ts | 8 +---- src/product/product.service.ts | 48 ++++++++++++++++---------- src/users/supplier/supplier.service.ts | 20 +++++++++++ 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 1dcc540..52d3c0e 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -73,15 +73,9 @@ export class ProductController { async findAll( @Query('page') page: number, @Query('sub-category') subcategory: string, - @Query('category') category: string, @Query('supplier') supplier: string, ) { - const data = await this.productService.findAll( - page, - supplier, - category, - subcategory, - ); + const data = await this.productService.findAll(page, supplier, subcategory); return { ...data, diff --git a/src/product/product.service.ts b/src/product/product.service.ts index a850649..007a995 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -1,9 +1,4 @@ -import { - HttpException, - HttpStatus, - Injectable, - UnauthorizedException, -} from '@nestjs/common'; +import { HttpException, HttpStatus } from '@nestjs/common'; import { EntityNotFoundError, Repository } from 'typeorm'; import { Product } from './entities/product.entity'; import { InjectRepository } from '@nestjs/typeorm'; @@ -15,7 +10,6 @@ import { productType } from '../helper/enum-list'; import { UpdatePriceProductDto } from './dto/product/update-price-product.dto'; import { UsersService } from '../users/users.service'; import { SupplierService } from '../users/supplier/supplier.service'; -import { type } from 'os'; export class ProductService { constructor( @@ -53,18 +47,30 @@ export class ProductService { return this.productRepository.findOneOrFail(result.identifiers[0].id); } - async findAll(page, supplier, categories, subCategories) { - if (supplier == 'null' || !supplier) { - supplier = (await this.supplierService.findByActive()).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()); + } + // if (supplier.length > 0) { + // const dataSupplier = await this.supplierService.findByActiveAll(); + // supplier = dataSupplier.map((item) => item.id); + // } const baseQuery = this.productRepository .createQueryBuilder('product') .leftJoin('product.sub_categories', 'sub_categories') .leftJoin('sub_categories.category', 'category') - .where(`product.supplier_id = :supplier_id`, { - supplier_id: supplier, - }) + .leftJoin('product.supplier', 'supplier') + .where('supplier.status = :status', { status: true }) + // .where(`product.supplier_id = :supplier_id`, { + // supplier_id: In(supplier), + // }) .leftJoinAndMapOne( 'product.currentPrice', 'product.priceHistory', @@ -78,12 +84,18 @@ export class ProductService { 'sub_categories.name', 'category.name', ]); - // .addSelect('current_price.price') - // .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); + // .addSelect('current_price.price') + // .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); - if (subCategories != 'null' && subCategories) { - baseQuery.andWhere('product.sub_categories_id = :id', { - id: subCategories, + if (filterSubCategories.length > 0) { + baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { + subCategoryId: filterSubCategories, + }); + } + + if (filterSupplier.length > 0) { + baseQuery.where('supplier.id IN (:...supplierId)', { + supplierId: filterSupplier, }); } diff --git a/src/users/supplier/supplier.service.ts b/src/users/supplier/supplier.service.ts index 00ff8c0..13699cf 100644 --- a/src/users/supplier/supplier.service.ts +++ b/src/users/supplier/supplier.service.ts @@ -199,4 +199,24 @@ export class SupplierService { } } } + + async findByActiveAll() { + try { + return await this.supplierRepository.find({ + status: true, + }); + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Supplier Data not found', + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } + } }