Skip to content

Commit

Permalink
Feat invoice custom sections - models updates (#2859)
Browse files Browse the repository at this point in the history
## Context

In order to save invoice custom sections, we need to make create new
models related to the new tables

## Description

Added 
- InvoiceCustomSection
- AppliedInvoiceCustomSection
- InvoiceCustomSectionSelection

Updated
- Organization
- Customer
  • Loading branch information
annvelents authored Dec 4, 2024
1 parent 01bc75a commit 89ad7de
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 1 deletion.
27 changes: 27 additions & 0 deletions app/models/applied_invoice_custom_section.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class AppliedInvoiceCustomSection < ApplicationRecord
belongs_to :invoice
end

# == Schema Information
#
# Table name: applied_invoice_custom_sections
#
# id :uuid not null, primary key
# code :string not null
# details :string
# display_name :string
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# invoice_id :uuid not null
#
# Indexes
#
# index_applied_invoice_custom_sections_on_invoice_id (invoice_id)
#
# Foreign Keys
#
# fk_rails_... (invoice_id => invoices.id)
#
4 changes: 4 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class Customer < ApplicationRecord
has_many :applied_taxes, class_name: 'Customer::AppliedTax', dependent: :destroy
has_many :taxes, through: :applied_taxes

has_many :invoice_custom_sections
has_many :invoice_custom_section_selections
has_many :selected_invoice_custom_sections, through: :invoice_custom_section_selections, source: :invoice_custom_section

has_one :stripe_customer, class_name: 'PaymentProviderCustomers::StripeCustomer'
has_one :gocardless_customer, class_name: 'PaymentProviderCustomers::GocardlessCustomer'
has_one :adyen_customer, class_name: 'PaymentProviderCustomers::AdyenCustomer'
Expand Down
1 change: 1 addition & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Invoice < ApplicationRecord

has_many :applied_usage_thresholds
has_many :usage_thresholds, through: :applied_usage_thresholds
has_many :applied_invoice_custom_sections

has_one_attached :file

Expand Down
34 changes: 34 additions & 0 deletions app/models/invoice_custom_section.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

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

belongs_to :organization
end

# == Schema Information
#
# Table name: invoice_custom_sections
#
# id :uuid not null, primary key
# code :string not null
# deleted_at :datetime
# description :string
# details :string
# display_name :string
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :uuid not null
#
# Indexes
#
# idx_on_organization_id_deleted_at_225e3f789d (organization_id,deleted_at)
# index_invoice_custom_sections_on_organization_id (organization_id)
# index_invoice_custom_sections_on_organization_id_and_code (organization_id,code) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (organization_id => organizations.id)
#
31 changes: 31 additions & 0 deletions app/models/invoice_custom_section_selection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class InvoiceCustomSectionSelection < ApplicationRecord
belongs_to :invoice_custom_section
belongs_to :organization, optional: true
belongs_to :customer, optional: true
end

# == Schema Information
#
# Table name: invoice_custom_section_selections
#
# id :uuid not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# customer_id :uuid
# invoice_custom_section_id :uuid not null
# organization_id :uuid
#
# Indexes
#
# idx_on_invoice_custom_section_id_7edbcef7b5 (invoice_custom_section_id)
# index_invoice_custom_section_selections_on_customer_id (customer_id)
# index_invoice_custom_section_selections_on_organization_id (organization_id)
#
# Foreign Keys
#
# fk_rails_... (customer_id => customers.id)
# fk_rails_... (invoice_custom_section_id => invoice_custom_sections.id)
# fk_rails_... (organization_id => organizations.id)
#
4 changes: 3 additions & 1 deletion app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class Organization < ApplicationRecord

has_one :applied_dunning_campaign, -> { where(applied_to_organization: true) }, class_name: "DunningCampaign"

has_one :applied_dunning_campaign, -> { where(applied_to_organization: true) }, class_name: "DunningCampaign"
has_many :invoice_custom_sections
has_many :invoice_custom_section_selections
has_many :selected_invoice_custom_sections, through: :invoice_custom_section_selections, source: :invoice_custom_section

has_one_attached :logo

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

FactoryBot.define do
factory :applied_invoice_custom_section do
invoice
code { Faker::Lorem.words(number: 3).join('_') }
name { Faker::Lorem.words(number: 3).join(' ') }
display_name { Faker::Lorem.words(number: 3).join(' ') }
details { 'These details are shown in the invoice' }
end
end
14 changes: 14 additions & 0 deletions spec/factories/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,19 @@
create(:salesforce_customer, customer:)
end
end

trait :with_inherited_invoice_custom_sections do
organization { create(:organization, :with_invoice_custom_sections) }
end

trait :with_custom_invoice_custom_section_selections do
after :create do |customer|
customer.invoice_custom_section_selections = create_list(:invoice_custom_section, 3, organization: customer.organization)
end
end

trait :with_skipped_invoice_custom_section_selections do
skip_invoice_custom_sections { true }
end
end
end
11 changes: 11 additions & 0 deletions spec/factories/invoice_custom_sections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

FactoryBot.define do
factory :invoice_custom_section do
organization
code { Faker::Lorem.words(number: 3).join('_') }
name { Faker::Lorem.words(number: 3).join(' ') }
display_name { Faker::Lorem.words(number: 3).join(' ') }
details { 'These details are shown in the invoice' }
end
end
10 changes: 10 additions & 0 deletions spec/factories/organizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@
organization.webhook_endpoints.create!(webhook_url: evaluator.webhook_url)
end
end

trait :with_invoice_custom_sections do
after :create do |org|
sections = []
3.times do
sections << create(:invoice_custom_section, organization: org)
end
org.invoice_custom_section_selections = sections
end
end
end
end
9 changes: 9 additions & 0 deletions spec/models/applied_invoice_custom_section_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe AppliedInvoiceCustomSection, type: :model do
subject(:applied_invoice_custom_section) { create(:applied_invoice_custom_section) }

it { is_expected.to belong_to(:invoice) }
end
3 changes: 3 additions & 0 deletions spec/models/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
it { is_expected.to have_one(:hubspot_customer) }
it { is_expected.to have_one(:salesforce_customer) }

it { is_expected.to have_many(:invoice_custom_section_selections) }
it { is_expected.to have_many(:selected_invoice_custom_sections) }

it 'sets the default value to inherit' do
expect(customer.finalize_zero_amount_invoice).to eq "inherit"
end
Expand Down
9 changes: 9 additions & 0 deletions spec/models/invoice_custom_section_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe InvoiceCustomSection, type: :model do
subject(:invoice_custom_section) { create(:invoice_custom_section) }

it { is_expected.to belong_to(:organization) }
end
3 changes: 3 additions & 0 deletions spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
it { is_expected.to have_many(:data_exports) }
it { is_expected.to have_many(:dunning_campaigns) }
it { is_expected.to have_many(:daily_usages) }
it { is_expected.to have_many(:invoice_custom_sections) }
it { is_expected.to have_many(:invoice_custom_section_selections) }
it { is_expected.to have_many(:selected_invoice_custom_sections) }

it { is_expected.to have_one(:applied_dunning_campaign).conditions(applied_to_organization: true) }

Expand Down

0 comments on commit 89ad7de

Please sign in to comment.