Merge pull request #6485 from artoscinote/ma_SCI_9522

Implement low stock notification [SCI-9522]
This commit is contained in:
Martin Artnik 2023-10-19 11:41:57 +02:00 committed by GitHub
commit 51a2b3902b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 6 deletions

View file

@ -18,6 +18,8 @@ class RepositoryStockValue < ApplicationRecord
validates :low_stock_threshold, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true 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? 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 after_create do
next if is_a?(RepositoryStockConsumptionValue) next if is_a?(RepositoryStockConsumptionValue)
@ -199,4 +201,8 @@ class RepositoryStockValue < ApplicationRecord
.my_module_repository_rows .my_module_repository_rows
.update_all(repository_stock_unit_item_id: repository_stock_unit_item_id) .update_all(repository_stock_unit_item_id: repository_stock_unit_item_id)
end end
def send_low_stock_notification
LowStockNotification.send_notifications({ repository_row_id: repository_cell.repository_row_id })
end
end end

View file

@ -4,7 +4,8 @@ class BaseNotification < Noticed::Base
deliver_by :database, if: :database_notification? deliver_by :database, if: :database_notification?
def self.send_notifications(params, later: false) 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| recipients_class.new(params).recipients.each do |recipient|
if later if later
with(params).deliver_later(recipient) with(params).deliver_later(recipient)
@ -14,6 +15,10 @@ class BaseNotification < Noticed::Base
end end
end end
def self.subtype
params[:type]
end
def subject; end def subject; end
private private
@ -23,7 +28,8 @@ class BaseNotification < Noticed::Base
end end
def notification_subgroup def notification_subgroup
NotificationExtends::NOTIFICATIONS_GROUPS.values.reduce({}, :merge) NotificationExtends::NOTIFICATIONS_GROUPS.values.reduce({}, :merge).find do |_sg, n|
.find { |_sg, n| n.include?(params[:type].to_sym) }[0] n.include?(self.class.subtype.to_sym)
end[0]
end end
end end

View file

@ -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

View file

@ -6,6 +6,6 @@ class Recipients::ItemCreatorRecipients
end end
def recipients def recipients
[] [RepositoryRow.find(@params[:repository_row_id]).created_by]
end end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class NotificationExtends class NotificationExtends
NOTIFICATIONS_TYPES = { NOTIFICATIONS_TYPES = {
designate_user_to_my_module_activity: { designate_user_to_my_module_activity: {

View file

@ -3339,8 +3339,8 @@ en:
other_team_invitation: "You are invited or removed from the team" other_team_invitation: "You are invited or removed from the team"
notification: notification:
my_module_due_date_reminder_html: "Due date for %{my_module_name} is coming up" 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_low_stock_reminder_html: "Item %{repository_row_name} is running low"
item_date_reminder_html: "Date reminder for %{repository_item_name} is coming up in %{value} %{units}" item_date_reminder_html: "Date reminder for %{repository_row_name} is coming up in %{value} %{units}"
deliver: deliver:
download_link: "Download link:" download_link: "Download link:"
download_text: "Click the link to download the file." download_text: "Click the link to download the file."