From 6a7c66f461e4585b5a427f932eea6b6027f7e8bb Mon Sep 17 00:00:00 2001 From: mfadiln2018 Date: Tue, 6 Sep 2022 16:44:11 +0700 Subject: [PATCH] fix: get list product by categories --- src/product/product.service.ts | 134 ++++++++++++++++++--------------- 1 file changed, 72 insertions(+), 62 deletions(-) diff --git a/src/product/product.service.ts b/src/product/product.service.ts index c4f9fc8..f113775 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -66,7 +66,7 @@ export class ProductService { let productStatusPartner; const subCategories = - await this.productSubCategoriesService.findOneForCSVParser(it[2]); + await this.productSubCategoriesService.findOneForCSVParser(it[2]); if (!subCategories) { return; @@ -123,6 +123,7 @@ export class ProductService { status: it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE', }); } + dataHistoryPrice = await this.productHistoryPrice.findOne({ product: productData, partner: IsNull(), @@ -223,12 +224,12 @@ export class ProductService { 'current_price', 'current_price.end_date is NULL', ) - .innerJoinAndMapOne( - 'product.currentStatus', - 'product.statusHistory', - 'history_status', - 'history_status.deleted_at is NULL' - ) + .innerJoinAndMapOne( + 'product.currentStatus', + 'product.statusHistory', + 'history_status', + 'history_status.deleted_at is NULL', + ) .select(['product.id']) .addSelect([ 'product.name', @@ -237,7 +238,7 @@ export class ProductService { 'supplier.name', 'category.name', ]) - .addSelect('history_status.status', 'status') + .addSelect('history_status.status', 'status') .addSelect('current_price.price', 'price') .addSelect('current_price.partner_fee', 'partner_fee') .addSelect('current_price.admin_price', 'admin_price') @@ -245,7 +246,7 @@ export class ProductService { '(current_price.price + current_price.mark_up_price) as mark_up_price', ) .orderBy('product.code') - .distinct(); + .distinct(); if (subCategories && filterSubCategories.length > 0) { baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { @@ -312,7 +313,7 @@ export class ProductService { .createQueryBuilder('product') .leftJoin('product.sub_categories', 'sub_categories') - .leftJoin('product.statusHistory', 'status_history') + .leftJoin('product.statusHistory', 'status_history') .leftJoinAndMapOne( 'product.currentPrice', @@ -330,7 +331,7 @@ export class ProductService { .addSelect(['product.name', 'product.code', 'sub_categories.name']) .addSelect('(current_price.price + current_price.mark_up_price) as price') .orderBy('price', 'ASC') - .distinct() + .distinct(); if (subCategories != 'null' && subCategories) { baseQuery.andWhere('product.sub_categories_id = :id', { @@ -393,18 +394,22 @@ export class ProductService { id_partner: user.partner.id, }, ) - .innerJoinAndMapOne( - 'product.currentStatus', - 'product.statusHistory', - 'history_status', - 'history_status.deleted_at is NULL' - ) - .where(`history_status.partner_id = :id_partner and history_status.status = 'ACTIVE'`, - { - id_partner: user.partner.id, - }, - ) - .select(['product.id']) + .innerJoinAndMapOne( + 'product.currentStatus', + 'product.statusHistory', + 'history_status', + 'history_status.partner_id = :id_partner and history_status.deleted_at is NULL', + { + id_partner: user.partner.id, + }, + ) + // .where(`history_status.partner_id = :id_partner`, { + // partner: user.partner.id, + // }) + .where(`history_status.status = 'ACTIVE'`, { + status: 'ACTIVE', + }) + .select(['product.id']) .addSelect([ 'product.name', 'product.code', @@ -415,11 +420,11 @@ export class ProductService { 'current_price.partner_fee as partner_fee', 'current_price.price as price', ]) - .addSelect('history_status.status', 'status') - .distinct() - // .addSelect( - // '(current_price.price + current_price.mark_up_price) as price', - // ); + .addSelect('history_status.status', 'status') + .distinct(); + // .addSelect( + // '(current_price.price + current_price.mark_up_price) as price', + // ); // if ( // subCategories != 'null' && @@ -431,14 +436,15 @@ export class ProductService { // }); // } - - if (subCategories && filterSubCategories.length > 0) { - baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { - subCategoryId: filterSubCategories, - }).andWhere(`history_status.status = 'ACTIVE'`) + if (subCategories && filterSubCategories.length > 0) { + baseQuery + .where('product.sub_categories_id IN (:...subCategoryId)', { + subCategoryId: filterSubCategories, + }) + .andWhere(`history_status.status = 'ACTIVE'`); } - const newData = [] + const newData = []; const data = await baseQuery .offset(page * pageSize) @@ -446,23 +452,24 @@ export class ProductService { .getRawMany(); data.map((dataa) => { - let actualPrice = 0 + let actualPrice = 0; if (dataa.product_type === 'prepaid') { - actualPrice = Number(dataa['price']) + Number(dataa['markup_price']) - } + actualPrice = Number(dataa['price']) + Number(dataa['markup_price']); + } - if (dataa.product_type === 'postpaid') { - actualPrice = Number(dataa['admin_price'])- (Number(dataa['partner_fee']) + Number(dataa['markup_price'])) - } + if (dataa.product_type === 'postpaid') { + actualPrice = + Number(dataa['admin_price']) - + (Number(dataa['partner_fee']) + Number(dataa['markup_price'])); + } - dataa.price = actualPrice + dataa.price = actualPrice; - newData.push({ - ...dataa - }) - - }) + newData.push({ + ...dataa, + }); + }); const totalData = await baseQuery.getCount(); @@ -495,32 +502,35 @@ export class ProductService { } } - async findOneActive(code: string, type: string, roles: string, supplierId: string) { - if (roles == "Retail") { + async findOneActive( + code: string, + type: string, + roles: string, + supplierId: string, + ) { + if (roles == 'Retail') { try { return await this.productRepository.findOneOrFail({ relations: ['supplier'], where: { code: code, - supplier: supplierId + supplier: supplierId, }, }); } catch (e) { if (e instanceof EntityNotFoundError) { throw new HttpException( - { - statusCode: HttpStatus.NOT_FOUND, - error: 'Product not found', - }, - HttpStatus.NOT_FOUND, + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Product not found', + }, + HttpStatus.NOT_FOUND, ); } else { throw e; } } } else { - - try { return await this.productRepository.findOneOrFail({ relations: ['supplier'], @@ -531,17 +541,16 @@ export class ProductService { } catch (e) { if (e instanceof EntityNotFoundError) { throw new HttpException( - { - statusCode: HttpStatus.NOT_FOUND, - error: 'Product not found', - }, - HttpStatus.NOT_FOUND, + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Product not found', + }, + HttpStatus.NOT_FOUND, ); } else { throw e; } } - } } @@ -598,6 +607,7 @@ export class ProductService { await this.productHistoryStatus.update(dataStatus.id, { status: updateProductDto.status, }); + const result = await this.productRepository.update(id, { name: updateProductDto.name, code: updateProductDto.code,