From 4f03fee759c2416902656812e2a3308fb74d481d Mon Sep 17 00:00:00 2001 From: mfadiln2018 Date: Mon, 29 Aug 2022 15:46:02 +0700 Subject: [PATCH] Feat: add get status from product history status --- src/product/entities/product.entity.ts | 2 ++ src/product/product.service.ts | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/product/entities/product.entity.ts b/src/product/entities/product.entity.ts index 3a2d5aa..1f6fda9 100644 --- a/src/product/entities/product.entity.ts +++ b/src/product/entities/product.entity.ts @@ -65,6 +65,8 @@ export class Product extends BaseModel { ) statusHistory: ProductHistoryStatus; + currentStatus: ProductHistoryStatus; + @OneToMany( () => { return ProductHistoryPrice; diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 1afedee..5d9fb8c 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -194,6 +194,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' + ) .select(['product.id']) .addSelect([ 'product.name', @@ -201,8 +207,8 @@ export class ProductService { 'sub_categories.name', 'supplier.name', 'category.name', - 'product.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') @@ -282,7 +288,6 @@ export class ProductService { 'current_price', 'current_price.partner_id is NULL and current_price.end_date is NULL', ) - .leftJoin('product.statusHistory', 'status_history') .where( `product.supplier_id = :supplier_id and status_history.status = 'ACTIVE'`, { @@ -346,7 +351,6 @@ export class ProductService { 'supplier', 'supplier.status = true', ) - .where(`product.status = 'ACTIVE'`) .innerJoinAndMapOne( 'product.currentPrice', 'product.priceHistory', @@ -356,18 +360,25 @@ export class ProductService { id_partner: user.partner.id, }, ) - .select(['product.id']) + .innerJoinAndMapOne( + 'product.currentStatus', + 'product.statusHistory', + 'history_status', + 'history_status.deleted_at is NULL' + ) + .where(`history_status.status = 'ACTIVE'`) + .select(['product.id']) .addSelect([ 'product.name', 'product.code', 'product.type', - 'product.status', 'sub_categories.name', 'current_price.admin_price as admin_price', 'current_price.mark_up_price as markup_price', 'current_price.partner_fee as partner_fee', 'current_price.price as price', ]) + .addSelect('history_status.status', 'status') // .addSelect( // '(current_price.price + current_price.mark_up_price) as price', // ); @@ -386,7 +397,7 @@ export class ProductService { if (subCategories && filterSubCategories.length > 0) { baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { subCategoryId: filterSubCategories, - }).andWhere(`product.status = 'ACTIVE'`) + }).andWhere(`history_status.status = 'ACTIVE'`) } const newData = []