-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: WIP - add create integration_error_detail service
- Loading branch information
Anna Velentsevich
authored and
Anna Velentsevich
committed
Jul 22, 2024
1 parent
cf1a3ed
commit b7484ac
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
spec/services/integration_error_details/create_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |