Skip to content

Instantly share code, notes, and snippets.

@amcrawford
Created August 31, 2016 21:03
Show Gist options
  • Save amcrawford/194e9e2dcdc94a41e62923e8379064f3 to your computer and use it in GitHub Desktop.
Save amcrawford/194e9e2dcdc94a41e62923e8379064f3 to your computer and use it in GitHub Desktop.
def schedule_next_action(truancy_status)
completed_truancy_actions = TruancyEvent.where(shipment_id: truancy_status.shipment_id)
TRUANCY_ACTION_SCHEDULE.each do |action, details|
action_occurred = completed_truancy_actions.detect { |event| event.event == action }
# unless action_occurred
# last_action_occurred_at = nil
# if details.last_truancy_action
# last_action_occurred_at = completed_truancy_actions.detect { |event| event.event == details.last_truancy_action }.occurred_at
# end
# # schedule
# scheduled_at = nil # ?
# truancy_status.update_attributes!(next_action: action,
# next_action_scheduled_at: scheduled_at)
# break
# end
next_event_has_occurred = completed_truancy_actions.detect { |event| event.event == action.next_truancy_action }
if !next_event_has_occurred
truancy_status.update_attributes!(next_action: action.next_truancy_action,
next_action_scheduled_at: scheduled_at)
break
end
# unless completed_truancy_actions.detect { |event| event.event == action.next_truancy_action }
# next_action = action
# case action
# when 'final_reminder'
# first_reminder_occurred_at = completed_truancy_actions.detect { |event| event.event == 'first_reminder' }
# next_action_scheduled_at = first_reminder_occurred_at + 3.days
# end
#
#
#
#
#
# end
end
# QUESTION: is it okay to base on occurred_at? Or should we base on known order of actions...
last_completed_action = TruancyEvent.where(shipment_id: truancy_status.shipment_id).order(:occurred_at).last # order correct here?
next_action = TRUANCY_ACTION_SCHEDULE[last_completed_action.event].next_action
# scheduled_at = last_completed_action.occurred_at +
truancy_status.update_attributes!(next_action: next_action,
next_action_scheduled_at: scheduled_at)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment