From 9ddc21619b67990fe0e3495585fc94d799a0d9cd Mon Sep 17 00:00:00 2001 From: Du Peng Date: Thu, 13 Oct 2022 14:42:32 +0200 Subject: [PATCH] (bug) fix product cannot update low stock alert --- app/services/product_service.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/services/product_service.rb b/app/services/product_service.rb index 85ce4f963..6e2a4d9e7 100644 --- a/app/services/product_service.rb +++ b/app/services/product_service.rb @@ -50,7 +50,8 @@ class ProductService } end || {} product.stock = remaining_stock - notify_on_low_stock(product, stock_movements) + notify_on_low_stock(product) if product.low_stock_alert + product end def create(product_params, stock_movement_params = []) @@ -188,12 +189,11 @@ class ProductService movements.where(reason: filters[:reason]) end - def notify_on_low_stock(product, stock_movements) + def notify_on_low_stock(product) return product unless product.low_stock_threshold - affected_stocks = stock_movements&.map { |m| m[:stock_type] }&.uniq - if (remaining_stock[:internal] < product.low_stock_threshold && affected_stocks&.include?('internal')) || - (remaining_stock[:external] < product.low_stock_threshold && affected_stocks&.include?('external')) + if (product.stock['internal'] < product.low_stock_threshold) || + (product.stock['external'] < product.low_stock_threshold) NotificationCenter.call type: 'notify_admin_low_stock_threshold', receiver: User.admins_and_managers, attached_object: product