diff --git a/src/configurable/commission.service.ts b/src/configurable/commission.service.ts index 491e929..4b605a4 100644 --- a/src/configurable/commission.service.ts +++ b/src/configurable/commission.service.ts @@ -34,7 +34,7 @@ export class CommissionService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Commission not found', }, HttpStatus.NOT_FOUND, ); @@ -52,7 +52,7 @@ export class CommissionService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Commission not found', }, HttpStatus.NOT_FOUND, ); diff --git a/src/product/history-price/history-price.service.ts b/src/product/history-price/history-price.service.ts index eb0a183..77e71bb 100644 --- a/src/product/history-price/history-price.service.ts +++ b/src/product/history-price/history-price.service.ts @@ -25,7 +25,7 @@ export class ProductHistoryPriceService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Price not found', }, HttpStatus.NOT_FOUND, ); diff --git a/src/product/product-categories.service.ts b/src/product/product-categories.service.ts index d3c2cdc..6aa1e30 100644 --- a/src/product/product-categories.service.ts +++ b/src/product/product-categories.service.ts @@ -54,7 +54,7 @@ export class ProductCategoriesService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product Categories not found', }, HttpStatus.NOT_FOUND, ); @@ -76,7 +76,7 @@ export class ProductCategoriesService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product Categories not found', }, HttpStatus.NOT_FOUND, ); @@ -97,7 +97,7 @@ export class ProductCategoriesService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product Categories not found', }, HttpStatus.NOT_FOUND, ); @@ -122,7 +122,7 @@ export class ProductCategoriesService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product Categories not found', }, HttpStatus.NOT_FOUND, ); diff --git a/src/product/product-sub-categories.service.ts b/src/product/product-sub-categories.service.ts index 34e79a4..ffc0492 100644 --- a/src/product/product-sub-categories.service.ts +++ b/src/product/product-sub-categories.service.ts @@ -44,10 +44,37 @@ export class ProductSubCategoriesService { ); } - findAll(page) { + async findAll(page, category) { + const baseQuery = this.productSubCategoriesRepository + .createQueryBuilder('product_sub_categories') + .leftJoinAndSelect('product_sub_categories.category', 'category'); + + if (category != 'null' && category) { + baseQuery.where({ + category: category, + }); + } + + const data = await baseQuery + .skip(page * 10) + .take(10) + .getMany(); + + const totalData = await baseQuery.getCount(); + + return { + data, + count: totalData, + }; + } + + findAllByCategories(page, category) { return this.productSubCategoriesRepository.findAndCount({ skip: page * 10, take: 10, + where: { + category: category, + }, relations: ['category'], order: { version: 'DESC', @@ -63,7 +90,7 @@ export class ProductSubCategoriesService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product Sub Categories not found', }, HttpStatus.NOT_FOUND, ); @@ -84,7 +111,7 @@ export class ProductSubCategoriesService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product Sub Categories not found', }, HttpStatus.NOT_FOUND, ); @@ -113,7 +140,7 @@ export class ProductSubCategoriesService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product Sub Categories not found', }, HttpStatus.NOT_FOUND, ); diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index a3bea09..08f09ce 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -69,13 +69,22 @@ export class ProductController { }; } - @Get() - async findAll(@Query('page') page: number) { - const [data, count] = await this.productService.findAll(page); + @Get('all') + 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, + ); return { - data, - count, + ...data, statusCode: HttpStatus.OK, message: 'success', }; @@ -98,12 +107,12 @@ export class ProductController { @Get('by-categories-all') async findByCategoriesAll( @Query('page') page: number, - @Query('categories') categories: string, + @Query('sub-category') subcategory: string, @Query('supplier') supplier: string, ) { const data = await this.productService.findAllBySubCategories( page, - categories, + subcategory, supplier, ); @@ -148,12 +157,14 @@ export class ProductController { } @Get('sub-categories') - async findAllSubCategories(@Query('page') page: number) { - const [data, count] = await this.productSubCategoriesService.findAll(page); + async findAllSubCategories( + @Query('page') page: number, + @Query('category') category: string, + ) { + const data = await this.productSubCategoriesService.findAll(page, category); return { - data, - count, + ...data, statusCode: HttpStatus.OK, message: 'success', }; @@ -168,24 +179,6 @@ export class ProductController { }; } - @Get('categories/:id') - async findOneCategories(@Param('id', ParseUUIDPipe) id: string) { - return { - data: await this.productCategoriesService.findOne(id), - statusCode: HttpStatus.OK, - message: 'success', - }; - } - - @Get('sub-categories/:id') - async findOneSubCategories(@Param('id', ParseUUIDPipe) id: string) { - return { - data: await this.productSubCategoriesService.findOne(id), - statusCode: HttpStatus.OK, - message: 'success', - }; - } - @Put(':id') async update( @Param('id', ParseUUIDPipe) id: string, diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 8e2980d..57f96dc 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 { UsersService } from '../users/users.service'; import { SupplierService } from '../users/supplier/supplier.service'; +import { type } from 'os'; export class ProductService { constructor( @@ -52,15 +53,47 @@ export class ProductService { return this.productRepository.findOneOrFail(result.identifiers[0].id); } - findAll(page) { - return this.productRepository.findAndCount({ - skip: page * 10, - relations: ['sub_categories'], - take: 10, - order: { - version: 'DESC', - }, - }); + async findAll(page, supplier, categories, subCategories) { + if (supplier == 'null' || !supplier) { + supplier = (await this.supplierService.findByActive()).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, + }) + .leftJoinAndMapOne( + 'product.currentPrice', + 'product.priceHistory', + 'current_price', + 'current_price.partner_id is null', + ); + + if (subCategories != 'null' && subCategories) { + baseQuery.where('product.sub_categories_id = :id', { + id: subCategories, + }); + } + + if (categories != 'null' && categories) { + baseQuery.where('sub_categories.category_id = :id', { + id: categories, + }); + } + + const data = await baseQuery + .skip(page * 10) + .take(10) + .getMany(); + + const totalData = await baseQuery.getCount(); + + return { + data, + count: totalData, + }; } async findAllByCategories(page, subCategories, supplier) { @@ -95,16 +128,15 @@ export class ProductService { } async findAllBySubCategories(page, subCategories, supplier) { + if (supplier != 'null' && !supplier) { + supplier = (await this.supplierService.findByActive()).id; + } const baseQuery = this.productRepository .createQueryBuilder('product') .leftJoin('product.sub_categories', 'sub_categories') - .where( - 'sub_categories.category_id = :id and product.supplier_id = :supplier_id', - { - id: subCategories, - supplier_id: supplier, - }, - ) + .where('product.supplier_id = :supplier_id', { + supplier_id: supplier, + }) .leftJoinAndMapOne( 'product.currentPrice', 'product.priceHistory', @@ -112,6 +144,12 @@ export class ProductService { 'current_price.partner_id is null', ); + if (subCategories != 'null' && subCategories) { + baseQuery.where('product.sub_categories_id = :id', { + id: subCategories, + }); + } + const data = await baseQuery .skip(page * 10) .take(10) @@ -178,7 +216,7 @@ export class ProductService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product not found', }, HttpStatus.NOT_FOUND, ); @@ -196,7 +234,7 @@ export class ProductService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product not found', }, HttpStatus.NOT_FOUND, ); @@ -245,7 +283,7 @@ export class ProductService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Product not found', }, HttpStatus.NOT_FOUND, ); diff --git a/src/transaction/coa.service.ts b/src/transaction/coa.service.ts index 33beffc..fac2a6e 100644 --- a/src/transaction/coa.service.ts +++ b/src/transaction/coa.service.ts @@ -66,7 +66,7 @@ export class CoaService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'COA not found', }, HttpStatus.NOT_FOUND, ); diff --git a/src/users/supplier/supplier.service.ts b/src/users/supplier/supplier.service.ts index 7a5155e..68bef24 100644 --- a/src/users/supplier/supplier.service.ts +++ b/src/users/supplier/supplier.service.ts @@ -164,7 +164,7 @@ export class SupplierService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'Supplier not found', }, HttpStatus.NOT_FOUND, ); diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 4235e3f..38fb9e9 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -205,7 +205,7 @@ export class UsersService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'User not found', }, HttpStatus.NOT_FOUND, ); @@ -228,7 +228,7 @@ export class UsersService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'User not found', }, HttpStatus.NOT_FOUND, ); @@ -258,7 +258,7 @@ export class UsersService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'User not found', }, HttpStatus.NOT_FOUND, ); @@ -276,7 +276,7 @@ export class UsersService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'User not found', }, HttpStatus.NOT_FOUND, ); @@ -337,7 +337,7 @@ export class UsersService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'User not found', }, HttpStatus.NOT_FOUND, ); @@ -371,7 +371,7 @@ export class UsersService { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, - error: 'Data not found', + error: 'User not found', }, HttpStatus.NOT_FOUND, );