From a7cf3c766d396b0ab48ffd517ca918bf76fbc824 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 2 Oct 2024 12:19:31 +0100 Subject: [PATCH] feat: added invoices service --- .../objects/deploy_invoices_service.rb | 1 + .../objects/deploy_invoices_service_spec.rb | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 spec/services/integrations/hubspot/objects/deploy_invoices_service_spec.rb diff --git a/app/services/integrations/hubspot/objects/deploy_invoices_service.rb b/app/services/integrations/hubspot/objects/deploy_invoices_service.rb index 864077b1afe..9f847739dc4 100644 --- a/app/services/integrations/hubspot/objects/deploy_invoices_service.rb +++ b/app/services/integrations/hubspot/objects/deploy_invoices_service.rb @@ -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? diff --git a/spec/services/integrations/hubspot/objects/deploy_invoices_service_spec.rb b/spec/services/integrations/hubspot/objects/deploy_invoices_service_spec.rb new file mode 100644 index 00000000000..1f92a9a210a --- /dev/null +++ b/spec/services/integrations/hubspot/objects/deploy_invoices_service_spec.rb @@ -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 \ No newline at end of file