ppob-backend/src/product/history-price/history-price.service.ts
2022-01-10 00:13:41 +07:00

154 lines
4.1 KiB
TypeScript

import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { EntityNotFoundError, IsNull, Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { ProductHistoryPrice } from '../entities/product-history-price.entity';
import { UpdateUserDto } from '../../users/dto/update-user.dto';
import { hashPassword } from '../../helper/hash_password';
import { COA } from '../../transaction/entities/coa.entity';
@Injectable()
export class ProductHistoryPriceService {
constructor(
@InjectRepository(ProductHistoryPrice)
private productHistoryPriceRepository: Repository<ProductHistoryPrice>,
) {}
async create(dataProduct: ProductHistoryPrice) {
const result = await this.productHistoryPriceRepository.insert(dataProduct);
return result;
}
async getAllActivePriceByProduct(productId: string) {
const result = await this.productHistoryPriceRepository
.createQueryBuilder('product_history_price')
.leftJoin('product_history_price.product', 'product')
.leftJoinAndSelect('product_history_price.partner', 'partner')
.where({ product: productId })
.andWhere('product_history_price.endDate IS NULL')
.getMany();
return result;
}
async updateEndDate(idProduct: string) {
try {
const dataPrice = await this.productHistoryPriceRepository
.createQueryBuilder()
.update(ProductHistoryPrice)
.set({
endDate: new Date(),
})
.where('product_id = :id and endDate is null', { id: idProduct })
.execute();
return dataPrice;
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Failed to update',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
async findOne(product: string, partner: string) {
try {
return await this.productHistoryPriceRepository.findOneOrFail({
where: {
product: product,
endDate: IsNull(),
partner: partner ? partner : IsNull(),
},
});
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Price not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
async findById(id: string) {
try {
return await this.productHistoryPriceRepository.findOneOrFail({
where: {
id: id,
},
relations: ['product'],
});
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Price not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
async findOneByProductId(
page: number,
productId: string,
supplierId: string,
pageSize?: number,
) {
try {
const query = this.productHistoryPriceRepository
.createQueryBuilder('product_history_price')
.leftJoin('product_history_price.product', 'product')
.where({ product: productId })
.andWhere('product_history_price.endDate IS NULL');
if (supplierId !== 'null' && supplierId) {
query.andWhere('product.supplier = :supplierId', {
supplierId: supplierId,
});
}
const data = await query
.orderBy('product_history_price.createdAt', 'DESC')
.skip(page * (pageSize || 10))
.take(pageSize || 10)
.getMany();
const totalData = await query.getCount();
return {
data,
count: totalData,
};
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Product History Price not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
}