-
-
Notifications
You must be signed in to change notification settings - Fork 205
feat: signup nudge email sequence for members without a chapter #2835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class SendSignupNudgeEmailJob < ApplicationJob | ||
| queue_as :default | ||
|
|
||
| def perform | ||
| SignupNudgeEmailService.send_nudges | ||
| end | ||
| end |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| class SignupNudgeEmailService | ||
| NUDGE = 'signup_nudge'.freeze | ||
| FOLLOWUP = 'signup_nudge_followup'.freeze | ||
|
|
||
| def self.send_nudges | ||
| send_signup_nudge | ||
| send_signup_nudge_followup | ||
| end | ||
|
|
||
| def self.send_signup_nudge | ||
| Member.not_banned | ||
| .where(created_at: nudge_window) | ||
| .merge(unemailed(NUDGE)) | ||
| .merge(never_subscribed) | ||
| .find_each { |member| MemberMailer.with(member:).signup_nudge.deliver_later } | ||
| end | ||
|
|
||
| def self.send_signup_nudge_followup | ||
| Member.not_banned | ||
| .joins(:member_email_deliveries) | ||
| .where(member_email_deliveries: { email_type: NUDGE, created_at: ..1.month.ago }) | ||
| .merge(unemailed(FOLLOWUP)) | ||
| .merge(never_subscribed) | ||
| .distinct | ||
| .find_each { |member| MemberMailer.with(member:).signup_nudge_followup.deliver_later } | ||
| end | ||
|
|
||
| # NULL-safe NOT IN: a NULL member_id in the subquery would exclude every member | ||
| def self.unemailed(email_type) | ||
| Member.where.not(id: MemberEmailDelivery.where(email_type:).where.not(member_id: nil).select(:member_id)) | ||
| end | ||
|
|
||
| def self.never_subscribed | ||
| Member.where.not(id: Subscription.where.not(member_id: nil).select(:member_id)) | ||
| end | ||
|
|
||
| def self.nudge_window | ||
| 30.days.ago.beginning_of_day..7.days.ago.end_of_day | ||
| end | ||
|
|
||
| private_class_method :send_signup_nudge, :send_signup_nudge_followup, :unemailed, :never_subscribed, | ||
| :nudge_window | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| %h1 Hi #{@member.name}, | ||
|
|
||
| %p | ||
| Thanks for signing up to codebar, we can’t wait to see you! | ||
|
|
||
| %p | ||
| We noticed you haven’t joined a chapter yet, and we’d love to help you get started. | ||
|
|
||
| %p | ||
| Codebar chapters are where the magic happens, you can meet fellow members whether they’re students or coaches, and of course attend our workshops and events. | ||
|
|
||
| %p | ||
| 👉 #{link_to 'Find your local chapter and join today', subscriptions_url} | ||
|
|
||
| %p | ||
| If you’re not sure which chapter is right for you, or you’d like tips on getting started, we’re happy to help, just hit reply to this email and we’ll guide you. | ||
|
|
||
| %p | ||
| We can’t wait to see you at a workshop or event very soon! | ||
|
|
||
| %p | ||
| #{"-- "} | ||
| %br | ||
| Warmly, | ||
| The codebar Team |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| namespace :chaser do | ||
| desc "Send emails to users who've not attended in a while" | ||
|
|
||
| task three_months: :environment do | ||
| SendThreeMonthEmailJob.perform_later | ||
| end | ||
|
|
||
| desc 'Send emails to new members who have not subscribed to a chapter' | ||
| task signup_nudges: :environment do | ||
| SendSignupNudgeEmailJob.perform_later | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'rake chaser:signup_nudges', type: :task do | ||
| it 'preloads the Rails environment' do | ||
| expect(task.prerequisites).to include 'environment' | ||
| end | ||
|
|
||
| it 'enqueues the signup nudge email job' do | ||
| allow(SendSignupNudgeEmailJob).to receive(:perform_later) | ||
|
|
||
| task.invoke | ||
|
|
||
| expect(SendSignupNudgeEmailJob).to have_received(:perform_later) | ||
| end | ||
| end | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe SignupNudgeEmailService, type: :service do | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Require.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — added |
||
| describe '#send_nudges' do | ||
| subject(:call) { described_class.send_nudges } | ||
|
|
||
| around do |example| | ||
| original_adapter = ActiveJob::Base.queue_adapter | ||
| ActiveJob::Base.queue_adapter = :test | ||
| example.run | ||
| ensure | ||
| ActiveJob::Base.queue_adapter = original_adapter | ||
| end | ||
|
|
||
| let!(:nudge_eligible) { Fabricate(:member, created_at: 10.days.ago) } | ||
| let!(:followup_eligible) { Fabricate(:member, created_at: 6.weeks.ago) } | ||
| let!(:subscribed_in_window) { Fabricate(:member, created_at: 10.days.ago) } | ||
| let!(:banned_in_window) { Fabricate(:banned_member, created_at: 10.days.ago) } | ||
| let!(:too_young) { Fabricate(:member, created_at: 2.days.ago) } | ||
| let!(:too_old) { Fabricate(:member, created_at: 5.weeks.ago) } | ||
| let!(:recently_nudged) { Fabricate(:member, created_at: 10.days.ago) } | ||
| let!(:completed_sequence) { Fabricate(:member, created_at: 7.weeks.ago) } | ||
| let!(:subscribed_after_nudge) { Fabricate(:member, created_at: 6.weeks.ago) } | ||
|
|
||
| before do | ||
| Fabricate(:subscription, member: subscribed_in_window) | ||
| Fabricate(:member_email_delivery, member: followup_eligible, email_type: 'signup_nudge', | ||
| created_at: 5.weeks.ago) | ||
| Fabricate(:member_email_delivery, member: recently_nudged, email_type: 'signup_nudge') | ||
| Fabricate(:member_email_delivery, member: completed_sequence, email_type: 'signup_nudge', | ||
| created_at: 6.weeks.ago) | ||
| Fabricate(:member_email_delivery, member: completed_sequence, email_type: 'signup_nudge_followup', | ||
| created_at: 5.weeks.ago) | ||
| Fabricate(:member_email_delivery, member: subscribed_after_nudge, email_type: 'signup_nudge', | ||
| created_at: 5.weeks.ago) | ||
| Fabricate(:subscription, member: subscribed_after_nudge) | ||
| end | ||
|
|
||
| it 'nudges members created 7-14 days ago who have no subscription' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .to change { MemberEmailDelivery.where(member: nudge_eligible, email_type: 'signup_nudge').count } | ||
| .by(1) | ||
| end | ||
|
|
||
| it 'sends the follow-up to members nudged more than a month ago' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .to change { | ||
| MemberEmailDelivery.where(member: followup_eligible, email_type: 'signup_nudge_followup').count | ||
| } | ||
| .by(1) | ||
| end | ||
|
|
||
| it 'does not nudge subscribed members' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .not_to(change { MemberEmailDelivery.where(member: subscribed_in_window).count }) | ||
| end | ||
|
|
||
| it 'does not nudge banned members' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .not_to(change { MemberEmailDelivery.where(member: banned_in_window).count }) | ||
| end | ||
|
|
||
| it 'nudges members created 15-30 days ago who have no subscription' do | ||
| older_eligible = Fabricate(:member, created_at: 20.days.ago) | ||
| expect { perform_enqueued_jobs { call } } | ||
| .to change { MemberEmailDelivery.where(member: older_eligible, email_type: 'signup_nudge').count } | ||
| .by(1) | ||
| end | ||
|
|
||
| it 'does not nudge members younger than 7 days' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .not_to(change { MemberEmailDelivery.where(member: too_young).count }) | ||
| end | ||
|
|
||
| it 'does not nudge members older than 30 days without a nudge row' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .not_to(change { MemberEmailDelivery.where(member: too_old).count }) | ||
| end | ||
|
|
||
| it 'does not re-nudge a member already nudged' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .not_to(change { MemberEmailDelivery.where(member: recently_nudged, email_type: 'signup_nudge').count }) | ||
| end | ||
|
|
||
| it 'does not send a follow-up while the nudge is less than a month old' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .not_to(change { MemberEmailDelivery.where(member: recently_nudged, email_type: 'signup_nudge_followup').count }) | ||
| end | ||
|
|
||
| it 'sends nothing further to members who completed the sequence' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .not_to(change { MemberEmailDelivery.where(member: completed_sequence).count }) | ||
| end | ||
|
|
||
| it 'does not send a follow-up to a member who has since subscribed' do | ||
| expect { perform_enqueued_jobs { call } } | ||
| .not_to(change do | ||
| MemberEmailDelivery.where(member: subscribed_after_nudge, email_type: 'signup_nudge_followup').count | ||
| end) | ||
| end | ||
|
|
||
| # DB allows NULL member_id on both tables; one such row would make NOT IN exclude everyone | ||
| it 'still sends nudges when log or subscription rows have no member_id' do | ||
| Fabricate(:member_email_delivery, email_type: 'signup_nudge').update_column(:member_id, nil) | ||
| Fabricate(:subscription).update_column(:member_id, nil) | ||
|
|
||
| expect { perform_enqueued_jobs { call } } | ||
| .to change { MemberEmailDelivery.where(member: nudge_eligible, email_type: 'signup_nudge').count } | ||
| .by(1) | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require the rails_helper - at the top of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — added
require 'rails_helper'at the top.