Commit 566c32f9 authored by Josianne Hyson's avatar Josianne Hyson

Destroy UpcomingReconciliation when none upcoming

Previously, when a user cancelled their subscription, we would still
show the upcoming reconciliation alert to the user at the time they
would have originally been reconciled. This is because we create the
UpcomingReconciliation record but we never clean it up when the
reconciliation won't actually happen.

This MR destroys the UpcomingReconciliation record once we stop
receiving the next reconciliation date from the Seat Link API.

Issue: https://gitlab.com/gitlab-org/customers-gitlab-com/-/issues/3722
parent a1a79b1b
......@@ -29,7 +29,7 @@ class SyncSeatLinkRequestWorker
reset_license!(response['license']) if response['license']
save_future_subscriptions(response)
save_reconciliation_dates!(response)
update_reconciliation!(response)
else
raise RequestError, request_error_message(response)
end
......@@ -64,18 +64,22 @@ class SyncSeatLinkRequestWorker
"Seat Link request failed! Code:#{response.code} Body:#{response.body}"
end
def save_reconciliation_dates!(response)
return if response['next_reconciliation_date'].blank? || response['display_alert_from'].blank?
def update_reconciliation!(response)
reconciliation = GitlabSubscriptions::UpcomingReconciliation.next
attributes = {
next_reconciliation_date: Date.parse(response['next_reconciliation_date']),
display_alert_from: Date.parse(response['display_alert_from'])
}
if (reconciliation = GitlabSubscriptions::UpcomingReconciliation.next)
reconciliation.update!(attributes)
if response['next_reconciliation_date'].blank? || response['display_alert_from'].blank?
reconciliation&.destroy!
else
GitlabSubscriptions::UpcomingReconciliation.create!(attributes)
attributes = {
next_reconciliation_date: Date.parse(response['next_reconciliation_date']),
display_alert_from: Date.parse(response['display_alert_from'])
}
if reconciliation
reconciliation.update!(attributes)
else
GitlabSubscriptions::UpcomingReconciliation.create!(attributes)
end
end
end
......
......@@ -107,7 +107,7 @@ RSpec.describe SyncSeatLinkRequestWorker, type: :worker do
current_license = License.current
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).and_call_original
expect { sync_seat_link }.to raise_error
expect { sync_seat_link }.to raise_error ActiveRecord::RecordInvalid
expect(License).to exist(current_license.id)
end
......@@ -146,11 +146,11 @@ RSpec.describe SyncSeatLinkRequestWorker, type: :worker do
context 'when an upcoming_reconciliation already exists' do
it 'updates the upcoming_reconciliation' do
create(:upcoming_reconciliation, :self_managed, next_reconciliation_date: today + 2.days, display_alert_from: today + 1.day)
upcoming_reconciliation = create(:upcoming_reconciliation, :self_managed, next_reconciliation_date: today + 2.days, display_alert_from: today + 1.day)
sync_seat_link
upcoming_reconciliation = GitlabSubscriptions::UpcomingReconciliation.next
upcoming_reconciliation.reload
expect(upcoming_reconciliation.next_reconciliation_date).to eq(today)
expect(upcoming_reconciliation.display_alert_from).to eq(today - 7.days)
......@@ -197,6 +197,36 @@ RSpec.describe SyncSeatLinkRequestWorker, type: :worker do
end
end
context 'when the response does not contain reconciliation dates' do
let(:body) do
{
success: true,
next_reconciliation_date: nil,
display_alert_from: nil
}.to_json
end
before do
stub_request(:post, seat_link_url).to_return(
status: 200,
body: body,
headers: { content_type: 'application/json' }
)
end
it 'destroys the existing upcoming reconciliation record for the instance' do
create(:upcoming_reconciliation, :self_managed)
expect { sync_seat_link }
.to change(GitlabSubscriptions::UpcomingReconciliation, :count)
.by(-1)
end
it 'does not change anything when there is no existing record' do
expect { sync_seat_link }.not_to change(GitlabSubscriptions::UpcomingReconciliation, :count)
end
end
shared_examples 'unsuccessful request' do
context 'when the request is not successful' do
before do
......
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