Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(anrok): create integration error details service #2316

Merged
merged 25 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0c34827
feat: add migration for IntegrationErrorDetails
Jul 18, 2024
bcacbcc
feat: add polymorphic relation on integration_error_details to the in…
Jul 22, 2024
a4b58d2
feat: found better name for the abstraction
Jul 22, 2024
396276b
feat: more discussion on abstractions at error_details
Jul 22, 2024
08b9fcd
feat: add model for integration_error_details
Jul 18, 2024
a94ae7e
feat: WIP adding test setup for new model
Jul 19, 2024
a15c3d4
feat: rename integration relation on integration_error_details to err…
Jul 22, 2024
eb112e8
feat: better definition of integration_error_details factory
Jul 22, 2024
806e07a
feat: rename integration_error_detail model to error_detail
Jul 22, 2024
13abfaf
feat: update error_detail model to belong to organization
annvelents Jul 23, 2024
4bbef33
feat: remove integration association from error_detail
annvelents Jul 23, 2024
55b3025
change error_code type on error_detail to integer
annvelents Jul 24, 2024
89b18b2
unite two migrations
annvelents Jul 24, 2024
6324999
unite two migrations
annvelents Jul 24, 2024
9c1fabc
feat: WIP adding test setup for new model
Jul 19, 2024
b0c3fb6
feat: WIP - add create integration_error_detail service
Jul 22, 2024
8f59836
feat: more tests for the new service
Jul 22, 2024
5931cdc
feat: remove not needed duplications
Jul 22, 2024
7eada19
feat: delete accidentally added empty files
annvelents Jul 23, 2024
36fd2f1
feat: add relation to organization for error_detail
annvelents Jul 23, 2024
353e474
pr review comments
annvelents Jul 23, 2024
ada49c7
add record invalid rescue on error details creation
annvelents Jul 23, 2024
0e51768
feat: remove integration form event_detail
annvelents Jul 23, 2024
3154c9e
remove integration from attr reader for base error_detail service
annvelents Jul 24, 2024
5df6e76
remove accidentally added check on integration_error_details relation
annvelents Jul 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/services/error_details/base_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module ErrorDetails
class BaseService < BaseService
def initialize(params:, owner:, organization:)
@params = params
@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, :owner, :organization
end
end
30 changes: 30 additions & 0 deletions app/services/error_details/create_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module ErrorDetails
class CreateService < BaseService
def call
result = super
return result unless result.success?

create_error_details!
end

private

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

result.error_details = new_error
result
rescue ArgumentError => e
annvelents marked this conversation as resolved.
Show resolved Hide resolved
result.validation_failure!(errors: e.message)
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
end
end
end
82 changes: 82 additions & 0 deletions spec/services/error_details/create_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

require 'rails_helper'

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

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

let(:params) do
{
error_code: 'not_provided',
details: {'error_code' => 'taxDateTooFarInFuture'}
annvelents marked this conversation as resolved.
Show resolved Hide resolved
}
end

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

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.organization_id).to eq(organization.id)
expect(result.error_details.details).to eq(params[:details])
annvelents marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end

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

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::NotFoundFailure)
expect(result.error.message).to include('owner_not_found')
end
end
end

context 'when error code is not registered in enum' do
subject(:service_call) { described_class.call(params:, owner:, organization:) }

let(:params) do
{
error_code: 'this_error_code_will_never_achieve_its_goal',
details: {'error_received' => '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
end
end