From 1bfd132a2ee2458ae5b2070258d44ef0d1eb4c68 Mon Sep 17 00:00:00 2001 From: ilham Date: Sun, 26 Dec 2021 12:02:56 +0700 Subject: [PATCH] fix: update and create product by csv --- src/product/product.service.ts | 38 +++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 55573ef..e4bcfb0 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -1,5 +1,5 @@ import { HttpException, HttpStatus } from '@nestjs/common'; -import { EntityNotFoundError, Repository } from 'typeorm'; +import { EntityNotFoundError, IsNull, Repository } from 'typeorm'; import { Product } from './entities/product.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { CreateProductDto } from './dto/product/create-product.dto'; @@ -87,27 +87,37 @@ export class ProductService { partner: partnerData.id, }, }); + + } else { dataHistoryPrice = await this.productHistoryPrice.findOne({ product: productData, + partner: IsNull(), }); } if (!dataHistoryPrice) { - return; + await this.productHistoryPrice.insert({ + product: productData, + mark_up_price: it[4], + price: it[3], + type: productType.NORMAL, + startDate: new Date(), + partner: it[6] != '-' ? partnerData : null, + }); + } else { + dataHistoryPrice.endDate = new Date(); + await this.productHistoryPrice.save(dataHistoryPrice); + + await this.productHistoryPrice.insert({ + product: productData, + mark_up_price: it[4], + price: it[3], + type: productType.NORMAL, + startDate: new Date(), + partner: it[6] != '-' ? partnerData : null, + }); } - - dataHistoryPrice.endDate = new Date(); - await this.productHistoryPrice.save(dataHistoryPrice); - - await this.productHistoryPrice.insert({ - product: productData, - mark_up_price: it[4], - price: it[3], - type: productType.NORMAL, - startDate: new Date(), - partner: it[6] != '-' ? partnerData : null, - }); } else { let partnerData; if (it[6] != '-' && it[6] != '') {