From a5f711aa460dd0d01b25503b88115d3eefc41813 Mon Sep 17 00:00:00 2001 From: Ivan Kljun Date: Fri, 1 Dec 2023 15:00:30 +0100 Subject: [PATCH] Add job for RepositoryDateValue, fix translation [SCI-5961] --- app/jobs/repository_item_date_reminder_job.rb | 22 ++++++++----- .../repository_item_date_notification.rb | 31 +++++++++++++++++-- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/app/jobs/repository_item_date_reminder_job.rb b/app/jobs/repository_item_date_reminder_job.rb index 4b97ad140..a988a7ba0 100644 --- a/app/jobs/repository_item_date_reminder_job.rb +++ b/app/jobs/repository_item_date_reminder_job.rb @@ -4,15 +4,23 @@ class RepositoryItemDateReminderJob < ApplicationJob queue_as :default def perform - RepositoryDateTimeValue + process_repository_values(RepositoryDateTimeValue, DateTime.current) + process_repository_values(RepositoryDateValue, Date.current) + end + + private + + def process_repository_values(model, comparison_value) + model .where(notification_sent: false) - .where('data <= ?', DateTime.current) + .where('data <= ?', comparison_value) .joins(repository_cell: { repository_column: :repository }) .where(repositories: { type: 'Repository' }) - .find_each do |date_time_value| - RepositoryItemDateNotification - .send_notifications({ date_time_value_id: date_time_value.id, - repository_row_id: date_time_value.repository_cell.repository_row_id }) - end + .find_each do |value| + 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 }) + end end end diff --git a/app/notifications/repository_item_date_notification.rb b/app/notifications/repository_item_date_notification.rb index 5d5003806..6548ee5f3 100644 --- a/app/notifications/repository_item_date_notification.rb +++ b/app/notifications/repository_item_date_notification.rb @@ -2,9 +2,12 @@ class RepositoryItemDateNotification < BaseNotification def message + unit = human_readable_unit(column.metadata['reminder_unit'], column.metadata['reminder_value']) I18n.t( 'notifications.notification.item_date_reminder_html', - repository_row_name: subject.name + repository_row_name: subject.name, + value: column.metadata['reminder_value'], + units: unit ) end @@ -18,7 +21,31 @@ class RepositoryItemDateNotification < BaseNotification RepositoryRow.find(params[:repository_row_id]) end + def column + RepositoryColumn.find(params[:repository_column_id]) + end + after_deliver do - RepositoryDateTimeValue.find(params[:date_time_value_id]).update(notification_sent: true) + if params[:repository_date_time_value_id] + RepositoryDateTimeValue.find(params[:repository_date_time_value_id]).update(notification_sent: true) + elsif params[:repository_date_value_id] + RepositoryDateVal7ue.find(params[:repository_date_value_id]).update(notification_sent: true) + end + end + + private + + def human_readable_unit(seconds, value) + units_hash = { + '2419200' => 'month', + '604800' => 'week', + '86400' => 'day' + } + + base_unit = units_hash.fetch(seconds) do + raise ArgumentError, "Unrecognized time unit for seconds value: #{seconds}" + end + + value.to_i > 1 ? base_unit.pluralize : base_unit end end