add: callback transaction
This commit is contained in:
@@ -12,6 +12,7 @@ import { UsersService } from '../users/users.service';
|
||||
import { SupplierService } from '../users/supplier/supplier.service';
|
||||
import { parsingFile } from '../helper/csv-parser';
|
||||
import { PartnerService } from '../users/partner/partner.service';
|
||||
import { mapSeries } from 'bluebird';
|
||||
import { isNull } from 'util';
|
||||
|
||||
export class ProductService {
|
||||
@@ -56,82 +57,81 @@ export class ProductService {
|
||||
|
||||
const data = await parsingFile(uploadFile);
|
||||
data.shift();
|
||||
await Promise.all(
|
||||
data.map(async (it) => {
|
||||
let dataHistoryPrice;
|
||||
let partnerData;
|
||||
await mapSeries(data, async (it) => {
|
||||
let dataHistoryPrice;
|
||||
let partnerData;
|
||||
|
||||
const subCategories =
|
||||
await this.productSubCategoriesService.findOneForCSVParser(it[2]);
|
||||
const subCategories =
|
||||
await this.productSubCategoriesService.findOneForCSVParser(it[2]);
|
||||
|
||||
if (!subCategories) {
|
||||
if (!subCategories) {
|
||||
return;
|
||||
}
|
||||
|
||||
const productData = await this.productRepository.findOne({
|
||||
code: it[0],
|
||||
supplier: supplierData,
|
||||
});
|
||||
if (productData) {
|
||||
//TODO : Handle Update Product
|
||||
productData.name = it[1];
|
||||
productData.status = it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE';
|
||||
await this.productRepository.save(productData);
|
||||
|
||||
//TODO : Handle History Price
|
||||
if (it[6] != '-' && it[6] != '') {
|
||||
partnerData = await this.partnerService.findOne(it[6]);
|
||||
dataHistoryPrice = await this.productHistoryPrice.findOne({
|
||||
where: {
|
||||
product: productData.id,
|
||||
partner: partnerData.id,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
dataHistoryPrice = await this.productHistoryPrice.findOne({
|
||||
product: productData,
|
||||
});
|
||||
}
|
||||
|
||||
if (!dataHistoryPrice) {
|
||||
return;
|
||||
}
|
||||
|
||||
const productData = await this.productRepository.findOne({
|
||||
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] != '') {
|
||||
partnerData = await this.partnerService.findOne(it[6]);
|
||||
}
|
||||
const savedProduct = await this.productRepository.insert({
|
||||
name: it[1],
|
||||
code: it[0],
|
||||
status: it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE',
|
||||
sub_categories: subCategories,
|
||||
supplier: supplierData,
|
||||
});
|
||||
if (productData) {
|
||||
//TODO : Handle Update Product
|
||||
productData.name = it[1];
|
||||
productData.status = it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE';
|
||||
await this.productRepository.save(productData);
|
||||
|
||||
//TODO : Handle History Price
|
||||
if (it[6] != '-' && it[6] != '') {
|
||||
partnerData = await this.partnerService.findOne(it[6]);
|
||||
dataHistoryPrice = await this.productHistoryPrice.findOne({
|
||||
product: productData,
|
||||
partner: partnerData,
|
||||
});
|
||||
} else {
|
||||
dataHistoryPrice = await this.productHistoryPrice.findOne({
|
||||
product: productData,
|
||||
});
|
||||
}
|
||||
|
||||
if(!dataHistoryPrice){
|
||||
console.log(productData,"productnya",partnerData);
|
||||
return;
|
||||
}
|
||||
|
||||
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] != '') {
|
||||
partnerData = await this.partnerService.findOne(it[6]);
|
||||
}
|
||||
const savedProduct = await this.productRepository.insert({
|
||||
name: it[1],
|
||||
code: it[0],
|
||||
status: it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE',
|
||||
sub_categories: subCategories,
|
||||
supplier: supplierData,
|
||||
});
|
||||
|
||||
await this.productHistoryPrice.insert({
|
||||
product: savedProduct.identifiers[0],
|
||||
mark_up_price: it[4],
|
||||
price: it[3],
|
||||
type: productType.NORMAL,
|
||||
startDate: new Date(),
|
||||
endDate: null,
|
||||
partner: partnerData,
|
||||
});
|
||||
}
|
||||
}),
|
||||
);
|
||||
return await this.productHistoryPrice.insert({
|
||||
product: savedProduct.identifiers[0],
|
||||
mark_up_price: it[4],
|
||||
price: it[3],
|
||||
type: productType.NORMAL,
|
||||
startDate: new Date(),
|
||||
endDate: null,
|
||||
partner: partnerData,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -162,7 +162,7 @@ export class ProductService {
|
||||
.leftJoin('sub_categories.category', 'category')
|
||||
.leftJoin('product.supplier', 'supplier')
|
||||
.where('supplier.status = :status', { status: true })
|
||||
.leftJoinAndMapOne(
|
||||
.innerJoinAndMapOne(
|
||||
'product.currentPrice',
|
||||
'product.priceHistory',
|
||||
'current_price',
|
||||
|
||||
Reference in New Issue
Block a user