diff --git a/src/product/product-sub-categories.service.ts b/src/product/product-sub-categories.service.ts index ffc0492..6ad2644 100644 --- a/src/product/product-sub-categories.service.ts +++ b/src/product/product-sub-categories.service.ts @@ -1,5 +1,5 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; -import { EntityNotFoundError, Repository } from 'typeorm'; +import { EntityNotFoundError, In, Repository } from 'typeorm'; import { ProductSubCategories } from './entities/product-sub-category.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto'; @@ -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 .createQueryBuilder('product_sub_categories') .leftJoinAndSelect('product_sub_categories.category', 'category'); - if (category != 'null' && category) { + if (category && filterCategories.length > 0) { baseQuery.where({ - category: category, + category: In(filterCategories), }); } diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 1dcc540..0539d81 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -73,14 +73,12 @@ 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, + JSON.parse(supplier), + JSON.parse(subcategory), ); return { @@ -161,7 +159,10 @@ export class ProductController { @Query('page') page: number, @Query('category') category: string, ) { - const data = await this.productSubCategoriesService.findAll(page, category); + const data = await this.productSubCategoriesService.findAll( + page, + JSON.parse(category), + ); return { ...data, diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 5077910..89aad86 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,28 @@ 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, filterSubCategories; + if (supplier) { + filterSupplier = supplier.split(',').map((data) => data.trim()); } + if (subCategories) { + 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', @@ -81,9 +85,15 @@ export class ProductService { .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 (subCategories && filterSubCategories.length > 0) { + baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { + subCategoryId: filterSubCategories, + }); + } + + if (supplier && 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; + } + } + } }