2023-11-07 00:25:48 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryItemDateNotification < BaseNotification
|
|
|
|
def self.subtype
|
|
|
|
:item_date_reminder
|
|
|
|
end
|
|
|
|
|
2023-12-11 15:52:28 +08:00
|
|
|
def title
|
2024-01-03 18:28:47 +08:00
|
|
|
unit = human_readable_unit(params[:reminder_unit], params[:reminder_value])
|
2023-12-11 20:18:08 +08:00
|
|
|
I18n.t(
|
|
|
|
'notifications.content.item_date_reminder.message_html',
|
|
|
|
repository_row_name: subject.name,
|
2024-01-03 18:28:47 +08:00
|
|
|
value: params[:reminder_value],
|
2023-12-11 20:18:08 +08:00
|
|
|
units: unit
|
|
|
|
)
|
2023-12-11 15:52:28 +08:00
|
|
|
end
|
2023-11-07 00:25:48 +08:00
|
|
|
|
|
|
|
def subject
|
|
|
|
RepositoryRow.find(params[:repository_row_id])
|
2023-12-11 18:28:24 +08:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
NonExistantRecord.new(params[:repository_row_name])
|
2023-11-07 00:25:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
after_deliver do
|
2023-12-01 22:00:30 +08:00
|
|
|
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]
|
2023-12-06 18:42:40 +08:00
|
|
|
RepositoryDateValue.find(params[:repository_date_value_id]).update(notification_sent: true)
|
2023-12-01 22:00:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def human_readable_unit(seconds, value)
|
2023-12-11 15:52:28 +08:00
|
|
|
return unless seconds
|
|
|
|
|
2023-12-01 22:00:30 +08:00
|
|
|
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
|
2023-11-07 00:25:48 +08:00
|
|
|
end
|
|
|
|
end
|