2016-08-17 17:39:12 +02:00
|
|
|
class ReservationReminderWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
## In case the reminder is enabled but no delay were configured, we use this default value
|
|
|
|
DEFAULT_REMINDER_DELAY = 24.hours
|
|
|
|
|
|
|
|
def perform
|
|
|
|
enabled = Setting.find_by(name: 'reminder_enable').try(:value)
|
|
|
|
if enabled == 'true'
|
|
|
|
delay = Setting.find_by(name: 'reminder_delay').try(:value).try(:to_i).try(:hours) || DEFAULT_REMINDER_DELAY
|
|
|
|
|
2019-12-02 11:57:25 +01:00
|
|
|
starting = DateTime.current.beginning_of_hour + delay
|
2016-08-17 17:39:12 +02:00
|
|
|
ending = starting + 1.hour
|
|
|
|
|
2018-01-09 14:39:52 +01:00
|
|
|
Reservation.joins(:slots).where('slots.start_at >= ? AND slots.start_at <= ? AND slots.canceled_at IS NULL', starting, ending).each do |r|
|
2016-09-01 16:26:08 +02:00
|
|
|
already_sent = Notification.where(
|
|
|
|
attached_object_type: Reservation.name,
|
|
|
|
attached_object_id: r.id,
|
2017-01-05 15:15:31 +01:00
|
|
|
notification_type_id: NotificationType.find_by_name('notify_member_reservation_reminder')
|
2016-09-01 16:26:08 +02:00
|
|
|
).count
|
|
|
|
unless already_sent > 0
|
|
|
|
NotificationCenter.call type: 'notify_member_reservation_reminder',
|
|
|
|
receiver: r.user,
|
|
|
|
attached_object: r
|
|
|
|
end
|
2016-08-17 17:39:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|