fixing: get product by supplier and categories

This commit is contained in:
ilham 2021-12-14 22:15:21 +07:00
parent a3ce252e3c
commit 6951d052ff
3 changed files with 42 additions and 14 deletions

View File

@ -83,6 +83,7 @@ export class ProductController {
async findByCategoriesAll( async findByCategoriesAll(
@Query('page') page: number, @Query('page') page: number,
@Query('categories') categories: string, @Query('categories') categories: string,
@Query('supplier') supplier: string,
) { ) {
const data = await this.productService.findAllByCategories( const data = await this.productService.findAllByCategories(
page, page,
@ -102,7 +103,7 @@ export class ProductController {
@Query('categories') categories: string, @Query('categories') categories: string,
@Request() req, @Request() req,
) { ) {
const data = await this.productService.findAllByCategoriesAndPartner( const data = await this.productService.findAllForPartner(
page, page,
categories, categories,
req.user.username, req.user.username,

View File

@ -15,6 +15,7 @@ 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 { Raw } from 'typeorm/browser'; import { Raw } from 'typeorm/browser';
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
import { SupplierService } from '../users/supplier/supplier.service';
export class ProductService { export class ProductService {
constructor( constructor(
@ -24,6 +25,7 @@ export class ProductService {
private productHistoryPrice: Repository<ProductHistoryPrice>, private productHistoryPrice: Repository<ProductHistoryPrice>,
private productSubCategoriesService: ProductSubCategoriesService, private productSubCategoriesService: ProductSubCategoriesService,
private usersService: UsersService, private usersService: UsersService,
private supplierService: SupplierService,
) {} ) {}
async create(createProductDto: CreateProductDto) { async create(createProductDto: CreateProductDto) {
@ -62,13 +64,17 @@ export class ProductService {
}); });
} }
async findAllByCategories(page, categories) { async findAllByCategories(page, categories, supplier) {
const baseQuery = this.productRepository const baseQuery = this.productRepository
.createQueryBuilder('product') .createQueryBuilder('product')
.leftJoin('product.sub_categories', 'sub_categories') .leftJoin('product.sub_categories', 'sub_categories')
.where('sub_categories.category_id = :id', { .where(
'sub_categories.category_id = :id and product.supplier_id = :supplier_id',
{
id: categories, id: categories,
}) supplier_id: supplier,
},
)
.leftJoinAndMapOne( .leftJoinAndMapOne(
'product.currentPrice', 'product.currentPrice',
'product.priceHistory', 'product.priceHistory',
@ -89,19 +95,20 @@ export class ProductService {
}; };
} }
async findAllByCategoriesAndPartner( async findAllForPartner(page: number, categories: string, username: string) {
page: number,
categories: string,
username: string,
) {
const user = await this.usersService.findOneByUsername(username); const user = await this.usersService.findOneByUsername(username);
const supplier = await this.supplierService.findByActive();
const baseQuery = this.productRepository const baseQuery = this.productRepository
.createQueryBuilder('product') .createQueryBuilder('product')
.leftJoin('product.sub_categories', 'sub_categories') .leftJoin('product.sub_categories', 'sub_categories')
.where('sub_categories.category_id = :id', { .where(
'sub_categories.category_id = :id and product.supplier_id = :supplier_id',
{
id: categories, id: categories,
}) supplier_id: supplier.id,
},
)
.leftJoinAndMapOne( .leftJoinAndMapOne(
'product.currentPrice', 'product.currentPrice',
'product.priceHistory', 'product.priceHistory',

View File

@ -48,7 +48,7 @@ export class SupplierService {
supplierData.id = uuid.v4(); supplierData.id = uuid.v4();
supplierData.name = createSupplierDto.name; supplierData.name = createSupplierDto.name;
supplierData.code = createSupplierDto.code; supplierData.code = createSupplierDto.code;
supplierData.status = true; supplierData.status = false;
await this.connection.transaction(async (manager) => { await this.connection.transaction(async (manager) => {
const result = await manager.insert(Supplier, supplierData); const result = await manager.insert(Supplier, supplierData);
@ -158,4 +158,24 @@ export class SupplierService {
} }
} }
} }
async findByActive() {
try {
return await this.supplierRepository.findOneOrFail({
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;
}
}
}
} }