Commit 79a30c82 authored by Vijay Hawoldar's avatar Vijay Hawoldar

Change future renewal service cache key

To ensure the cache is expired when the
subscription is updated
parent 671982d6
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
# returns true, false # returns true, false
module GitlabSubscriptions module GitlabSubscriptions
class CheckFutureRenewalService class CheckFutureRenewalService
def initialize(namespace_id:) def initialize(namespace:)
@namespace_id = namespace_id @namespace = namespace
end end
def execute def execute
...@@ -21,14 +21,14 @@ module GitlabSubscriptions ...@@ -21,14 +21,14 @@ module GitlabSubscriptions
private private
attr_reader :namespace_id attr_reader :namespace
def client def client
Gitlab::SubscriptionPortal::Client Gitlab::SubscriptionPortal::Client
end end
def last_term_request def last_term_request
response = client.subscription_last_term(namespace_id) response = client.subscription_last_term(namespace.id)
if response[:success] if response[:success]
response[:last_term] == false response[:last_term] == false
...@@ -42,7 +42,7 @@ module GitlabSubscriptions ...@@ -42,7 +42,7 @@ module GitlabSubscriptions
end end
def cache_key def cache_key
"subscription:future_renewal:namespace:#{namespace_id}" "subscription:future_renewal:namespace:#{namespace.gitlab_subscription.cache_key}"
end end
def future_renewal def future_renewal
......
...@@ -123,6 +123,7 @@ module EE ...@@ -123,6 +123,7 @@ module EE
subscription_params = declared_params(include_missing: false) subscription_params = declared_params(include_missing: false)
subscription_params[:trial_starts_on] ||= subscription_params[:start_date] if subscription_params[:trial] subscription_params[:trial_starts_on] ||= subscription_params[:start_date] if subscription_params[:trial]
subscription_params[:updated_at] = Time.current
if subscription.update(subscription_params) if subscription.update(subscription_params)
present subscription, with: ::EE::API::Entities::GitlabSubscription present subscription, with: ::EE::API::Entities::GitlabSubscription
......
...@@ -122,7 +122,7 @@ module Gitlab ...@@ -122,7 +122,7 @@ module Gitlab
def subscription_future_renewal? def subscription_future_renewal?
return if self_managed? || namespace.nil? return if self_managed? || namespace.nil?
::GitlabSubscriptions::CheckFutureRenewalService.new(namespace_id: namespace.id).execute ::GitlabSubscriptions::CheckFutureRenewalService.new(namespace: namespace).execute
end end
def require_notification? def require_notification?
......
...@@ -115,7 +115,7 @@ RSpec.describe Gitlab::ExpiringSubscriptionMessage do ...@@ -115,7 +115,7 @@ RSpec.describe Gitlab::ExpiringSubscriptionMessage do
let_it_be(:namespace) { create(:group, name: 'No Limit Records') } let_it_be(:namespace) { create(:group, name: 'No Limit Records') }
before do before do
allow_next_instance_of(GitlabSubscriptions::CheckFutureRenewalService, namespace_id: namespace.id) do |service| allow_next_instance_of(GitlabSubscriptions::CheckFutureRenewalService, namespace: namespace) do |service|
allow(service).to receive(:execute).and_return(has_future_renewal) allow(service).to receive(:execute).and_return(has_future_renewal)
end end
end end
...@@ -203,7 +203,7 @@ RSpec.describe Gitlab::ExpiringSubscriptionMessage do ...@@ -203,7 +203,7 @@ RSpec.describe Gitlab::ExpiringSubscriptionMessage do
let_it_be(:namespace) { create(:group, name: 'No Limit Records') } let_it_be(:namespace) { create(:group, name: 'No Limit Records') }
before do before do
allow_next_instance_of(GitlabSubscriptions::CheckFutureRenewalService, namespace_id: namespace.id) do |service| allow_next_instance_of(GitlabSubscriptions::CheckFutureRenewalService, namespace: namespace) do |service|
allow(service).to receive(:execute).and_return(has_future_renewal) allow(service).to receive(:execute).and_return(has_future_renewal)
end end
end end
......
...@@ -532,6 +532,12 @@ RSpec.describe API::Namespaces do ...@@ -532,6 +532,12 @@ RSpec.describe API::Namespaces do
max_seats_used: 42 max_seats_used: 42
) )
end end
it 'updates the timestamp when the attributes are the same' do
expect do
do_put(namespace.id, admin, gitlab_subscription.attributes)
end.to change { gitlab_subscription.reload.updated_at }
end
end end
end end
......
...@@ -6,11 +6,11 @@ RSpec.describe GitlabSubscriptions::CheckFutureRenewalService, :use_clean_rails_ ...@@ -6,11 +6,11 @@ RSpec.describe GitlabSubscriptions::CheckFutureRenewalService, :use_clean_rails_
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
describe '#execute' do describe '#execute' do
let(:namespace) { create(:namespace) } let_it_be(:namespace) { create(:namespace_with_plan) }
let(:namespace_id) { namespace.id }
let(:cache_key) { "subscription:future_renewal:namespace:#{namespace_id}" }
subject(:execute_service) { described_class.new(namespace_id: namespace_id).execute } let(:cache_key) { "subscription:future_renewal:namespace:#{namespace.gitlab_subscription.cache_key}" }
subject(:execute_service) { described_class.new(namespace: namespace).execute }
where(:in_last_term, :expected_response) do where(:in_last_term, :expected_response) do
true | false true | false
......
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