From a0ee6b9040d9efda7acc0d7d658c1dfc44913505 Mon Sep 17 00:00:00 2001 From: ilham Date: Wed, 22 Dec 2021 15:17:07 +0700 Subject: [PATCH] fix: get product for partner and for get --- src/product/product.controller.ts | 4 +-- src/product/product.service.ts | 41 ++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index f26f7e0..557857a 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -125,13 +125,13 @@ export class ProductController { async findByCategories( @Query('page') page: number, @Query('pageSize') pageSize: number, - @Query('categories') categories: string, + @Query('sub-category') subcategory: string, @Request() req, ) { const data = await this.productService.findAllForPartner( page, pageSize, - categories, + subcategory, req.user.username, ); diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 89aad86..f27966b 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -73,7 +73,7 @@ export class ProductService { 'product.currentPrice', 'product.priceHistory', 'current_price', - 'current_price.partner_id is null', + 'current_price.partner_id is null and current_price.end_date is NULL', ) .select(['product.id']) .addSelect([ @@ -83,7 +83,9 @@ export class ProductService { 'category.name', ]) .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 && filterSubCategories.length > 0) { baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { @@ -190,19 +192,33 @@ export class ProductService { async findAllForPartner( page: number, pageSize: number, - categories: string, + subCategories: string, username: string, ) { const user = await this.usersService.findOneByUsername(username); const supplier = await this.supplierService.findByActive(); + let filterSupplier, filterSubCategories; + + if (subCategories) { + filterSubCategories = subCategories.split(',').map((data) => data.trim()); + } else { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Sub Categories not inlcude', + }, + HttpStatus.NOT_FOUND, + ); + } + const baseQuery = this.productRepository .createQueryBuilder('product') .leftJoin('product.sub_categories', 'sub_categories') .where( - `sub_categories.category_id = :id and product.supplier_id = :supplier_id and product.status = 'ACTIVE'`, + `product.sub_categories_id IN (:...subCategoryId) and product.supplier_id = :supplier_id and product.status = 'ACTIVE'`, { - id: categories, + subCategoryId: filterSubCategories, supplier_id: supplier.id, }, ) @@ -211,13 +227,20 @@ export class ProductService { 'product.priceHistory', 'current_price', 'current_price.partner_id = :id_partner and current_price.end_date is NULL', + { + id_partner: user.partner.id, + }, ) - .setParameter('id_partner', user.partner.id); + .select(['product.id']) + .addSelect(['product.name', 'product.code', 'sub_categories.name']) + .addSelect( + '(current_price.price + current_price.mark_up_price) as price', + ); const data = await baseQuery - .skip(page * pageSize) - .take(pageSize) - .getMany(); + .offset(page * 10) + .limit(10) + .getRawMany(); const totalData = await baseQuery.getCount();