Commit a6b1f340 authored by Reuben Pereira's avatar Reuben Pereira

Save reconciliation dates returned in seat usage API response

Save the reconciliation dates in the UpcomingReconciliation model.
parent 3cb81609
......@@ -25,6 +25,8 @@ class SyncSeatLinkRequestWorker
if response.success?
reset_license!(response['license']) if response['license']
save_reconciliation_dates!(response)
else
raise RequestError, request_error_message(response)
end
......@@ -59,4 +61,19 @@ class SyncSeatLinkRequestWorker
def request_error_message(response)
"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?
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)
else
GitlabSubscriptions::UpcomingReconciliation.create!(attributes)
end
end
end
......@@ -123,6 +123,40 @@ RSpec.describe SyncSeatLinkRequestWorker, type: :worker do
end
end
context 'when response contains reconciliation dates' do
let(:body) { { success: true, next_reconciliation_date: today.to_s, display_alert_from: (today - 7.days).to_s }.to_json }
let(:today) { Date.current }
before do
stub_request(:post, seat_link_url).to_return(
status: 200,
body: body,
headers: { content_type: 'application/json' }
)
end
it 'saves the reconciliation dates' do
sync_seat_link
upcoming_reconciliation = GitlabSubscriptions::UpcomingReconciliation.next
expect(upcoming_reconciliation.next_reconciliation_date).to eq(today)
expect(upcoming_reconciliation.display_alert_from).to eq(today - 7.days)
end
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)
sync_seat_link
upcoming_reconciliation = GitlabSubscriptions::UpcomingReconciliation.next
expect(upcoming_reconciliation.next_reconciliation_date).to eq(today)
expect(upcoming_reconciliation.display_alert_from).to eq(today - 7.days)
end
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