Last active
May 26, 2016 17:31
-
-
Save jvanderhoof/f1e825d8e4f29479a02a280b86efe573 to your computer and use it in GitHub Desktop.
Potential Email Schedular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class StudyVisitMailer < BaseMailer | |
| def send_email(user_id, email_id) | |
| user = User.find(user_id) | |
| email = Email.find(email_id) | |
| ... | |
| mail( | |
| :to => user.email, | |
| :subject => email.subject(:study_visit_number_text => sv_number), | |
| :template_name => email.template | |
| ) | |
| end | |
| end | |
| class Emails::AdvanceParkinsonsResearchEmail < Email | |
| def send_email(user_id) | |
| StudyVisitMailer.delay.send_email(user_id, id) | |
| end | |
| def template | |
| 'advance_parkinsons_research' | |
| end | |
| def ganalytics_options | |
| { | |
| :utm_campaign => 'fi_feature_notifications', | |
| :utm_source => 'advance_parkinsons_research_email', | |
| :utm_medium => 'email' | |
| } | |
| end | |
| end | |
| class StudyVisitEmailSchedulerService | |
| def queue_emails(profile) | |
| queue(profile, email_list(profile)) | |
| end | |
| def dequeue_emails(profile) | |
| dequeue(profile, email_list(profile)) | |
| end | |
| def default_emails | |
| with_default([ | |
| {:klass => Emails::StudyVisitReminderEightySevenDayEmail, :days_in => 87} | |
| ]) | |
| end | |
| def new_participant_baseline_emails | |
| with_default([ | |
| {:klass => Emails::AdvanceParkinsonsResearchEmail, :days_in => 7}, | |
| {:klass => , :days_in => }, | |
| {:klass => , :days_in => }, | |
| {:klass => , :days_in => }, | |
| {:klass => , :days_in => }, | |
| {:klass => , :days_in => }, | |
| {:klass => , :days_in => }, | |
| {:klass => , :days_in => }, | |
| {:klass => , :days_in => }, | |
| ]) | |
| end | |
| def new_participant_second_study_visit_emails | |
| with_default([ | |
| {:klass => , :days_in => }, | |
| ]) | |
| end | |
| def existing_participant_baseline_emails | |
| [ | |
| {:klass => , :days_in => }, | |
| ] | |
| end | |
| def queue(profile, emails) | |
| [*emails].each do |email| | |
| next if profile.user.emails.detect {|e| e.is_a?(email_klass) } | |
| profile.user.user_emails.create( | |
| :email => email[:klass].first, | |
| :send_at => email[:days_in].days.from_now.utc | |
| ) | |
| end | |
| end | |
| def dequeue(profile, emails) | |
| [*emails].each do |email| | |
| profile.user.user_emails.where(:email => email).destoy_all | |
| end | |
| end | |
| def email_list(profile) | |
| if new_participant?(profile) | |
| case study_visit_count(profile) | |
| when 1 | |
| new_participant_baseline_emails | |
| when 2 | |
| new_participant_second_study_visit_emails | |
| end | |
| else | |
| case study_visit_count(profile) | |
| when 1 | |
| existing_participant_baseline_emails | |
| end | |
| end | |
| end | |
| def new_participant?(profile) | |
| profile.study_visits.all? {|sv| sv.linear_study_visit? } | |
| end | |
| def study_visit_count(profile) | |
| profile.study_visits.map.count {|sv| sv.linear_study_visit? } | |
| end | |
| def with_default(arr) | |
| arr.concat(default_emails).uniq | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment