You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.7 KiB
90 lines
2.7 KiB
class RecipientSchedule < ApplicationRecord
|
|
|
|
belongs_to :recipient
|
|
belongs_to :schedule
|
|
has_many :attempts
|
|
|
|
validates_associated :recipient
|
|
validates_associated :schedule
|
|
validates :next_attempt_at, presence: true
|
|
|
|
scope :ready, -> { where('next_attempt_at <= ?', Time.new) }
|
|
scope :for, -> (recipient_or_recipient_id) {
|
|
id = recipient_or_recipient_id.is_a?(Recipient) ?
|
|
recipient_or_recipient_id.id :
|
|
recipient_or_recipient_id
|
|
where(recipient_id: id)
|
|
}
|
|
|
|
def next_question
|
|
if queued_question_ids.present?
|
|
next_question_id = queued_question_ids.split(/,/).first
|
|
else
|
|
next_question_id = upcoming_question_ids.split(/,/).first
|
|
end
|
|
Question.where(id: next_question_id).first
|
|
end
|
|
|
|
def attempt_question(send_message: true, question: next_question)
|
|
return if recipient.opted_out?
|
|
unanswered_attempt = recipient.attempts.with_no_response.last
|
|
|
|
return if question.nil? && unanswered_attempt.nil?
|
|
|
|
if unanswered_attempt.nil?
|
|
attempt = recipient.attempts.create(
|
|
schedule: schedule,
|
|
recipient_schedule: self,
|
|
question: question
|
|
)
|
|
end
|
|
|
|
if send_message && (unanswered_attempt || attempt).send_message
|
|
upcoming = upcoming_question_ids.try(:split, /,/) || []
|
|
queued = queued_question_ids.try(:split, /,/) || []
|
|
attempted = attempted_question_ids.try(:split, /,/) || []
|
|
|
|
if question.present?
|
|
question_id = [question.id.to_s]
|
|
upcoming = upcoming - question_id
|
|
if unanswered_attempt.nil?
|
|
attempted += question_id
|
|
queued -= question_id
|
|
else
|
|
queued += question_id
|
|
end
|
|
end
|
|
|
|
update_attributes(
|
|
upcoming_question_ids: upcoming.empty? ? nil : upcoming.join(','),
|
|
attempted_question_ids: attempted.empty? ? nil : attempted.join(','),
|
|
queued_question_ids: queued.empty? ? nil : queued.join(','),
|
|
last_attempt_at: (unanswered_attempt || attempt).sent_at,
|
|
next_attempt_at: next_attempt_at + (60 * 60 * schedule.frequency_hours)
|
|
)
|
|
end
|
|
return (unanswered_attempt || attempt)
|
|
end
|
|
|
|
def self.create_for_recipient(recipient_or_recipient_id, schedule, next_attempt_at=nil)
|
|
if next_attempt_at.nil?
|
|
next_attempt_at = Time.at(schedule.start_date.to_time.to_i + (60 * schedule.time))
|
|
end
|
|
|
|
question_ids = schedule.question_list.question_ids.split(/,/)
|
|
question_ids = question_ids.shuffle if schedule.random?
|
|
|
|
recipient_id = recipient_or_recipient_id.is_a?(Recipient) ?
|
|
recipient_or_recipient_id.id :
|
|
recipient_or_recipient_id
|
|
|
|
schedule.recipient_schedules.create(
|
|
recipient_id: recipient_id,
|
|
upcoming_question_ids: question_ids.join(','),
|
|
next_attempt_at: next_attempt_at
|
|
)
|
|
end
|
|
|
|
|
|
end
|