From 8a1648c58c400a17c8d2e18330b8340eb6831828 Mon Sep 17 00:00:00 2001 From: Ivan Novosad Date: Tue, 28 May 2024 13:32:49 +0200 Subject: [PATCH] fix(integrations): Sync credit notes - create job --- .../aggregator/credit_notes/create_job.rb | 18 +++++++++ .../credit_notes/create_job_spec.rb | 25 +++++++++++++ .../credit_notes/create_service_spec.rb | 37 +++++++++---------- 3 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 app/jobs/integrations/aggregator/credit_notes/create_job.rb create mode 100644 spec/jobs/integrations/aggregator/credit_notes/create_job_spec.rb diff --git a/app/jobs/integrations/aggregator/credit_notes/create_job.rb b/app/jobs/integrations/aggregator/credit_notes/create_job.rb new file mode 100644 index 000000000000..bc27822d6d71 --- /dev/null +++ b/app/jobs/integrations/aggregator/credit_notes/create_job.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Integrations + module Aggregator + module CreditNotes + class CreateJob < ApplicationJob + queue_as 'integrations' + + retry_on LagoHttpClient::HttpError, wait: :exponentially_longer, attempts: 3 + + def perform(credit_note:) + result = Integrations::Aggregator::CreditNotes::CreateService.call(credit_note:) + result.raise_if_error! + end + end + end + end +end diff --git a/spec/jobs/integrations/aggregator/credit_notes/create_job_spec.rb b/spec/jobs/integrations/aggregator/credit_notes/create_job_spec.rb new file mode 100644 index 000000000000..e2b174b4da5f --- /dev/null +++ b/spec/jobs/integrations/aggregator/credit_notes/create_job_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Integrations::Aggregator::CreditNotes::CreateJob, type: :job do + subject(:create_job) { described_class } + + let(:service) { instance_double(Integrations::Aggregator::CreditNotes::CreateService) } + let(:credit_note) { create(:credit_note) } + let(:result) { BaseService::Result.new } + + before do + allow(Integrations::Aggregator::CreditNotes::CreateService).to receive(:new).and_return(service) + allow(service).to receive(:call).and_return(result) + end + + it 'calls the aggregator create credit_note service' do + described_class.perform_now(credit_note:) + + aggregate_failures do + expect(Integrations::Aggregator::CreditNotes::CreateService).to have_received(:new) + expect(service).to have_received(:call) + end + end +end diff --git a/spec/services/integrations/aggregator/credit_notes/create_service_spec.rb b/spec/services/integrations/aggregator/credit_notes/create_service_spec.rb index ea8135cfebbd..152087b4e583 100644 --- a/spec/services/integrations/aggregator/credit_notes/create_service_spec.rb +++ b/spec/services/integrations/aggregator/credit_notes/create_service_spec.rb @@ -209,29 +209,28 @@ integration.save! end - # TODO: uncomment after jobs are merged - # describe '#call_async' do - # subject(:service_call_async) { described_class.new(credit_note:).call_async } + describe '#call_async' do + subject(:service_call_async) { described_class.new(credit_note:).call_async } - # context 'when credit_note exists' do - # it 'enqueues credit_note create job' do - # expect { service_call_async }.to enqueue_job(Integrations::Aggregator::CreditNotes::CreateJob) - # end - # end + context 'when credit_note exists' do + it 'enqueues credit_note create job' do + expect { service_call_async }.to enqueue_job(Integrations::Aggregator::CreditNotes::CreateJob) + end + end - # context 'when credit_note does not exist' do - # let(:credit_note) { nil } + context 'when credit_note does not exist' do + let(:credit_note) { nil } - # it 'returns an error' do - # result = service_call_async + it 'returns an error' do + result = service_call_async - # aggregate_failures do - # expect(result).not_to be_success - # expect(result.error.error_code).to eq('credit_note_not_found') - # end - # end - # end - # end + aggregate_failures do + expect(result).not_to be_success + expect(result.error.error_code).to eq('credit_note_not_found') + end + end + end + end describe '#call' do context 'when service call is successful' do