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 services #2551

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion app/services/customers/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def create_from_api(organization:, params:)
customer.net_payment_term = params[:net_payment_term] if params.key?(:net_payment_term)
customer.external_salesforce_id = params[:external_salesforce_id] if params.key?(:external_salesforce_id)
customer.finalize_zero_amount_invoice = params[:finalize_zero_amount_invoice] if params.key?(:finalize_zero_amount_invoice)
customer.firstname = params[:firstname] if params.key?(:firstname)
customer.lastname = params[:lastname] if params.key?(:lastname)
customer.customer_type = params[:customer_type] if params.key?(:customer_type)
if params.key?(:tax_identification_number)
customer.tax_identification_number = params[:tax_identification_number]
end
Expand Down Expand Up @@ -149,7 +152,10 @@ def create(**args)
payment_provider_code: args[:payment_provider_code],
currency: args[:currency],
document_locale: billing_configuration[:document_locale],
tax_identification_number: args[:tax_identification_number]
tax_identification_number: args[:tax_identification_number],
firstname: args[:firstname],
lastname: args[:lastname],
customer_type: args[:customer_type]
)

if args.key?(:finalize_zero_amount_invoice)
Expand Down
3 changes: 3 additions & 0 deletions app/services/customers/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def update(**args)
customer.shipping_zipcode = shipping_address[:zipcode] if shipping_address.key?(:zipcode)
customer.shipping_state = shipping_address[:state] if shipping_address.key?(:state)
customer.shipping_country = shipping_address[:country]&.upcase if shipping_address.key?(:country)
customer.firstname = args[:firstname] if args.key?(:firstname)
customer.lastname = args[:lastname] if args.key?(:lastname)
customer.customer_type = args[:customer_type] if args.key?(:customer_type)

assign_premium_attributes(customer, args)

Expand Down
55 changes: 55 additions & 0 deletions spec/services/customers/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
external_id:,
name: 'Foo Bar',
currency: 'EUR',
firstname: 'First',
lastname: 'Last',
tax_identification_number: '123456789',
billing_configuration: {
document_locale: 'fr'
Expand Down Expand Up @@ -46,6 +48,9 @@
expect(customer.organization_id).to eq(organization.id)
expect(customer.external_id).to eq(create_args[:external_id])
expect(customer.name).to eq(create_args[:name])
expect(customer.firstname).to eq(create_args[:firstname])
expect(customer.lastname).to eq(create_args[:lastname])
expect(customer.customer_type).to be_nil
expect(customer.currency).to eq(create_args[:currency])
expect(customer.tax_identification_number).to eq(create_args[:tax_identification_number])
expect(customer.timezone).to be_nil
Expand Down Expand Up @@ -156,6 +161,30 @@
end
end

context 'with customer_type' do
let(:create_args) do
{
external_id:,
name: 'Foo Bar',
currency: 'EUR',
customer_type: 'company'
}
end

it 'creates customer with correct customer_type' do
result = customers_service.create_from_api(
organization:,
params: create_args
)

aggregate_failures do
expect(result).to be_success
customer = result.customer
expect(customer.customer_type).to eq(create_args[:customer_type])
end
end
end

context 'with metadata' do
let(:create_args) do
{
Expand Down Expand Up @@ -975,6 +1004,8 @@
{
external_id: SecureRandom.uuid,
name: 'Foo Bar',
firstname: 'First',
lastname: 'Last',
organization_id: organization.id,
timezone: 'Europe/Paris',
invoice_grace_period: 2
Expand All @@ -988,12 +1019,36 @@
expect(result).to be_success

customer = result.customer
expect(customer.firstname).to eq(create_args[:firstname])
expect(customer.lastname).to eq(create_args[:lastname])
expect(customer.customer_type).to be_nil
expect(customer.timezone).to eq('Europe/Paris')
expect(customer.invoice_grace_period).to eq(2)
end
end
end

context 'with customer_type' do
let(:create_args) do
{
external_id: SecureRandom.uuid,
name: 'Foo Bar',
customer_type: 'individual',
organization_id: organization.id
}
end

it 'creates customer with customer_type' do
result = customers_service.create(**create_args)

aggregate_failures do
expect(result).to be_success
customer = result.customer
expect(customer.customer_type).to eq(create_args[:customer_type])
end
end
end

context 'with metadata' do
let(:create_args) do
{
Expand Down
10 changes: 8 additions & 2 deletions spec/services/customers/update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
{
id: customer.id,
name: 'Updated customer name',
firstname: 'Updated customer firstname',
lastname: 'Updated customer lastname',
customer_type: 'individual',
tax_identification_number: '2246',
net_payment_term: 8,
external_id:,
Expand All @@ -34,8 +37,11 @@

updated_customer = result.customer
aggregate_failures do
expect(updated_customer.name).to eq('Updated customer name')
expect(updated_customer.tax_identification_number).to eq('2246')
expect(updated_customer.name).to eq(update_args[:name])
expect(updated_customer.firstname).to eq(update_args[:firstname])
expect(updated_customer.lastname).to eq(update_args[:lastname])
expect(updated_customer.customer_type).to eq(update_args[:customer_type])
expect(updated_customer.tax_identification_number).to eq(update_args[:tax_identification_number])

shipping_address = update_args[:shipping_address]
expect(updated_customer.shipping_city).to eq(shipping_address[:city])
Expand Down