Commit 678af159 authored by Nicolas Dular's avatar Nicolas Dular Committed by Gabriel Mazetto

Only send in-product emails to free groups

parent 199ae04f
......@@ -107,6 +107,8 @@ class Group < Namespace
scope :with_users, -> { includes(:users) }
scope :with_onboarding_progress, -> { joins(:onboarding_progress) }
scope :by_id, ->(groups) { where(id: groups) }
scope :for_authorized_group_members, -> (user_ids) do
......
......@@ -66,7 +66,6 @@ module Namespaces
Experiment.add_group(:in_product_marketing_emails, variant: variant, group: group)
end
# rubocop: disable CodeReuse/ActiveRecord
def groups_for_track
onboarding_progress_scope = OnboardingProgress
.completed_actions_with_latest_in_range(completed_actions, range)
......@@ -75,9 +74,18 @@ module Namespaces
# Filtering out sub-groups is a temporary fix to prevent calling
# `.root_ancestor` on groups that are not root groups.
# See https://gitlab.com/groups/gitlab-org/-/epics/5594 for more information.
Group.where(parent_id: nil).joins(:onboarding_progress).merge(onboarding_progress_scope)
Group
.top_most
.with_onboarding_progress
.merge(onboarding_progress_scope)
.merge(subscription_scope)
end
def subscription_scope
{}
end
# rubocop: disable CodeReuse/ActiveRecord
def users_for_group(group)
group.users
.where(email_opted_in: true)
......@@ -136,3 +144,5 @@ module Namespaces
end
end
end
Namespaces::InProductMarketingEmailsService.prepend_ee_mod
# frozen_string_literal: true
module EE
module Namespaces
module InProductMarketingEmailsService
extend ::Gitlab::Utils::Override
private
override :subscription_scope
def subscription_scope
::Namespace.in_default_plan
end
end
end
end
---
title: Only send in-product emails to free groups
merge_request: 60906
author:
type: changed
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Namespaces::InProductMarketingEmailsService, '#execute' do
let(:frozen_time) { Time.zone.parse('23 Mar 2021 10:14:40 UTC') }
let_it_be(:user) { create(:user, email_opted_in: true) }
before do
travel_to(frozen_time)
create(:onboarding_progress, namespace: group, created_at: frozen_time - 2.days, git_write_at: nil)
group.add_developer(user)
stub_experiment_for_subject(in_product_marketing_emails: true)
allow(Ability).to receive(:allowed?).with(user, anything, anything).and_return(true)
allow(Notify).to receive(:in_product_marketing_email).and_return(double(deliver_later: nil))
end
context 'when group has a plan' do
before do
described_class.new(:create, 1).execute
end
context 'on the free plan' do
let(:group) { create(:group_with_plan, plan: :free_plan) }
it 'sends an email' do
expect(Notify).to have_received(:in_product_marketing_email)
end
end
context 'on a trial' do
let(:group) { create(:group_with_plan, trial_ends_on: frozen_time + 10.days) }
it 'sends an email' do
expect(Notify).to have_received(:in_product_marketing_email)
end
end
context 'on a paid plan' do
let(:group) { create(:group_with_plan, plan: :bronze_plan) }
it 'does not send email' do
expect(Notify).not_to have_received(:in_product_marketing_email)
end
end
end
end
......@@ -632,6 +632,16 @@ RSpec.describe Group do
it { is_expected.to match_array([private_group, internal_group]) }
end
describe 'with_onboarding_progress' do
subject { described_class.with_onboarding_progress }
it 'joins onboarding_progress' do
create(:onboarding_progress, namespace: group)
expect(subject).to eq([group])
end
end
describe 'for_authorized_group_members' do
let_it_be(:group_member1) { create(:group_member, source: private_group, user_id: user1.id, access_level: Gitlab::Access::OWNER) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment