Merge branch 'development' into 'devops-staging'
fixing: get product by supplier and categories See merge request empatnusabangsa/ppob/ppob-backend!21
This commit is contained in:
commit
d2b792e5ef
|
@ -83,6 +83,7 @@ export class ProductController {
|
|||
async findByCategoriesAll(
|
||||
@Query('page') page: number,
|
||||
@Query('categories') categories: string,
|
||||
@Query('supplier') supplier: string,
|
||||
) {
|
||||
const data = await this.productService.findAllByCategories(
|
||||
page,
|
||||
|
@ -102,7 +103,7 @@ export class ProductController {
|
|||
@Query('categories') categories: string,
|
||||
@Request() req,
|
||||
) {
|
||||
const data = await this.productService.findAllByCategoriesAndPartner(
|
||||
const data = await this.productService.findAllForPartner(
|
||||
page,
|
||||
categories,
|
||||
req.user.username,
|
||||
|
|
|
@ -15,6 +15,7 @@ import { productType } from '../helper/enum-list';
|
|||
import { UpdatePriceProductDto } from './dto/product/update-price-product.dto';
|
||||
import { Raw } from 'typeorm/browser';
|
||||
import { UsersService } from '../users/users.service';
|
||||
import { SupplierService } from '../users/supplier/supplier.service';
|
||||
|
||||
export class ProductService {
|
||||
constructor(
|
||||
|
@ -24,6 +25,7 @@ export class ProductService {
|
|||
private productHistoryPrice: Repository<ProductHistoryPrice>,
|
||||
private productSubCategoriesService: ProductSubCategoriesService,
|
||||
private usersService: UsersService,
|
||||
private supplierService: SupplierService,
|
||||
) {}
|
||||
|
||||
async create(createProductDto: CreateProductDto) {
|
||||
|
@ -62,13 +64,17 @@ export class ProductService {
|
|||
});
|
||||
}
|
||||
|
||||
async findAllByCategories(page, categories) {
|
||||
async findAllByCategories(page, categories, supplier) {
|
||||
const baseQuery = this.productRepository
|
||||
.createQueryBuilder('product')
|
||||
.leftJoin('product.sub_categories', 'sub_categories')
|
||||
.where('sub_categories.category_id = :id', {
|
||||
id: categories,
|
||||
})
|
||||
.where(
|
||||
'sub_categories.category_id = :id and product.supplier_id = :supplier_id',
|
||||
{
|
||||
id: categories,
|
||||
supplier_id: supplier,
|
||||
},
|
||||
)
|
||||
.leftJoinAndMapOne(
|
||||
'product.currentPrice',
|
||||
'product.priceHistory',
|
||||
|
@ -89,19 +95,20 @@ export class ProductService {
|
|||
};
|
||||
}
|
||||
|
||||
async findAllByCategoriesAndPartner(
|
||||
page: number,
|
||||
categories: string,
|
||||
username: string,
|
||||
) {
|
||||
async findAllForPartner(page: number, categories: string, username: string) {
|
||||
const user = await this.usersService.findOneByUsername(username);
|
||||
const supplier = await this.supplierService.findByActive();
|
||||
|
||||
const baseQuery = this.productRepository
|
||||
.createQueryBuilder('product')
|
||||
.leftJoin('product.sub_categories', 'sub_categories')
|
||||
.where('sub_categories.category_id = :id', {
|
||||
id: categories,
|
||||
})
|
||||
.where(
|
||||
'sub_categories.category_id = :id and product.supplier_id = :supplier_id',
|
||||
{
|
||||
id: categories,
|
||||
supplier_id: supplier.id,
|
||||
},
|
||||
)
|
||||
.leftJoinAndMapOne(
|
||||
'product.currentPrice',
|
||||
'product.priceHistory',
|
||||
|
|
|
@ -48,7 +48,7 @@ export class SupplierService {
|
|||
supplierData.id = uuid.v4();
|
||||
supplierData.name = createSupplierDto.name;
|
||||
supplierData.code = createSupplierDto.code;
|
||||
supplierData.status = true;
|
||||
supplierData.status = false;
|
||||
|
||||
await this.connection.transaction(async (manager) => {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user