ppob-backend/src/product/history-price/history-price.service.ts
2021-12-15 15:17:59 +07:00

38 lines
1.1 KiB
TypeScript

import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { EntityNotFoundError, IsNull, Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { ProductCategories } from '../entities/product-category.entity';
import { ProductHistoryPrice } from '../entities/product-history-price.entity';
@Injectable()
export class ProductHistoryPriceService {
constructor(
@InjectRepository(ProductHistoryPrice)
private productHistoryPriceService: Repository<ProductHistoryPrice>,
) {}
async findOne(product: string, partner: string) {
try {
return await this.productHistoryPriceService.findOneOrFail({
where: {
product: product,
endDate: IsNull(),
partner: partner ? partner : IsNull(),
},
});
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Data not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
}