From 41c5ecf1d8e6b115bf021d72b55c73ef63327db3 Mon Sep 17 00:00:00 2001 From: Ivan Kljun Date: Mon, 11 Dec 2023 11:28:24 +0100 Subject: [PATCH] Make Date/Date&Time and Low Stock notifications work when deleting Repository/row record [SCI-9825] --- app/jobs/repository_item_date_reminder_job.rb | 15 ++++++++++++--- app/models/non_existant_record.rb | 9 +++++++++ app/models/repository_stock_value.rb | 10 +++++++++- app/notifications/low_stock_notification.rb | 6 +++++- .../repository_item_date_notification.rb | 8 ++++++-- app/serializers/notification_serializer.rb | 2 ++ 6 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 app/models/non_existant_record.rb diff --git a/app/jobs/repository_item_date_reminder_job.rb b/app/jobs/repository_item_date_reminder_job.rb index cb0a72875..eadd8cfdc 100644 --- a/app/jobs/repository_item_date_reminder_job.rb +++ b/app/jobs/repository_item_date_reminder_job.rb @@ -21,10 +21,19 @@ class RepositoryItemDateReminderJob < ApplicationJob "(repository_columns.metadata->>'reminder_value')::int) || ' seconds' AS Interval))", comparison_value ).find_each do |value| + repository_row = RepositoryRow.find(value.repository_cell.repository_row_id) + repository_column = RepositoryColumn.find(value.repository_cell.repository_column_id) + RepositoryItemDateNotification - .send_notifications({ "#{value.class.name.underscore}_id": value.id, - repository_row_id: value.repository_cell.repository_row_id, - repository_column_id: value.repository_cell.repository_column_id }) + .send_notifications({ + "#{value.class.name.underscore}_id": value.id, + repository_row_id: repository_row.id, + repository_row_name: repository_row.name, + repository_column_id: repository_column.id, + repository_column_name: repository_column.name, + reminder_unit: repository_column.metadata['reminder_unit'], + reminder_value: repository_column.metadata['reminder_value'] + }) end end end diff --git a/app/models/non_existant_record.rb b/app/models/non_existant_record.rb new file mode 100644 index 000000000..c5e13f1d9 --- /dev/null +++ b/app/models/non_existant_record.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class NonExistantRecord + attr_reader :name + + def initialize(name) + @name = name + end +end diff --git a/app/models/repository_stock_value.rb b/app/models/repository_stock_value.rb index 3d634d52a..4c043d1a2 100644 --- a/app/models/repository_stock_value.rb +++ b/app/models/repository_stock_value.rb @@ -203,6 +203,14 @@ class RepositoryStockValue < ApplicationRecord end def send_low_stock_notification - LowStockNotification.send_notifications({ repository_row_id: repository_cell.repository_row_id }) + repository_row = RepositoryRow.find(repository_cell.repository_row_id) + repository = Repository.find(repository_row.repository_id) + + LowStockNotification.send_notifications({ + repository_row_id: repository_cell.repository_row_id, + repository_row_name: repository_row.name, + repository_id: repository_row.repository_id, + repository_name: repository.name + }) end end diff --git a/app/notifications/low_stock_notification.rb b/app/notifications/low_stock_notification.rb index 31956e65a..8220f7141 100644 --- a/app/notifications/low_stock_notification.rb +++ b/app/notifications/low_stock_notification.rb @@ -19,9 +19,13 @@ class LowStockNotification < BaseNotification def subject RepositoryRow.find(params[:repository_row_id]) + rescue ActiveRecord::RecordNotFound + NonExistantRecord.new(params[:repository_row_name]) end def repository - Repository.find(subject.repository_id) + Repository.find(params[:repository_id]) + rescue ActiveRecord::RecordNotFound + NonExistantRecord.new(params[:repository_name]) end end diff --git a/app/notifications/repository_item_date_notification.rb b/app/notifications/repository_item_date_notification.rb index b800835f8..99c545fa2 100644 --- a/app/notifications/repository_item_date_notification.rb +++ b/app/notifications/repository_item_date_notification.rb @@ -2,11 +2,11 @@ class RepositoryItemDateNotification < BaseNotification def message - unit = human_readable_unit(column.metadata['reminder_unit'], column.metadata['reminder_value']) + unit = human_readable_unit(params[:reminder_unit], params[:reminder_value]) I18n.t( 'notifications.content.item_date_reminder.message_html', repository_row_name: subject.name, - value: column.metadata['reminder_value'], + value: params[:reminder_value], units: unit ) end @@ -21,10 +21,14 @@ class RepositoryItemDateNotification < BaseNotification def subject RepositoryRow.find(params[:repository_row_id]) + rescue ActiveRecord::RecordNotFound + NonExistantRecord.new(params[:repository_row_name]) end def column RepositoryColumn.find(params[:repository_column_id]) + rescue ActiveRecord::RecordNotFound + NonExistantRecord.new(params[:repository_column_name]) end after_deliver do diff --git a/app/serializers/notification_serializer.rb b/app/serializers/notification_serializer.rb index 068ab0f2a..d24965691 100644 --- a/app/serializers/notification_serializer.rb +++ b/app/serializers/notification_serializer.rb @@ -33,6 +33,8 @@ class NotificationSerializer < ActiveModel::Serializer private def generate_breadcrumbs(subject, breadcrumbs) + return [] if subject.is_a?(NonExistantRecord) + case subject when Project parent = subject.team