-
-
Notifications
You must be signed in to change notification settings - Fork 198
Email users code #2449
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
base: master
Are you sure you want to change the base?
Email users code #2449
Changes from all commits
b973353
8f9e9b7
b896d10
7ac0062
67cc391
825db4b
a049c41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class SendThreeMonthEmailJob < ApplicationJob | ||
| queue_as :default | ||
|
|
||
| def perform | ||
| ThreeMonthEmailService.send_chaser | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| module EmailDelivery | ||
| extend ActiveSupport::Concern | ||
|
|
||
| private | ||
|
|
||
| def log_sent_email | ||
| member = params[:member] | ||
| return unless member | ||
|
|
||
| MemberEmailDelivery.create!( | ||
| member: member, | ||
| subject: mail.subject, | ||
| body: mail.body.to_s, | ||
| to: mail.to, | ||
| cc: mail.cc, | ||
| bcc: mail.bcc | ||
| ) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,16 @@ | ||||||
| class MemberMailer < ApplicationMailer | ||||||
| include EmailHeaderHelper | ||||||
| include EmailDelivery | ||||||
|
|
||||||
| after_action :log_sent_email, only: [:chaser] | ||||||
|
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. We might want to log only once the email is sent
Suggested change
|
||||||
|
|
||||||
| def chaser | ||||||
| @member = params[:member] | ||||||
| subject = "It’s been a while, how are you doing? ♥️" | ||||||
| mail mail_args(@member, subject, 'hello@codebar.io', 'hello@codebar.io') do |format| | ||||||
| format.html {render 'three_month_chaser'} | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| def welcome(member) | ||||||
| if member.student? | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| class MemberEmailDelivery < ApplicationRecord | ||
| belongs_to :member, polymorphic: true, optional: true | ||
| end |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| # frozen_string_literal: true | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| class ThreeMonthEmailService | ||||||||||||||||||||||||||||||||||||||||||||||
| def self.send_chaser | ||||||||||||||||||||||||||||||||||||||||||||||
| members = Member.joins(:workshop_invitations) | ||||||||||||||||||||||||||||||||||||||||||||||
| .left_joins(:member_email_deliveries) | ||||||||||||||||||||||||||||||||||||||||||||||
| .where(workshop_invitations: { attended: false }) | ||||||||||||||||||||||||||||||||||||||||||||||
| .where("workshop_invitations.created_at >= ?", 3.months.ago.beginning_of_day) | ||||||||||||||||||||||||||||||||||||||||||||||
| .where(member_email_deliveries: { id: nil }) | ||||||||||||||||||||||||||||||||||||||||||||||
| .distinct | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+5
to
+10
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. There might be a bit more to the query as it has to exclude certain members
Also the logic here only checks created_at>=3 months and attended:false which would still include members who did attend in the last three months but also have a non-attended workshop in that period
Suggested change
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. When I tested (with fake data), the |
||||||||||||||||||||||||||||||||||||||||||||||
| return if members.empty? | ||||||||||||||||||||||||||||||||||||||||||||||
| members.each do |member| | ||||||||||||||||||||||||||||||||||||||||||||||
|
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. Use find_each to lower the amount of records read into memory in one go
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| MemberMailer.with(member: member).chaser.deliver_later | ||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| %h1 Hi #{@member.name}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| %p | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| We’ve noticed you haven’t been to a codebar workshop in a little while, and we just wanted to check in. We know life gets busy, but we’d love to understand how things are going for you and whether there’s anything we can do to make it easier or more valuable for you to join again. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| %p | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| If you have a minute, could you please share your thoughts in this short form? 👉 https://forms.gle/tEETvC3zYP9mcLar7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| %p | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Or, if you’re thinking about coming back soon, we’ve got some great upcoming workshops and events you might like to join 👉https://codebar.io/events/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| %p | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Your feedback really helps us make codebar more welcoming and useful for everyone in our community 💜 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| %p | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| We’d love to see you again soon! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| %p | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #{"-- "} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| %br | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Warmly, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Codebar Team | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+21
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. Looks as if the multiline text under each As an aside, would be better to move everything to
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace :chaser do | ||
| desc "Send emails to users who've not attended in a while" | ||
|
|
||
| task three_months: :environment do | ||
| SendThreeMonthEmailJob.perform_later | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Fabricator(:member_email_delivery) do | ||
| member(fabricator: :member) | ||
| subject("Chaser") | ||
| body("Lorem ipsum") | ||
| to(["test_email@address"]) | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,4 +115,21 @@ | |
| end.to change { ActionMailer::Base.deliveries.count }.by 1 | ||
| end | ||
| end | ||
|
|
||
| describe "#chaser" do | ||
|
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. Nice spec. |
||
| it "logs the sent email" do | ||
| expect do | ||
| MemberMailer | ||
| .with(member: member) | ||
| .chaser | ||
| .deliver_now | ||
| end.to change(MemberEmailDelivery, :count).by(1) | ||
|
|
||
| log = MemberEmailDelivery.last! | ||
|
|
||
| expect(log.member).to eq(member) | ||
| expect(log.subject).to eq("It’s been a while, how are you doing? ♥️") | ||
| expect(log.to).to eq([member.email]) | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| RSpec.describe ThreeMonthEmailService, type: :service do | ||
|
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. Maybe we could add a few scenarios around different types of attendance - like people who had not attended one workshop but then attended the next one in a previous 3 months. Also check we don't send out for banned, or unsubscribed members. |
||
| describe "#send_chaser" do | ||
| subject(:call) { described_class.send_chaser } | ||
|
|
||
| let!(:eligible_member) { Fabricate(:member) } | ||
| let!(:emailed_member) { Fabricate(:member) } | ||
| let!(:old_invite_member) { Fabricate(:member) } | ||
|
|
||
| before do | ||
| # Eligible: recent invite, no email delivery | ||
| Fabricate( | ||
| :workshop_invitation, | ||
| member: eligible_member, | ||
| created_at: 3.months.ago, | ||
| attended: false | ||
| ) | ||
|
|
||
| # Already emailed: recent invite, but has email delivery | ||
| Fabricate( | ||
| :workshop_invitation, | ||
| member: emailed_member, | ||
| created_at: 2.months.ago | ||
| ) | ||
| Fabricate( | ||
| :member_email_delivery, | ||
| member: emailed_member | ||
| ) | ||
|
|
||
| # Old invite: more than 3 months ago | ||
| Fabricate( | ||
| :workshop_invitation, | ||
| member: old_invite_member, | ||
| created_at: 4.months.ago | ||
| ) | ||
| end | ||
|
|
||
| it "enqueues chaser emails only for eligible members" do | ||
| expect { | ||
| call | ||
| }.to have_enqueued_mail(MemberMailer, :chaser).once | ||
| end | ||
| end | ||
| end | ||
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.
Good idea to put it in a shared concern for future.