add: callback transaction

This commit is contained in:
ilham
2021-12-26 00:43:27 +07:00
parent c5e1df20b8
commit 4f50bad562
10 changed files with 384 additions and 71 deletions

View File

@@ -10,6 +10,12 @@ export class ProductHistoryPriceService {
private productHistoryPriceService: Repository<ProductHistoryPrice>,
) {}
async create(dataProduct: ProductHistoryPrice) {
const result = await this.productHistoryPriceService.save(dataProduct);
return result;
}
async findOne(product: string, partner: string) {
try {
return await this.productHistoryPriceService.findOneOrFail({
@@ -34,6 +40,24 @@ export class ProductHistoryPriceService {
}
}
async findById(id: string) {
try {
return await this.productHistoryPriceService.findOneOrFail(id);
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'Price not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
async findOneByProductId(
page: number,
productId: string,

View File

@@ -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',