Merge branch 'development' into 'devops-staging'

fix: update price

See merge request empatnusabangsa/ppob/ppob-backend!83
This commit is contained in:
ilham dwi pratama 2022-01-03 12:56:37 +00:00
commit 926163bd4b
2 changed files with 41 additions and 7 deletions

View File

@ -2,23 +2,53 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { EntityNotFoundError, IsNull, Repository } from 'typeorm'; import { EntityNotFoundError, IsNull, Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { ProductHistoryPrice } from '../entities/product-history-price.entity'; 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() @Injectable()
export class ProductHistoryPriceService { export class ProductHistoryPriceService {
constructor( constructor(
@InjectRepository(ProductHistoryPrice) @InjectRepository(ProductHistoryPrice)
private productHistoryPriceService: Repository<ProductHistoryPrice>, private productHistoryPriceRepository: Repository<ProductHistoryPrice>,
) {} ) {}
async create(dataProduct: ProductHistoryPrice) { async create(dataProduct: ProductHistoryPrice) {
const result = await this.productHistoryPriceService.save(dataProduct); const result = await this.productHistoryPriceRepository.insert(dataProduct);
return result; 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) { async findOne(product: string, partner: string) {
try { try {
return await this.productHistoryPriceService.findOneOrFail({ return await this.productHistoryPriceRepository.findOneOrFail({
where: { where: {
product: product, product: product,
endDate: IsNull(), endDate: IsNull(),
@ -42,7 +72,7 @@ export class ProductHistoryPriceService {
async findById(id: string) { async findById(id: string) {
try { try {
return await this.productHistoryPriceService.findOneOrFail({ return await this.productHistoryPriceRepository.findOneOrFail({
where: { where: {
id: id, id: id,
}, },
@ -70,7 +100,7 @@ export class ProductHistoryPriceService {
pageSize?: number, pageSize?: number,
) { ) {
try { try {
const query = this.productHistoryPriceService const query = this.productHistoryPriceRepository
.createQueryBuilder('product_history_price') .createQueryBuilder('product_history_price')
.leftJoin('product_history_price.product', 'product') .leftJoin('product_history_price.product', 'product')
.where({ product: productId }) .where({ product: productId })

View File

@ -522,11 +522,15 @@ export class TransactionService {
let newProductPrice = new ProductHistoryPrice(); let newProductPrice = new ProductHistoryPrice();
newProductPrice = product_price;
newProductPrice.id = uuid.v4(); newProductPrice.id = uuid.v4();
newProductPrice.type = product_price.type;
newProductPrice.price = hitSupplier.harga; newProductPrice.price = hitSupplier.harga;
newProductPrice.mark_up_price = product_price.mark_up_price;
newProductPrice.startDate = new Date();
newProductPrice.product = product;
newProductPrice.partner = product_price.partner;
await this.productHistoryPriceService.create(product_price); await this.productHistoryPriceService.updateEndDate(product.id);
await this.productHistoryPriceService.create(newProductPrice); await this.productHistoryPriceService.create(newProductPrice);
} }