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(
@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,
);
const data = await this.productService.findAll(page, supplier, subcategory);
return {
...data,

View File

@ -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,30 @@ 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 = [];
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
.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',
@ -78,12 +84,18 @@ export class ProductService {
'sub_categories.name',
'category.name',
]);
// .addSelect('current_price.price')
// .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price');
// .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 (filterSubCategories.length > 0) {
baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', {
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;
}
}
}
}