-
-
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
6dd2e94
9d7d887
d4fa250
7785a57
e97bda7
3b6cdca
1996d15
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 |
|---|---|---|
| @@ -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,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class ThreeMonthEmailService | ||
| def self.send_chaser | ||
| recent_attendees = Member.joins(:workshop_invitations) | ||
| .merge( | ||
| WorkshopInvitation.attended.to_students | ||
| .joins(:workshop) | ||
| .where('workshops.date_and_time >= ?', 3.months.ago.beginning_of_day) | ||
| ) | ||
| .distinct | ||
|
|
||
| members = Member.not_banned | ||
| .accepted_toc | ||
| .joins(:groups) | ||
| .merge(Group.students) | ||
| .left_joins(:member_email_deliveries) | ||
| .where(member_email_deliveries: { id: nil }) | ||
| .where.not(id: recent_attendees.select(:id)) | ||
| .distinct | ||
| return if members.empty? | ||
| members.find_each do |member| | ||
| 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 | ||
|---|---|---|---|---|
|
|
@@ -33,6 +33,9 @@ class Application < Rails::Application | |||
|
|
||||
| config.active_record.belongs_to_required_by_default = true | ||||
|
|
||||
| # let's start using Active Job! | ||||
|
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.
Suggested change
I agree with the sentiment, but the reader of the code could be left wondering “is this advocacy for a choice, or is the choice made?" |
||||
| config.active_job.queue_adapter = :delayed_job | ||||
|
|
||||
| if ENV["RAILS_LOG_TO_STDOUT"].present? | ||||
| $stdout.sync = true | ||||
| config.rails_semantic_logger.add_file_appender = false | ||||
|
|
||||
| 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.