From 6951d052ff711d65ad80c0db1e03df6b4b876420 Mon Sep 17 00:00:00 2001 From: ilham Date: Tue, 14 Dec 2021 22:15:21 +0700 Subject: [PATCH] fixing: get product by supplier and categories --- src/product/product.controller.ts | 3 ++- src/product/product.service.ts | 31 ++++++++++++++++---------- src/users/supplier/supplier.service.ts | 22 +++++++++++++++++- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index cdf0348..6476aa9 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -83,6 +83,7 @@ export class ProductController { async findByCategoriesAll( @Query('page') page: number, @Query('categories') categories: string, + @Query('supplier') supplier: string, ) { const data = await this.productService.findAllByCategories( page, @@ -102,7 +103,7 @@ export class ProductController { @Query('categories') categories: string, @Request() req, ) { - const data = await this.productService.findAllByCategoriesAndPartner( + const data = await this.productService.findAllForPartner( page, categories, req.user.username, diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 51f1790..7ea059a 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -15,6 +15,7 @@ import { productType } from '../helper/enum-list'; import { UpdatePriceProductDto } from './dto/product/update-price-product.dto'; import { Raw } from 'typeorm/browser'; import { UsersService } from '../users/users.service'; +import { SupplierService } from '../users/supplier/supplier.service'; export class ProductService { constructor( @@ -24,6 +25,7 @@ export class ProductService { private productHistoryPrice: Repository, private productSubCategoriesService: ProductSubCategoriesService, private usersService: UsersService, + private supplierService: SupplierService, ) {} async create(createProductDto: CreateProductDto) { @@ -62,13 +64,17 @@ export class ProductService { }); } - async findAllByCategories(page, categories) { + async findAllByCategories(page, categories, supplier) { const baseQuery = this.productRepository .createQueryBuilder('product') .leftJoin('product.sub_categories', 'sub_categories') - .where('sub_categories.category_id = :id', { - id: categories, - }) + .where( + 'sub_categories.category_id = :id and product.supplier_id = :supplier_id', + { + id: categories, + supplier_id: supplier, + }, + ) .leftJoinAndMapOne( 'product.currentPrice', 'product.priceHistory', @@ -89,19 +95,20 @@ export class ProductService { }; } - async findAllByCategoriesAndPartner( - page: number, - categories: string, - username: string, - ) { + async findAllForPartner(page: number, categories: string, username: string) { const user = await this.usersService.findOneByUsername(username); + const supplier = await this.supplierService.findByActive(); const baseQuery = this.productRepository .createQueryBuilder('product') .leftJoin('product.sub_categories', 'sub_categories') - .where('sub_categories.category_id = :id', { - id: categories, - }) + .where( + 'sub_categories.category_id = :id and product.supplier_id = :supplier_id', + { + id: categories, + supplier_id: supplier.id, + }, + ) .leftJoinAndMapOne( 'product.currentPrice', 'product.priceHistory', diff --git a/src/users/supplier/supplier.service.ts b/src/users/supplier/supplier.service.ts index 491b858..dcec953 100644 --- a/src/users/supplier/supplier.service.ts +++ b/src/users/supplier/supplier.service.ts @@ -48,7 +48,7 @@ export class SupplierService { supplierData.id = uuid.v4(); supplierData.name = createSupplierDto.name; supplierData.code = createSupplierDto.code; - supplierData.status = true; + supplierData.status = false; await this.connection.transaction(async (manager) => { const result = await manager.insert(Supplier, supplierData); @@ -158,4 +158,24 @@ export class SupplierService { } } } + + async findByActive() { + try { + return await this.supplierRepository.findOneOrFail({ + 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; + } + } + } }