Skip to content

Commit

Permalink
feat: added invoices service
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomiguelpinto committed Oct 2, 2024
1 parent 1abe762 commit a7cf3c7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def action_path

def call
return unless integration.type == 'Integrations::HubspotIntegration'
return result if integration.invoices_properties_version == VERSION

response = http_client.post_with_response(payload, headers)
if response.success?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Integrations::Hubspot::Objects::DeployInvoicesService do
subject(:deploy_invoices_service) { described_class.new(integration:) }

let(:integration) { create(:hubspot_integration) }

describe '.call' do
let(:http_client) { instance_double(LagoHttpClient::Client) }
let(:endpoint) { "https://api.nango.dev/v1/hubspot/object" }
let(:response) { instance_double('Response', success?: true) }

before do
allow(LagoHttpClient::Client).to receive(:new)
.with(endpoint)
.and_return(http_client)
allow(http_client).to receive(:post_with_response).and_return(response)

integration.invoices_properties_version = nil
integration.save!
end

it 'successfully deploys invoices and updates the invoices_properties_version' do
deploy_invoices_service.call

aggregate_failures do
expect(LagoHttpClient::Client).to have_received(:new).with(endpoint)
expect(http_client).to have_received(:post_with_response) do |payload, headers|
expect(payload[:name]).to eq('LagoInvoices')
expect(headers['Authorization']).to include('Bearer')
end
expect(integration.reload.invoices_properties_version).to eq(described_class::VERSION)
end
end

context 'when invoices_properties_version is already up-to-date' do
before do
integration.invoices_properties_version = described_class::VERSION
integration.save!
end

it 'does not make an API call and keeps the version unchanged' do
deploy_invoices_service.call

aggregate_failures do
expect(LagoHttpClient::Client).not_to have_received(:new)
expect(http_client).not_to have_received(:post_with_response)
expect(integration.reload.invoices_properties_version).to eq(described_class::VERSION)
end
end
end

context 'when the API call fails' do
let(:response) { instance_double('Response', success?: false) }

it 'does not update the invoices_properties_version' do
deploy_invoices_service.call

aggregate_failures do
expect(LagoHttpClient::Client).to have_received(:new).with(endpoint)
expect(integration.reload.invoices_properties_version).to be_nil
end
end
end
end
end

0 comments on commit a7cf3c7

Please sign in to comment.