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): add model for error_details #2311

Merged
merged 18 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
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
12 changes: 12 additions & 0 deletions app/models/error_detail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class ErrorDetail < ApplicationRecord
include Discard::Model
self.discard_column = :deleted_at

belongs_to :owner, polymorphic: true
belongs_to :organization

ERROR_CODES = %w[not_provided tax_error]
enum error_code: ERROR_CODES
end
4 changes: 4 additions & 0 deletions app/models/integrations/anrok_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module Integrations
class AnrokIntegration < BaseIntegration
has_many :error_details, -> { where({error_details: {error_code: 'tax_error'}}) },
lovrocolic marked this conversation as resolved.
Show resolved Hide resolved
primary_key: :organization_id,
foreign_key: :organization_id

validates :connection_id, :api_key, presence: true

secrets_accessors :connection_id, :api_key
Expand Down
1 change: 1 addition & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Invoice < ApplicationRecord
has_many :applied_taxes, class_name: 'Invoice::AppliedTax', dependent: :destroy
has_many :taxes, through: :applied_taxes
has_many :integration_resources, as: :syncable
has_many :error_details, as: :owner, dependent: :destroy

has_one_attached :file

Expand Down
1 change: 1 addition & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Organization < ApplicationRecord
has_many :webhooks, through: :webhook_endpoints
has_many :cached_aggregations
has_many :data_exports
has_many :error_details

has_many :stripe_payment_providers, class_name: 'PaymentProviders::StripeProvider'
has_many :gocardless_payment_providers, class_name: 'PaymentProviders::GocardlessProvider'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class RemoveIntegrationReferenceFromErrorDetail < ActiveRecord::Migration[7.1]
def change
change_table :error_details do |t|
t.remove_references :integration, polymorphic: true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class ChangeErrorCodeTypeOnErrorDetails < ActiveRecord::Migration[7.1]
lovrocolic marked this conversation as resolved.
Show resolved Hide resolved
def up
change_table :error_details, bulk: true do |t|
t.remove :error_code
t.integer :error_code, null: false, default: 0, index: true
end
end

def down
change_table :error_details, bulk: true do |t|
t.remove :error_code
t.string :error_code, index: true, null: false, default: 'not_provided'
end
end
end
7 changes: 2 additions & 5 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions spec/factories/error_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

FactoryBot.define do
factory :error_detail do
organization
association :owner, factory: %i[invoice].sample
end
end
6 changes: 6 additions & 0 deletions spec/factories/invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
payment_dispute_lost_at { DateTime.current - 1.day }
end

trait :with_error do
after :create do |i|
create(:error_detail, owner: i)
end
end

trait :failed do
status { :failed }
end
Expand Down
8 changes: 8 additions & 0 deletions spec/models/error_detail_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ErrorDetail, type: :model do
it { is_expected.to belong_to(:owner) }
it { is_expected.to belong_to(:organization) }
end
1 change: 1 addition & 0 deletions spec/models/integrations/anrok_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:connection_id) }
it { is_expected.to have_many(:error_details) }

describe 'validations' do
it 'validates uniqueness of the code' do
Expand Down
1 change: 1 addition & 0 deletions spec/models/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
it_behaves_like 'paper_trail traceable'

it { is_expected.to have_many(:integration_resources) }
it { is_expected.to have_many(:error_details) }

describe 'validation' do
describe 'of payment dispute lost absence' do
Expand Down