Skip to content

Commit

Permalink
feat: WIP - add create integration_error_detail service
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Velentsevich authored and Anna Velentsevich committed Jul 22, 2024
1 parent cf1a3ed commit b7484ac
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/services/integration_error_details/base_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module IntegrationErrorDetails
class BaseService < BaseService
def initialize(params:, error_producer:, owner:)
@params = params
@error_producer = error_producer
@owner = owner

super
end

def call
result.not_found_failure!(resource: 'error_producer') unless error_producer
result.not_found_failure!(resource: 'owner') unless owner
result
end

private

attr_reader :params, :error_producer, :owner
end
end
36 changes: 36 additions & 0 deletions app/services/integration_error_details/create_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module IntegrationErrorDetails
class CreateService < BaseService
def initialize(params:, error_producer:, owner:)
@error_producer = error_producer
@owner = owner
super(params:, error_producer:, owner:)
end

def call
result = super
return result if result.error

res = create_integration_error_details!
return res if res&.error

res
end

private

attr_reader :error_producer, :owner

def create_integration_error_details!
new_integration_error = IntegrationErrorDetail.create!(
error_producer:,
owner:,
details: params[:details]
)

result.integration_error_details = new_integration_error
result
end
end
end
40 changes: 40 additions & 0 deletions spec/services/integration_error_details/create_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe IntegrationErrorDetails::CreateService, type: :service do
let(:membership) { create(:membership) }
let(:organization) { membership.organization }
let(:customer) { create(:customer, organization:) }
let(:error_producer) { create(:anrok_integration, organization:) }
let(:owner) { create(:invoice, organization:, customer:) }

describe '#call' do
subject(:service_call) { described_class.call(params:, error_producer:, owner:) }

let(:params) do
{
details: {'error_code' => 'taxDateTooFarInFuture'}
}
end

context 'when all required data present' do
it 'creates an integration_error_detail' do
expect { service_call }.to change(IntegrationErrorDetail, :count).by(1)
end

it 'returns created integration_error_detail' do
result = service_call

aggregate_failures do
expect(result).to be_success
expect(result.integration_error_details.owner_id).to eq(owner.id)
expect(result.integration_error_details.owner_type).to eq(owner.class.to_s)
expect(result.integration_error_details.error_producer_id).to eq(error_producer.id)
expect(result.integration_error_details.error_producer.class.to_s).to eq(error_producer.class.to_s)
expect(result.integration_error_details.details).to eq(params[:details])
end
end
end
end
end

0 comments on commit b7484ac

Please sign in to comment.