Skip to content
Open
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_deliver :log_sent_email, 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
35 changes: 35 additions & 0 deletions app/services/three_month_email_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

class ThreeMonthEmailService
def self.send_chaser
three_month_cutoff = 3.months.ago.beginning_of_day
one_year_cutoff = 1.year.ago.beginning_of_day

recent_attendee_ids = WorkshopInvitation.to_students
.attended
.joins(:workshop)
.where('workshops.date_and_time >= ?', three_month_cutoff)
.select(:member_id)

past_year_attendee_ids = WorkshopInvitation.to_students
.attended
.joins(:workshop)
.where('workshops.date_and_time >= ?', one_year_cutoff)
.select(:member_id)

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_attendee_ids)
.where(id: past_year_attendee_ids)
.distinct
return if members.empty?

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!
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# let's start using Active Job!

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
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
200 changes: 200 additions & 0 deletions spec/services/three_month_email_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
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(:chapter) { Fabricate(:chapter) }
let(:students_group) { Fabricate(:group, name: "Students", chapter: chapter) }
let(:coaches_group) { Fabricate(:group, name: "Coaches", chapter: chapter) }

let!(:eligible_student) do
member = Fabricate(:member)
Fabricate(:subscription, member: member, group: students_group)
Fabricate(
:workshop_invitation,
member: member,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 6.months.ago),
role: "Student",
attended: true
)
member
end

let!(:already_emailed_student) do
member = Fabricate(:member)
Fabricate(:subscription, member: member, group: students_group)
Fabricate(:member_email_delivery, member: member)
member
end

let!(:student_with_recent_attendance) do
member = Fabricate(:member)
Fabricate(:subscription, member: member, group: students_group)
Fabricate(
:workshop_invitation,
member: member,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 1.month.ago),
role: "Student",
attended: true
)
member
end

let!(:student_with_old_attendance) do
member = Fabricate(:member)
Fabricate(:subscription, member: member, group: students_group)
Fabricate(
:workshop_invitation,
member: member,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 4.months.ago),
role: "Student",
attended: true
)
member
end

let!(:coach_member) do
member = Fabricate(:member)
Fabricate(:subscription, member: member, group: coaches_group)
member
end

let!(:unsubscribed_member) { Fabricate(:member) }
let!(:banned_student) do
member = Fabricate(:banned_member)
Fabricate(:subscription, member: member, group: students_group)
member
end
let!(:student_without_toc) do
member = Fabricate(:member_without_toc)
Fabricate(:subscription, member: member, group: students_group)
member
end

let!(:student_with_very_old_attendance) do
member = Fabricate(:member)
Fabricate(:subscription, member: member, group: students_group)
Fabricate(
:workshop_invitation,
member: member,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 14.months.ago),
role: "Student",
attended: true
)
member
end

it "emails only students who have not attended in the last 3 months and were not emailed before" do
expect { perform_enqueued_jobs { call } }.to change(MemberEmailDelivery, :count).by(2)

expect(MemberEmailDelivery.where(member: eligible_student)).to exist
expect(MemberEmailDelivery.where(member: student_with_old_attendance)).to exist
end

it "does not email a member already present in member_email_deliveries" do
expect { perform_enqueued_jobs { call } }
.not_to change { MemberEmailDelivery.where(member: already_emailed_student).count }
end

it "does not email students with a recent attended workshop" do
expect { perform_enqueued_jobs { call } }
.not_to change { MemberEmailDelivery.where(member: student_with_recent_attendance).count }
end

it "does not email members without a student subscription" do
perform_enqueued_jobs { call }

expect(MemberEmailDelivery.where(member: coach_member)).to be_empty
expect(MemberEmailDelivery.where(member: unsubscribed_member)).to be_empty
end

it "does not email banned students or students without accepted terms" do
perform_enqueued_jobs { call }

expect(MemberEmailDelivery.where(member: banned_student)).to be_empty
expect(MemberEmailDelivery.where(member: student_without_toc)).to be_empty
end

it "does not email students who have not attended in the past year" do
perform_enqueued_jobs { call }

expect(MemberEmailDelivery.where(member: student_with_very_old_attendance)).to be_empty
end

it "sends only one chaser for a member with multiple student subscriptions" do
member = Fabricate(:member)
other_chapter = Fabricate(:chapter)
other_students_group = Fabricate(:group, name: "Students", chapter: other_chapter)
Fabricate(:subscription, member: member, group: students_group)
Fabricate(:subscription, member: member, group: other_students_group)

perform_enqueued_jobs { call }

expect(MemberEmailDelivery.where(member: member).count).to eq(1)
end

it "sends only one chaser for a member with multiple qualifying old attendances" do
member = Fabricate(:member)
Fabricate(:subscription, member: member, group: students_group)
Fabricate(
:workshop_invitation,
member: member,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 5.months.ago),
role: "Student",
attended: true
)
Fabricate(
:workshop_invitation,
member: member,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 4.months.ago),
role: "Student",
attended: true
)

perform_enqueued_jobs { call }

expect(MemberEmailDelivery.where(member: member).count).to eq(1)
end

it "does not send chasers when there are no eligible members" do
Fabricate(
:workshop_invitation,
member: eligible_student,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 1.month.ago),
role: "Student",
attended: true
)
Fabricate(
:workshop_invitation,
member: student_with_old_attendance,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 1.month.ago),
role: "Student",
attended: true
)

expect { perform_enqueued_jobs { call } }.not_to change(MemberEmailDelivery, :count)
end

it "emails a student member who has recent attendance only as a coach" do
member = Fabricate(:member)
Fabricate(:subscription, member: member, group: students_group)
Fabricate(
:workshop_invitation,
member: member,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 6.months.ago),
role: "Student",
attended: true
)
Fabricate(
:workshop_invitation,
member: member,
workshop: Fabricate(:workshop, chapter: chapter, date_and_time: 1.month.ago),
role: "Coach",
attended: true
)

perform_enqueued_jobs { call }

expect(MemberEmailDelivery.where(member: member)).to exist
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