diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 12f3487..e8fff6c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2022-06-13 04:06:48 UTC using RuboCop version 1.30.1. +# on 2023-11-15 14:19:32 UTC using RuboCop version 1.30.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -55,9 +55,9 @@ Naming/MethodParameterName: - 'slack-market/app.rb' - 'slack-market/models/team.rb' -# Offense count: 30 +# Offense count: 32 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns. # URISchemes: http, https Layout/LineLength: - Max: 244 + Max: 246 diff --git a/slack-market/app.rb b/slack-market/app.rb index e81486b..086734c 100644 --- a/slack-market/app.rb +++ b/slack-market/app.rb @@ -37,17 +37,23 @@ def check_subscribed_teams! Team.where(subscribed: true, :stripe_customer_id.ne => nil).each do |team| begin customer = Stripe::Customer.retrieve(team.stripe_customer_id) - customer.subscriptions.each do |subscription| - subscription_name = "#{subscription.plan.name} (#{ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100)})" - logger.info "Checking #{team} subscription to #{subscription_name}, #{subscription.status}." - case subscription.status - when 'past_due' - logger.warn "Subscription for #{team} is #{subscription.status}, notifying." - team.inform! "Your subscription to #{subscription_name} is past due. #{team.update_cc_text}" - when 'canceled', 'unpaid' - logger.warn "Subscription for #{team} is #{subscription.status}, downgrading." - team.inform! "Your subscription to #{subscription.plan.name} (#{ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100)}) was canceled and your team has been downgraded. Thank you for being a customer!" - team.update_attributes!(subscribed: false) + if customer.subscriptions.none? + logger.info "No active subscriptions for #{team} (#{team.stripe_customer_id}), downgrading." + team.inform! 'Your subscription was canceled and your team has been downgraded. Thank you for being a customer!' + team.update_attributes!(subscribed: false) + else + customer.subscriptions.each do |subscription| + subscription_name = "#{subscription.plan.name} (#{ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100)})" + logger.info "Checking #{team} subscription to #{subscription_name}, #{subscription.status}." + case subscription.status + when 'past_due' + logger.warn "Subscription for #{team} is #{subscription.status}, notifying." + team.inform! "Your subscription to #{subscription_name} is past due. #{team.update_cc_text}" + when 'canceled', 'unpaid' + logger.warn "Subscription for #{team} is #{subscription.status}, downgrading." + team.inform! "Your subscription to #{subscription.plan.name} (#{ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100)}) was canceled and your team has been downgraded. Thank you for being a customer!" + team.update_attributes!(subscribed: false) + end end end rescue StandardError => e diff --git a/spec/slack-market/app_spec.rb b/spec/slack-market/app_spec.rb index c8d3d9f..a9751aa 100644 --- a/spec/slack-market/app_spec.rb +++ b/spec/slack-market/app_spec.rb @@ -57,6 +57,13 @@ subject.send(:check_subscribed_teams!) expect(team.reload.subscribed?).to be false end + it 'notifies no active subscriptions' do + customer.subscriptions.data = [] + expect(Stripe::Customer).to receive(:retrieve).and_return(customer) + expect_any_instance_of(Team).to receive(:inform!).with('Your subscription was canceled and your team has been downgraded. Thank you for being a customer!') + subject.send(:check_subscribed_teams!) + expect(team.reload.subscribed?).to be false + end end end end