Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/jobs/send_three_month_email_job.rb
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
19 changes: 19 additions & 0 deletions app/mailers/concerns/email_delivery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module EmailDelivery
extend ActiveSupport::Concern

private

def log_sent_email

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.

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
11 changes: 11 additions & 0 deletions app/mailers/member_mailer.rb
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]

Choose a reason for hiding this comment

The 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
after_action :log_sent_email, only: [:chaser]
after_deliver :log_sent_mail , only: [:chaser]


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?
Expand Down
1 change: 1 addition & 0 deletions app/models/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Member < ApplicationRecord
has_many :chapters, -> { distinct }, through: :groups
has_many :announcements, -> { distinct }, through: :groups
has_many :meeting_invitations
has_many :member_email_deliveries

validates :auth_services, presence: true
validates :name, :surname, :email, :about_you, presence: true, if: :can_log_in?
Expand Down
3 changes: 3 additions & 0 deletions app/models/member_email_delivery.rb
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
16 changes: 16 additions & 0 deletions app/services/three_month_email_service.rb
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

Choose a reason for hiding this comment

The 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

  • unsubscribed members
  • possibly coaches(?) One to check with Kimberley?
  • banned members
  • not accepted terms and conditions

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
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
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tested (with fake data), the attended column was nil rather than false which also failed the query. I'm not sure if that's the same in our live db

return if members.empty?
members.each do |member|

Choose a reason for hiding this comment

The 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
members.each do |member|
members.find_each do |member|

MemberMailer.with(member: member).chaser.deliver_later
end
end
end
21 changes: 21 additions & 0 deletions app/views/member_mailer/three_month_chaser.html.haml
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks as if the multiline text under each %p section needs indenting for it to display. At least this is how .slim files work and I'm assuming .haml works the same.

As an aside, would be better to move everything to .html.erb eventually? Much better format, although it's beyond this issue, might be worth discussing in future?

Suggested change
%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
%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

3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Application < Rails::Application

config.active_record.belongs_to_required_by_default = true

# let's start using Active Job!
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
Expand Down
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "active_support/core_ext/integer/time"
require "timecop"
require "active_job/test_helper"

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
Expand Down Expand Up @@ -55,6 +56,8 @@
# Raise error when a before_action's only/except options reference missing actions.
config.action_controller.raise_on_missing_callback_actions = true

config.active_job.queue_adapter = :test

# Fake omniauth for testing
OmniAuth.config.test_mode = true

Expand Down
7 changes: 7 additions & 0 deletions lib/tasks/chaser.rake
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
6 changes: 6 additions & 0 deletions spec/fabricators/member_email_delivery_fabricator.rb
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
17 changes: 17 additions & 0 deletions spec/mailers/member_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,21 @@
end.to change { ActionMailer::Base.deliveries.count }.by 1
end
end

describe "#chaser" do

Choose a reason for hiding this comment

The 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
43 changes: 43 additions & 0 deletions spec/services/three_month_email_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
RSpec.describe ThreeMonthEmailService, type: :service do

Choose a reason for hiding this comment

The 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
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def self.branch_coverage?
ActiveRecord::Migration.check_all_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
config.include ActiveJob::TestHelper
config.include ApplicationHelper
config.include LoginHelpers
config.include ActiveSupport::Testing::TimeHelpers
Expand Down Expand Up @@ -95,6 +96,9 @@ def self.branch_coverage?
to_return(status: 200, body: '{"status":"active","segments":[]}', headers: { 'Content-Type' => 'application/json' })

DatabaseCleaner.strategy = :transaction

clear_enqueued_jobs
clear_performed_jobs
end

# Driver is using an external browser with an app
Expand Down
Loading