fix: get product for partner and for get

This commit is contained in:
ilham 2021-12-22 15:17:07 +07:00
parent 0bb1800696
commit a0ee6b9040
2 changed files with 34 additions and 11 deletions

View File

@ -125,13 +125,13 @@ export class ProductController {
async findByCategories( async findByCategories(
@Query('page') page: number, @Query('page') page: number,
@Query('pageSize') pageSize: number, @Query('pageSize') pageSize: number,
@Query('categories') categories: string, @Query('sub-category') subcategory: string,
@Request() req, @Request() req,
) { ) {
const data = await this.productService.findAllForPartner( const data = await this.productService.findAllForPartner(
page, page,
pageSize, pageSize,
categories, subcategory,
req.user.username, req.user.username,
); );

View File

@ -73,7 +73,7 @@ export class ProductService {
'product.currentPrice', 'product.currentPrice',
'product.priceHistory', 'product.priceHistory',
'current_price', 'current_price',
'current_price.partner_id is null', 'current_price.partner_id is null and current_price.end_date is NULL',
) )
.select(['product.id']) .select(['product.id'])
.addSelect([ .addSelect([
@ -83,7 +83,9 @@ export class ProductService {
'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 && filterSubCategories.length > 0) { if (subCategories && filterSubCategories.length > 0) {
baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', {
@ -190,19 +192,33 @@ export class ProductService {
async findAllForPartner( async findAllForPartner(
page: number, page: number,
pageSize: number, pageSize: number,
categories: string, subCategories: string,
username: string, username: string,
) { ) {
const user = await this.usersService.findOneByUsername(username); const user = await this.usersService.findOneByUsername(username);
const supplier = await this.supplierService.findByActive(); const supplier = await this.supplierService.findByActive();
let filterSupplier, filterSubCategories;
if (subCategories) {
filterSubCategories = subCategories.split(',').map((data) => data.trim());
} else {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Sub Categories not inlcude',
},
HttpStatus.NOT_FOUND,
);
}
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( .where(
`sub_categories.category_id = :id and product.supplier_id = :supplier_id and product.status = 'ACTIVE'`, `product.sub_categories_id IN (:...subCategoryId) and product.supplier_id = :supplier_id and product.status = 'ACTIVE'`,
{ {
id: categories, subCategoryId: filterSubCategories,
supplier_id: supplier.id, supplier_id: supplier.id,
}, },
) )
@ -211,13 +227,20 @@ export class ProductService {
'product.priceHistory', 'product.priceHistory',
'current_price', 'current_price',
'current_price.partner_id = :id_partner and current_price.end_date is NULL', 'current_price.partner_id = :id_partner and current_price.end_date is NULL',
{
id_partner: user.partner.id,
},
) )
.setParameter('id_partner', user.partner.id); .select(['product.id'])
.addSelect(['product.name', 'product.code', 'sub_categories.name'])
.addSelect(
'(current_price.price + current_price.mark_up_price) as price',
);
const data = await baseQuery const data = await baseQuery
.skip(page * pageSize) .offset(page * 10)
.take(pageSize) .limit(10)
.getMany(); .getRawMany();
const totalData = await baseQuery.getCount(); const totalData = await baseQuery.getCount();