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: Add customer_type to Customers #2544

Merged
merged 13 commits into from
Sep 9, 2024
24 changes: 23 additions & 1 deletion app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ class Customer < ApplicationRecord
:finalize
].freeze

attribute :finalize_zero_amount_invoice, :integer # rails 7.1 check the field exists when defining enum and when running the migration first time is not there
CUSTOMER_TYPES = {
company: 'company',
individual: 'individual'
}.freeze

attribute :finalize_zero_amount_invoice, :integer
enum finalize_zero_amount_invoice: FINALIZE_ZERO_AMOUNT_INVOICE_OPTIONS, _prefix: :finalize_zero_amount_invoice
attribute :customer_type, :string
enum customer_type: CUSTOMER_TYPES, _prefix: :customer_type

before_save :ensure_slug

Expand Down Expand Up @@ -75,6 +82,18 @@ def self.ransackable_attributes(_auth_object = nil)
%w[id name external_id email]
end

def display_name(prefer_legal_name: false)
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
primary_name = prefer_legal_name ? legal_name.presence : name.presence

names = [primary_name]
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
if firstname.present? || lastname.present?
names << '-' if primary_name.present?
names << firstname
names << lastname
end
names.compact.join(' ')
end

def active_subscription
subscriptions.active.order(started_at: :desc).first
end
Expand Down Expand Up @@ -179,11 +198,14 @@ def ensure_slug
# city :string
# country :string
# currency :string
# customer_type :enum
# deleted_at :datetime
# document_locale :string
# email :string
# finalize_zero_amount_invoice :integer default("inherit"), not null
# firstname :string
# invoice_grace_period :integer
# lastname :string
# legal_name :string
# legal_number :string
# logo_url :string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class AddFirstnameAndLastnameToCustomers < ActiveRecord::Migration[7.1]
def change
safety_assured do
change_table :customers, bulk: true do |t|
t.string :firstname
t.string :lastname
end
end
end
end
13 changes: 13 additions & 0 deletions db/migrate/20240906170048_add_customer_type_to_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class AddCustomerTypeToCustomers < ActiveRecord::Migration[7.1]
def change
create_enum :customer_type, %w[company individual]

safety_assured do
change_table :customers do |t|
t.enum :customer_type, enum_type: 'customer_type', null: true
end
end
end
end
6 changes: 5 additions & 1 deletion db/schema.rb

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

106 changes: 106 additions & 0 deletions spec/models/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,112 @@
end
end

describe '#display_name' do
subject(:customer) { build(:customer, name:, legal_name:, firstname:, lastname:) }
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved

let(:name) { 'ACME Inc' }
let(:legal_name) { 'ACME International Corporation' }
let(:firstname) { 'Thomas' }
let(:lastname) { 'Anderson' }

context 'when all fields are nil' do
let(:name) { nil }
let(:legal_name) { nil }
let(:firstname) { nil }
let(:lastname) { nil }

it 'returns an empty string' do
expect(customer.display_name).to eq('')
end
end

context 'when name and legal_name are nil' do
let(:name) { nil }
let(:legal_name) { nil }

it 'returns only firstname and lastname if present' do
expect(customer.display_name).to eq('Thomas Anderson')
end
end

context 'when firstname and lastname are nil' do
let(:firstname) { nil }
let(:lastname) { nil }

it 'returns only the name when prefer_legal_name is false' do
expect(customer.display_name(prefer_legal_name: false)).to eq('ACME Inc')
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
end

it 'returns only the legal_name when prefer_legal_name is true' do
expect(customer.display_name(prefer_legal_name: true)).to eq('ACME International Corporation')
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
end
end

context 'when name is present and both firstname and lastname are present' do
let(:legal_name) { nil }

it 'returns only firstname and lastname when prefer_legal_name is true' do
expect(customer.display_name(prefer_legal_name: true)).to eq('Thomas Anderson')
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
end

it 'returns name with firstname and lastname when prefer_legal_name is false' do
expect(customer.display_name).to eq('ACME Inc - Thomas Anderson')
end
end

context 'when legal_name is present and both firstname and lastname are present' do
let(:name) { nil }

it 'returns legal_name with firstname and lastname when prefer_legal_name is true' do
expect(customer.display_name(prefer_legal_name: true)).to eq('ACME International Corporation - Thomas Anderson')
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
end

it 'returns only firstname and lastname when prefer_legal_name is false' do
expect(customer.display_name(prefer_legal_name: false)).to eq('Thomas Anderson')
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
end
end

context 'when all fields are present' do
it 'returns legal_name with firstname and lastname when prefer_legal_name is true' do
expect(customer.display_name(prefer_legal_name: true)).to eq('ACME International Corporation - Thomas Anderson')
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
end

it 'returns name with firstname and lastname when prefer_legal_name is false' do
expect(customer.display_name(prefer_legal_name: false)).to eq('ACME Inc - Thomas Anderson')
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

describe 'customer_type enum' do
subject(:customer) { build(:customer, customer_type:) }
brunomiguelpinto marked this conversation as resolved.
Show resolved Hide resolved

context 'when customer_type is company' do
let(:customer_type) { 'company' }

it 'identifies the customer as a company' do
expect(customer.customer_type).to eq('company')
expect(customer.customer_type_company?).to be true
end
end

context 'when customer_type is individual' do
let(:customer_type) { 'individual' }

it 'identifies the customer as an individual' do
expect(customer.customer_type).to eq('individual')
expect(customer.customer_type_individual?).to be true
end
end

context 'when customer_type is nil' do
subject(:customer) { build(:customer) }

it 'defaults to nil for existing customers' do
expect(customer.customer_type).to be_nil
end
end
end

describe 'preferred_document_locale' do
subject(:customer) do
described_class.new(
Expand Down