From cfca06ecc554e20da0cf2b490519cd52920795cb Mon Sep 17 00:00:00 2001 From: ilham Date: Mon, 3 Jan 2022 19:56:04 +0700 Subject: [PATCH] fix: update price --- .../history-price/history-price.service.ts | 40 ++++++++++++++++--- src/transaction/transaction.service.ts | 8 +++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/product/history-price/history-price.service.ts b/src/product/history-price/history-price.service.ts index 698b369..1e7ccac 100644 --- a/src/product/history-price/history-price.service.ts +++ b/src/product/history-price/history-price.service.ts @@ -2,23 +2,53 @@ 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 productHistoryPriceService: Repository, + private productHistoryPriceRepository: Repository, ) {} async create(dataProduct: ProductHistoryPrice) { - const result = await this.productHistoryPriceService.save(dataProduct); + const result = await this.productHistoryPriceRepository.insert(dataProduct); 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.productHistoryPriceService.findOneOrFail({ + return await this.productHistoryPriceRepository.findOneOrFail({ where: { product: product, endDate: IsNull(), @@ -42,7 +72,7 @@ export class ProductHistoryPriceService { async findById(id: string) { try { - return await this.productHistoryPriceService.findOneOrFail({ + return await this.productHistoryPriceRepository.findOneOrFail({ where: { id: id, }, @@ -70,7 +100,7 @@ export class ProductHistoryPriceService { pageSize?: number, ) { try { - const query = this.productHistoryPriceService + const query = this.productHistoryPriceRepository .createQueryBuilder('product_history_price') .leftJoin('product_history_price.product', 'product') .where({ product: productId }) diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index f7402b5..7b9bae4 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -522,11 +522,15 @@ export class TransactionService { let newProductPrice = new ProductHistoryPrice(); - newProductPrice = product_price; newProductPrice.id = uuid.v4(); + newProductPrice.type = product_price.type; 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); }