feat: add filter on endpoint product all

This commit is contained in:
caturbgs 2021-12-22 13:35:59 +07:00
parent d8b9202fb7
commit ad81712bfa
3 changed files with 51 additions and 25 deletions

View File

@ -73,15 +73,9 @@ export class ProductController {
async findAll( async findAll(
@Query('page') page: number, @Query('page') page: number,
@Query('sub-category') subcategory: string, @Query('sub-category') subcategory: string,
@Query('category') category: string,
@Query('supplier') supplier: string, @Query('supplier') supplier: string,
) { ) {
const data = await this.productService.findAll( const data = await this.productService.findAll(page, supplier, subcategory);
page,
supplier,
category,
subcategory,
);
return { return {
...data, ...data,

View File

@ -1,9 +1,4 @@
import { import { HttpException, HttpStatus } from '@nestjs/common';
HttpException,
HttpStatus,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { EntityNotFoundError, Repository } from 'typeorm'; import { EntityNotFoundError, Repository } from 'typeorm';
import { Product } from './entities/product.entity'; import { Product } from './entities/product.entity';
import { InjectRepository } from '@nestjs/typeorm'; 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 { UpdatePriceProductDto } from './dto/product/update-price-product.dto';
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
import { SupplierService } from '../users/supplier/supplier.service'; import { SupplierService } from '../users/supplier/supplier.service';
import { type } from 'os';
export class ProductService { export class ProductService {
constructor( constructor(
@ -53,18 +47,30 @@ export class ProductService {
return this.productRepository.findOneOrFail(result.identifiers[0].id); return this.productRepository.findOneOrFail(result.identifiers[0].id);
} }
async findAll(page, supplier, categories, subCategories) { async findAll(page: number, supplier: string, subCategories: string) {
if (supplier == 'null' || !supplier) { let filterSupplier = [];
supplier = (await this.supplierService.findByActive()).id; 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 const baseQuery = this.productRepository
.createQueryBuilder('product') .createQueryBuilder('product')
.leftJoin('product.sub_categories', 'sub_categories') .leftJoin('product.sub_categories', 'sub_categories')
.leftJoin('sub_categories.category', 'category') .leftJoin('sub_categories.category', 'category')
.where(`product.supplier_id = :supplier_id`, { .leftJoin('product.supplier', 'supplier')
supplier_id: supplier, .where('supplier.status = :status', { status: true })
}) // .where(`product.supplier_id = :supplier_id`, {
// supplier_id: In(supplier),
// })
.leftJoinAndMapOne( .leftJoinAndMapOne(
'product.currentPrice', 'product.currentPrice',
'product.priceHistory', 'product.priceHistory',
@ -78,12 +84,18 @@ export class ProductService {
'sub_categories.name', 'sub_categories.name',
'category.name', 'category.name',
]); ]);
// .addSelect('current_price.price') // .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 != 'null' && subCategories) { if (filterSubCategories.length > 0) {
baseQuery.andWhere('product.sub_categories_id = :id', { baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', {
id: subCategories, subCategoryId: filterSubCategories,
});
}
if (filterSupplier.length > 0) {
baseQuery.where('supplier.id IN (:...supplierId)', {
supplierId: filterSupplier,
}); });
} }

View File

@ -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;
}
}
}
} }