fix: get list product by categories
This commit is contained in:
		| @@ -123,6 +123,7 @@ export class ProductService { | |||||||
|               status: it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE', |               status: it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE', | ||||||
|             }); |             }); | ||||||
|           } |           } | ||||||
|  |  | ||||||
|           dataHistoryPrice = await this.productHistoryPrice.findOne({ |           dataHistoryPrice = await this.productHistoryPrice.findOne({ | ||||||
|             product: productData, |             product: productData, | ||||||
|             partner: IsNull(), |             partner: IsNull(), | ||||||
| @@ -227,7 +228,7 @@ export class ProductService { | |||||||
|         'product.currentStatus', |         'product.currentStatus', | ||||||
|         'product.statusHistory', |         'product.statusHistory', | ||||||
|         'history_status', |         'history_status', | ||||||
|             'history_status.deleted_at is NULL' |         'history_status.deleted_at is NULL', | ||||||
|       ) |       ) | ||||||
|       .select(['product.id']) |       .select(['product.id']) | ||||||
|       .addSelect([ |       .addSelect([ | ||||||
| @@ -330,7 +331,7 @@ export class ProductService { | |||||||
|       .addSelect(['product.name', 'product.code', 'sub_categories.name']) |       .addSelect(['product.name', 'product.code', 'sub_categories.name']) | ||||||
|       .addSelect('(current_price.price + current_price.mark_up_price) as price') |       .addSelect('(current_price.price + current_price.mark_up_price) as price') | ||||||
|       .orderBy('price', 'ASC') |       .orderBy('price', 'ASC') | ||||||
|         .distinct() |       .distinct(); | ||||||
|  |  | ||||||
|     if (subCategories != 'null' && subCategories) { |     if (subCategories != 'null' && subCategories) { | ||||||
|       baseQuery.andWhere('product.sub_categories_id = :id', { |       baseQuery.andWhere('product.sub_categories_id = :id', { | ||||||
| @@ -397,13 +398,17 @@ export class ProductService { | |||||||
|         'product.currentStatus', |         'product.currentStatus', | ||||||
|         'product.statusHistory', |         'product.statusHistory', | ||||||
|         'history_status', |         'history_status', | ||||||
|             'history_status.deleted_at is NULL' |         'history_status.partner_id = :id_partner and history_status.deleted_at is NULL', | ||||||
|         ) |  | ||||||
|         .where(`history_status.partner_id = :id_partner and history_status.status = 'ACTIVE'`, |  | ||||||
|         { |         { | ||||||
|           id_partner: user.partner.id, |           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']) |       .select(['product.id']) | ||||||
|       .addSelect([ |       .addSelect([ | ||||||
|         'product.name', |         'product.name', | ||||||
| @@ -416,7 +421,7 @@ export class ProductService { | |||||||
|         'current_price.price as price', |         'current_price.price as price', | ||||||
|       ]) |       ]) | ||||||
|       .addSelect('history_status.status', 'status') |       .addSelect('history_status.status', 'status') | ||||||
|         .distinct() |       .distinct(); | ||||||
|     // .addSelect( |     // .addSelect( | ||||||
|     //   '(current_price.price + current_price.mark_up_price) as price', |     //   '(current_price.price + current_price.mark_up_price) as price', | ||||||
|     // ); |     // ); | ||||||
| @@ -431,14 +436,15 @@ export class ProductService { | |||||||
|     //   }); |     //   }); | ||||||
|     // } |     // } | ||||||
|  |  | ||||||
|  |  | ||||||
|     if (subCategories && filterSubCategories.length > 0) { |     if (subCategories && filterSubCategories.length > 0) { | ||||||
|       baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { |       baseQuery | ||||||
|  |         .where('product.sub_categories_id IN (:...subCategoryId)', { | ||||||
|           subCategoryId: filterSubCategories, |           subCategoryId: filterSubCategories, | ||||||
|       }).andWhere(`history_status.status = 'ACTIVE'`) |         }) | ||||||
|  |         .andWhere(`history_status.status = 'ACTIVE'`); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const newData = [] |     const newData = []; | ||||||
|  |  | ||||||
|     const data = await baseQuery |     const data = await baseQuery | ||||||
|       .offset(page * pageSize) |       .offset(page * pageSize) | ||||||
| @@ -446,23 +452,24 @@ export class ProductService { | |||||||
|       .getRawMany(); |       .getRawMany(); | ||||||
|  |  | ||||||
|     data.map((dataa) => { |     data.map((dataa) => { | ||||||
|         let actualPrice = 0 |       let actualPrice = 0; | ||||||
|  |  | ||||||
|       if (dataa.product_type === 'prepaid') { |       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') { |       if (dataa.product_type === 'postpaid') { | ||||||
|           actualPrice = Number(dataa['admin_price'])- (Number(dataa['partner_fee']) + Number(dataa['markup_price'])) |         actualPrice = | ||||||
|  |           Number(dataa['admin_price']) - | ||||||
|  |           (Number(dataa['partner_fee']) + Number(dataa['markup_price'])); | ||||||
|       } |       } | ||||||
|  |  | ||||||
|         dataa.price = actualPrice |       dataa.price = actualPrice; | ||||||
|  |  | ||||||
|       newData.push({ |       newData.push({ | ||||||
|           ...dataa |         ...dataa, | ||||||
|         }) |       }); | ||||||
|  |     }); | ||||||
|       }) |  | ||||||
|  |  | ||||||
|     const totalData = await baseQuery.getCount(); |     const totalData = await baseQuery.getCount(); | ||||||
|  |  | ||||||
| @@ -495,14 +502,19 @@ export class ProductService { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   async findOneActive(code: string, type: string, roles: string, supplierId: string) { |   async findOneActive( | ||||||
|     if (roles == "Retail") { |     code: string, | ||||||
|  |     type: string, | ||||||
|  |     roles: string, | ||||||
|  |     supplierId: string, | ||||||
|  |   ) { | ||||||
|  |     if (roles == 'Retail') { | ||||||
|       try { |       try { | ||||||
|         return await this.productRepository.findOneOrFail({ |         return await this.productRepository.findOneOrFail({ | ||||||
|           relations: ['supplier'], |           relations: ['supplier'], | ||||||
|           where: { |           where: { | ||||||
|             code: code, |             code: code, | ||||||
|             supplier: supplierId |             supplier: supplierId, | ||||||
|           }, |           }, | ||||||
|         }); |         }); | ||||||
|       } catch (e) { |       } catch (e) { | ||||||
| @@ -519,8 +531,6 @@ export class ProductService { | |||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     } else { |     } else { | ||||||
|  |  | ||||||
|  |  | ||||||
|       try { |       try { | ||||||
|         return await this.productRepository.findOneOrFail({ |         return await this.productRepository.findOneOrFail({ | ||||||
|           relations: ['supplier'], |           relations: ['supplier'], | ||||||
| @@ -541,7 +551,6 @@ export class ProductService { | |||||||
|           throw e; |           throw e; | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|  |  | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -598,6 +607,7 @@ export class ProductService { | |||||||
|     await this.productHistoryStatus.update(dataStatus.id, { |     await this.productHistoryStatus.update(dataStatus.id, { | ||||||
|       status: updateProductDto.status, |       status: updateProductDto.status, | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     const result = await this.productRepository.update(id, { |     const result = await this.productRepository.update(id, { | ||||||
|       name: updateProductDto.name, |       name: updateProductDto.name, | ||||||
|       code: updateProductDto.code, |       code: updateProductDto.code, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user