diff --git a/app/models/repository_stock_value.rb b/app/models/repository_stock_value.rb index c0ba13806..80fea00d8 100644 --- a/app/models/repository_stock_value.rb +++ b/app/models/repository_stock_value.rb @@ -18,6 +18,8 @@ class RepositoryStockValue < ApplicationRecord validates :low_stock_threshold, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true before_save :update_consumption_stock_units, if: :repository_stock_unit_item_id_changed? + after_save :send_low_stock_notification, if: -> { status == :low } + after_create do next if is_a?(RepositoryStockConsumptionValue) @@ -199,4 +201,8 @@ class RepositoryStockValue < ApplicationRecord .my_module_repository_rows .update_all(repository_stock_unit_item_id: repository_stock_unit_item_id) end + + def send_low_stock_notification + LowStockNotification.send_notifications({ repository_row_id: repository_cell.repository_row_id }) + end end diff --git a/app/notifications/base_notification.rb b/app/notifications/base_notification.rb index 96fa85e86..2445d1889 100644 --- a/app/notifications/base_notification.rb +++ b/app/notifications/base_notification.rb @@ -4,7 +4,8 @@ class BaseNotification < Noticed::Base deliver_by :database, if: :database_notification? def self.send_notifications(params, later: false) - recipients_class = "Recipients::#{NotificationExtends::NOTIFICATIONS_TYPES[params[:type]][:recipients_module]}".constantize + recipients_class = + "Recipients::#{NotificationExtends::NOTIFICATIONS_TYPES[subtype][:recipients_module]}".constantize recipients_class.new(params).recipients.each do |recipient| if later with(params).deliver_later(recipient) @@ -14,6 +15,10 @@ class BaseNotification < Noticed::Base end end + def self.subtype + params[:type] + end + def subject; end private @@ -23,7 +28,8 @@ class BaseNotification < Noticed::Base end def notification_subgroup - NotificationExtends::NOTIFICATIONS_GROUPS.values.reduce({}, :merge) - .find { |_sg, n| n.include?(params[:type].to_sym) }[0] + NotificationExtends::NOTIFICATIONS_GROUPS.values.reduce({}, :merge).find do |_sg, n| + n.include?(self.class.subtype.to_sym) + end[0] end end diff --git a/app/notifications/low_stock_notification.rb b/app/notifications/low_stock_notification.rb new file mode 100644 index 000000000..5ec7bb774 --- /dev/null +++ b/app/notifications/low_stock_notification.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class LowStockNotification < BaseNotification + def message + I18n.t( + 'notifications.notification.item_low_stock_reminder_html', + repository_row_name: subject.name + ) + end + + def self.subtype + :item_low_stock_reminder + end + + def title; end + + def subject + RepositoryRow.find(params[:repository_row_id]) + end +end diff --git a/app/notifications/recipients/item_creator_recipients.rb b/app/notifications/recipients/item_creator_recipients.rb index a5bbcb3a5..4296fc9a5 100644 --- a/app/notifications/recipients/item_creator_recipients.rb +++ b/app/notifications/recipients/item_creator_recipients.rb @@ -6,6 +6,6 @@ class Recipients::ItemCreatorRecipients end def recipients - [] + [RepositoryRow.find(@params[:repository_row_id]).created_by] end end diff --git a/config/initializers/extends/notifcation_extends.rb b/config/initializers/extends/notification_extends.rb similarity index 99% rename from config/initializers/extends/notifcation_extends.rb rename to config/initializers/extends/notification_extends.rb index 64500534d..74e751e31 100644 --- a/config/initializers/extends/notifcation_extends.rb +++ b/config/initializers/extends/notification_extends.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class NotificationExtends NOTIFICATIONS_TYPES = { designate_user_to_my_module_activity: { diff --git a/config/locales/en.yml b/config/locales/en.yml index d69167837..59bc44a7b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3339,8 +3339,8 @@ en: other_team_invitation: "You are invited or removed from the team" notification: my_module_due_date_reminder_html: "Due date for %{my_module_name} is coming up" - item_low_stock_reminder_html: "Item %{repository_item_name} is running low" - item_date_reminder_html: "Date reminder for %{repository_item_name} is coming up in %{value} %{units}" + item_low_stock_reminder_html: "Item %{repository_row_name} is running low" + item_date_reminder_html: "Date reminder for %{repository_row_name} is coming up in %{value} %{units}" deliver: download_link: "Download link:" download_text: "Click the link to download the file."