Skip to content

Commit

Permalink
feat: add relation to organization for error_detail
Browse files Browse the repository at this point in the history
  • Loading branch information
annvelents committed Jul 23, 2024
1 parent 97fe754 commit 699c9fc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 43 deletions.
Empty file.
6 changes: 4 additions & 2 deletions app/services/error_details/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

module ErrorDetails
class BaseService < BaseService
def initialize(params:, integration: nil, owner:)
def initialize(params:, integration: nil, owner:, organization:)
@params = params
@integration = integration
@owner = owner
@organization = organization

super
end

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

private

attr_reader :params, :integration, :owner
attr_reader :params, :integration, :owner, :organization
end
end
10 changes: 7 additions & 3 deletions app/services/error_details/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module ErrorDetails
class CreateService < BaseService
def initialize(params:, integration: nil, owner:)
super(params:, integration:, owner:)
def initialize(params:, owner:, organization:, integration: nil)
super(params:, integration:, owner:, organization:)
end

def call
Expand All @@ -19,14 +19,18 @@ def call
private

def create_error_details!
new_error = ErrorDetail.create!(
new_error = ErrorDetail.create(
integration:,
owner:,
organization:,
error_code: params[:error_code],
details: params[:details]
)

result.error_details = new_error
result
rescue ArgumentError => e
result.validation_failure!(errors: e.message)
end
end
end
105 changes: 67 additions & 38 deletions spec/services/error_details/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,95 @@
let(:owner) { create(:invoice, organization:, customer:) }

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

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

context 'when all both - owner and integration are provided' do
it 'creates an integration_error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(1)
end
context 'when created succesfully' do
context 'when all - owner, organization and integration are provided' do
it 'creates an error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(1)
end

it 'returns created integration_error_detail' do
result = service_call
it 'returns created error_detail' do
result = service_call

aggregate_failures do
expect(result).to be_success
expect(result.error_details.owner_id).to eq(owner.id)
expect(result.error_details.owner_type).to eq(owner.class.to_s)
expect(result.error_details.integration_id).to eq(integration.id)
expect(result.error_details.integration.class.to_s).to eq(integration.class.to_s)
expect(result.error_details.details).to eq(params[:details])
aggregate_failures do
expect(result).to be_success
expect(result.error_details.owner_id).to eq(owner.id)
expect(result.error_details.owner_type).to eq(owner.class.to_s)
expect(result.error_details.integration_id).to eq(integration.id)
expect(result.error_details.integration.class.to_s).to eq(integration.class.to_s)
expect(result.error_details.organization_id).to eq(organization.id)
expect(result.error_details.details).to eq(params[:details])
end
end
end
end

context 'when no integration association is provided' do
subject(:service_call) { described_class.call(params:, owner:) }
context 'when no integration association is provided' do
subject(:service_call) { described_class.call(params:, owner:, organization:) }

it 'creates an integration_error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(1)
end
it 'creates an error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(1)
end

it 'returns created integration_error_detail' do
result = service_call
it 'returns created error_detail' do
result = service_call

aggregate_failures do
expect(result).to be_success
expect(result.error_details.owner_id).to eq(owner.id)
expect(result.error_details.owner_type).to eq(owner.class.to_s)
expect(result.error_details.integration_id).to eq(nil)
expect(result.error_details.integration_type).to eq(nil)
expect(result.error_details.details).to eq(params[:details])
aggregate_failures do
expect(result).to be_success
expect(result.error_details.owner_id).to eq(owner.id)
expect(result.error_details.owner_type).to eq(owner.class.to_s)
expect(result.error_details.integration_id).to eq(nil)
expect(result.error_details.integration_type).to eq(nil)
expect(result.error_details.details).to eq(params[:details])
end
end
end
end

context 'when no owner is provided' do
subject(:service_call) { described_class.call(params:, integration:, owner: nil) }
context 'when not created succesfully' do
context 'when no owner is provided' do
subject(:service_call) { described_class.call(params:, integration:, owner: nil, organization:) }

it 'does not create an error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(0)
end

it 'does not create an integration_error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(0)
it 'returns error for error_detail' do
result = service_call
aggregate_failures do
expect(result.error).to be_a(BaseService::NotFoundFailure)
expect(result.error.message).to include('owner_not_found')
end
end
end

it 'returns created integration_error_detail' do
result = service_call
aggregate_failures do
expect(result.error).to be_a(BaseService::NotFoundFailure)
expect(result.error.message).to include('owner_not_found')
context 'when error code is not registered in enum' do
subject(:service_call) { described_class.call(params:, integration:, owner:, organization:) }

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

it 'does not create an error_detail' do
expect { service_call }.to change(ErrorDetail, :count).by(0)
end

it 'returns error for error_detail' do
result = service_call
aggregate_failures do
expect(result.error).to be_a(BaseService::ValidationFailure)
expect(result.error.message).to include("'this_error_code_will_never_achieve_its_goal' is not a valid error_code")
end
end
end
end
Expand Down

0 comments on commit 699c9fc

Please sign in to comment.