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
|
|
|
|
|
|
|
|
starting = Time.now.beginning_of_hour + delay
|
|
|
|
ending = starting + 1.hour
|
|
|
|
|
2016-08-23 10:10:58 +02:00
|
|
|
Reservation.joins(:slots).where('slots.start_at >= ? AND slots.start_at <= ?', 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,
|
2016-11-23 16:30:19 +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
|