From 6393804182cff6b5cf079fa76f23ee84b5b9a86a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Morais?= <385232+gnclmorais@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:01:33 +0200 Subject: [PATCH 1/2] Skip students and coaches without TOC accepted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After talking with Kim about this a few weeks ago, we realised it doesn’t make much sense to email members that have not accepted our terms about new events. We see that as not having come back to codebar in a while, thus making more sense to save our resources. --- app/models/invitation_manager.rb | 4 +++ spec/models/invitation_manager_spec.rb | 46 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/app/models/invitation_manager.rb b/app/models/invitation_manager.rb index f65031273..3e011e75a 100644 --- a/app/models/invitation_manager.rb +++ b/app/models/invitation_manager.rb @@ -29,6 +29,8 @@ def send_meeting_emails(meeting) def invite_students_to_event(event, chapter) chapter_students(chapter).each do |student| + next unless student.accepted_toc_at + invitation = Invitation.new(event: event, member: student, role: 'Student') EventInvitationMailer.invite_student(event, student, invitation).deliver_now if invitation.save end @@ -36,6 +38,8 @@ def invite_students_to_event(event, chapter) def invite_coaches_to_event(event, chapter) chapter_coaches(chapter).each do |coach| + next unless coach.accepted_toc_at + invitation = Invitation.new(event: event, member: coach, role: 'Coach') EventInvitationMailer.invite_coach(event, coach, invitation).deliver_now if invitation.save end diff --git a/spec/models/invitation_manager_spec.rb b/spec/models/invitation_manager_spec.rb index 10235f1f6..4f0553b90 100644 --- a/spec/models/invitation_manager_spec.rb +++ b/spec/models/invitation_manager_spec.rb @@ -66,6 +66,52 @@ manager.send_event_emails(event, chapter) end + + it 'emails only students that accepted toc' do + event = Fabricate(:event, chapters: [chapter], audience: 'Students') + + first_student, *other_students = students + first_student.update(accepted_toc_at: nil) + + expect(Invitation).to_not( + receive(:new). + with(event: event, member: first_student, role: 'Student'). + and_call_original + ) + + other_students.each do |other_student| + expect(Invitation).to( + receive(:new). + with(event: event, member: other_student, role: 'Student'). + and_call_original + ) + end + + manager.send_event_emails(event, chapter) + end + + it 'emails only coaches that accepted toc' do + event = Fabricate(:event, chapters: [chapter], audience: 'Coaches') + + first_coach, *other_coaches = coaches + first_coach.update(accepted_toc_at: nil) + + expect(Invitation).to_not( + receive(:new). + with(event: event, member: first_coach, role: 'Coach'). + and_call_original + ) + + other_coaches.each do |other_coach| + expect(Invitation).to( + receive(:new). + with(event: event, member: other_coach, role: 'Coach'). + and_call_original + ) + end + + manager.send_event_emails(event, chapter) + end end describe '#send_monthly_attendance_reminder_emails', wip: true do From 2eddf9dbf24140cf23540a171ae9bcd6807b5d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Morais?= <385232+gnclmorais@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:12:56 +0200 Subject: [PATCH 2/2] Move TOC check to scope --- app/models/invitation_manager.rb | 4 ---- app/models/member.rb | 5 ++++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/models/invitation_manager.rb b/app/models/invitation_manager.rb index 3e011e75a..f65031273 100644 --- a/app/models/invitation_manager.rb +++ b/app/models/invitation_manager.rb @@ -29,8 +29,6 @@ def send_meeting_emails(meeting) def invite_students_to_event(event, chapter) chapter_students(chapter).each do |student| - next unless student.accepted_toc_at - invitation = Invitation.new(event: event, member: student, role: 'Student') EventInvitationMailer.invite_student(event, student, invitation).deliver_now if invitation.save end @@ -38,8 +36,6 @@ def invite_students_to_event(event, chapter) def invite_coaches_to_event(event, chapter) chapter_coaches(chapter).each do |coach| - next unless coach.accepted_toc_at - invitation = Invitation.new(event: event, member: coach, role: 'Coach') EventInvitationMailer.invite_coach(event, coach, invitation).deliver_now if invitation.save end diff --git a/app/models/member.rb b/app/models/member.rb index d13eac0ec..15ce7c427 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -20,6 +20,7 @@ class Member < ApplicationRecord validates :email, uniqueness: true validates :about_you, length: { maximum: 255 } + scope :accepted_toc, -> { where.not(accepted_toc_at: nil) } scope :order_by_email, -> { order(:email) } scope :subscribers, -> { joins(:subscriptions).order('created_at desc').uniq } scope :not_banned, lambda { @@ -32,7 +33,9 @@ class Member < ApplicationRecord .where('meeting_invitations.meeting_id = ? and meeting_invitations.attending = ?', meeting.id, true) } - scope :in_group, ->(members) { not_banned.joins(:groups).where(groups: { id: members.select(:id) }) } + scope :in_group, lambda { |members| + not_banned.accepted_toc.joins(:groups).where(groups: { id: members.select(:id) }) + } scope :with_skill, ->(skill_name) { tagged_with(skill_name) }