Add job for RepositoryDateValue, fix translation [SCI-5961]

This commit is contained in:
Ivan Kljun 2023-12-01 15:00:30 +01:00
parent 4481541a9f
commit a5f711aa46
2 changed files with 44 additions and 9 deletions

View file

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

View file

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