fixing: get product

This commit is contained in:
ilham
2021-12-14 21:48:21 +07:00
parent ff60406af2
commit 6f1f82cc03
11 changed files with 198 additions and 36 deletions

View File

@@ -1,4 +1,9 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
HttpException,
HttpStatus,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { EntityNotFoundError, Repository } from 'typeorm';
import { Product } from './entities/product.entity';
import { InjectRepository } from '@nestjs/typeorm';
@@ -8,6 +13,8 @@ import { UpdateProductDto } from './dto/product/update-product.dto';
import { ProductHistoryPrice } from './entities/product-history-price.entity';
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';
export class ProductService {
constructor(
@@ -16,6 +23,7 @@ export class ProductService {
@InjectRepository(ProductHistoryPrice)
private productHistoryPrice: Repository<ProductHistoryPrice>,
private productSubCategoriesService: ProductSubCategoriesService,
private usersService: UsersService,
) {}
async create(createProductDto: CreateProductDto) {
@@ -54,18 +62,65 @@ export class ProductService {
});
}
findAllByCategories(page, categories) {
return this.productRepository.findAndCount({
join: {
alias: 'sub_categories',
innerJoin: { sub_categories: 'roles.users' },
},
skip: page * 10,
take: 10,
order: {
version: 'DESC',
},
});
async findAllByCategories(page, categories) {
const baseQuery = this.productRepository
.createQueryBuilder('product')
.leftJoin('product.sub_categories', 'sub_categories')
.where('sub_categories.category_id = :id', {
id: categories,
})
.leftJoinAndMapOne(
'product.currentPrice',
'product.priceHistory',
'current_price',
'current_price.partner_id is null',
);
const data = await baseQuery
.skip(page * 10)
.take(10)
.getMany();
const totalData = await baseQuery.getCount();
return {
data,
count: totalData,
};
}
async findAllByCategoriesAndPartner(
page: number,
categories: string,
username: string,
) {
const user = await this.usersService.findOneByUsername(username);
const baseQuery = this.productRepository
.createQueryBuilder('product')
.leftJoin('product.sub_categories', 'sub_categories')
.where('sub_categories.category_id = :id', {
id: categories,
})
.leftJoinAndMapOne(
'product.currentPrice',
'product.priceHistory',
'current_price',
'current_price.partner_id = :id_partner and current_price.end_date is NULL',
)
.setParameter('id_partner', user.partner.id);
const data = await baseQuery
.skip(page * 10)
.take(10)
.getMany();
const totalData = await baseQuery.getCount();
return {
data,
count: totalData,
};
}
async findOne(code: string) {