Skip to content

Commit c4ddbed

Browse files
committed
It only sends the trial user exceeded email once
1 parent 7caf430 commit c4ddbed

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

app/models/organization.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,12 @@ def update_subscription
284284
end
285285

286286
def maybe_notify_trial_users_count_exceeded
287-
pp within_trial_period?
288-
return unless within_trial_period? && trial_users_exceeded? && in_app_billing
287+
return unless !trail_users_count_exceeded_email_sent? &&
288+
within_trial_period? &&
289+
trial_users_exceeded? &&
290+
in_app_billing
289291

290292
TrialUsersCountExceededMailer.notify(self)
293+
update_attributes(trail_users_count_exceeded_email_sent: true)
291294
end
292295
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddTrailUsersCountExceededEmailSentToOrganization < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :organizations, :trail_users_count_exceeded_email_sent, :boolean, default: false, null: false
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20181016214104) do
13+
ActiveRecord::Schema.define(version: 20181023184040) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -220,6 +220,7 @@
220220
t.datetime "trial_ends_at"
221221
t.integer "trial_users_count", default: 0, null: false
222222
t.boolean "in_app_billing", default: true, null: false
223+
t.boolean "trail_users_count_exceeded_email_sent", default: false, null: false
223224
t.index ["slug"], name: "index_organizations_on_slug", unique: true
224225
end
225226

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '2.3'
2+
services:
3+
elasticsearch:
4+
image: docker.elastic.co/elasticsearch/elasticsearch:6.4.0
5+
ports:
6+
- "9200:9200"

spec/models/organization_spec.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,23 @@
195195
allow(TrialUsersCountExceededMailer).to receive(:notify)
196196
end
197197

198+
context 'already sent' do
199+
it 'does nothing' do
200+
organization = create(:organization,
201+
in_app_billing: true,
202+
trial_users_count: 10,
203+
active_users_count: 10,
204+
trail_users_count_exceeded_email_sent: true)
205+
expect(TrialUsersCountExceededMailer).not_to receive(:notify)
206+
organization.update_attributes(
207+
active_users_count: 11,
208+
)
209+
end
210+
end
211+
198212
context 'not within trial period' do
199213
it 'does nothing' do
200-
organization = create(:organization, in_app_billing: true, trial_users_count: 10, active_users_count: 5, trial_ends_at: 1.day.ago)
201-
organization.in_app_billing = true
214+
organization = create(:organization, in_app_billing: true, trial_users_count: 10, active_users_count: 10, trial_ends_at: 1.day.ago)
202215
expect(TrialUsersCountExceededMailer).not_to receive(:notify)
203216
organization.update_attributes(
204217
active_users_count: 11,
@@ -229,11 +242,20 @@
229242

230243
context 'active users count greater than trial user count and in app billing enabled' do
231244
it 'uses the TrialUsersCountExceededMailer' do
232-
organization = create(:organization, in_app_billing: true, trial_users_count: 5, active_users_count: 5, trial_ends_at: 1.day.from_now)
233-
expect(TrialUsersCountExceededMailer).to receive(:notify)
245+
organization = create(:organization,
246+
in_app_billing: true,
247+
trial_users_count: 5,
248+
active_users_count: 5,
249+
trial_ends_at: 1.day.from_now,
250+
trail_users_count_exceeded_email_sent: false)
251+
expect(TrialUsersCountExceededMailer).to receive(:notify).once
234252
organization.update_attributes(
235253
active_users_count: 6,
236254
)
255+
organization.update_attributes(
256+
active_users_count: 7,
257+
)
258+
expect(organization.trail_users_count_exceeded_email_sent).to be true
237259
end
238260
end
239261
end

0 commit comments

Comments
 (0)