add: parser upload product
This commit is contained in:
@@ -105,6 +105,14 @@ export class ProductSubCategoriesService {
|
||||
}
|
||||
}
|
||||
|
||||
async findOneForCSVParser(code: string) {
|
||||
return await this.productSubCategoriesRepository.findOne({
|
||||
where: {
|
||||
code: code,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async update(
|
||||
id: string,
|
||||
updateCategoriesProductDto: UpdateSubCategoriesProductDto,
|
||||
|
||||
@@ -92,10 +92,7 @@ export class ProductController {
|
||||
|
||||
@Get('test')
|
||||
async test(@Request() req) {
|
||||
const data = await this.productHistoryPriceService.findOne(
|
||||
'4d3b328c-3ce4-4c84-84bb-cd9c36e85a45',
|
||||
'27effb3e-0351-428a-ba7f-08a21c54e16a',
|
||||
);
|
||||
const data = await this.productService.processUploadCSV();
|
||||
|
||||
return {
|
||||
data,
|
||||
|
||||
@@ -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';
|
||||
@@ -10,6 +10,8 @@ import { productType } from '../helper/enum-list';
|
||||
import { UpdatePriceProductDto } from './dto/product/update-price-product.dto';
|
||||
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';
|
||||
|
||||
export class ProductService {
|
||||
constructor(
|
||||
@@ -20,6 +22,7 @@ export class ProductService {
|
||||
private productSubCategoriesService: ProductSubCategoriesService,
|
||||
private usersService: UsersService,
|
||||
private supplierService: SupplierService,
|
||||
private partnerService: PartnerService,
|
||||
) {}
|
||||
|
||||
async create(createProductDto: CreateProductDto) {
|
||||
@@ -47,6 +50,83 @@ export class ProductService {
|
||||
return this.productRepository.findOneOrFail(result.identifiers[0].id);
|
||||
}
|
||||
|
||||
async processUploadCSV() {
|
||||
const data = await parsingFile('');
|
||||
data.shift();
|
||||
await Promise.all(
|
||||
data.map(async (it) => {
|
||||
let dataHistoryPrice;
|
||||
let partnerData;
|
||||
|
||||
const subCategories =
|
||||
await this.productSubCategoriesService.findOneForCSVParser(it[2]);
|
||||
|
||||
if (!subCategories) {
|
||||
return;
|
||||
}
|
||||
|
||||
const productData = await this.productRepository.findOne({
|
||||
code: it[0],
|
||||
});
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
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(),
|
||||
endDate: null,
|
||||
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,
|
||||
});
|
||||
|
||||
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: it[6] != '-' ? partnerData : null,
|
||||
});
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
async findAll(
|
||||
page: number,
|
||||
supplier: string,
|
||||
|
||||
Reference in New Issue
Block a user