Skip to content

Commit

Permalink
feat(grace_period): Move properties into billing configuration for or…
Browse files Browse the repository at this point in the history
…ganization
  • Loading branch information
rsempe committed Nov 21, 2022
1 parent cb1ff09 commit ee233a5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 35 deletions.
8 changes: 5 additions & 3 deletions app/controllers/api/v1/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def update
def input_params
params.require(:organization).permit(
:webhook_url,
:vat_rate,
:country,
:address_line1,
:address_line2,
Expand All @@ -34,8 +33,11 @@ def input_params
:city,
:legal_name,
:legal_number,
:invoice_footer,
:invoice_grace_period,
billing_configuration: [
:invoice_footer,
:invoice_grace_period,
:vat_rate,
],
)
end
end
Expand Down
8 changes: 5 additions & 3 deletions app/serializers/v1/organization_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def serialize
name: model.name,
created_at: model.created_at.iso8601,
webhook_url: model.webhook_url,
vat_rate: model.vat_rate,
country: model.country,
address_line1: model.address_line1,
address_line2: model.address_line2,
Expand All @@ -18,8 +17,11 @@ def serialize
city: model.city,
legal_name: model.legal_name,
legal_number: model.legal_number,
invoice_footer: model.invoice_footer,
invoice_grace_period: model.invoice_grace_period,
billing_configuration: {
invoice_footer: model.invoice_footer,
invoice_grace_period: model.invoice_grace_period,
vat_rate: model.vat_rate,
},
}
end
end
Expand Down
11 changes: 8 additions & 3 deletions app/services/organizations/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def update(**args)

def update_from_api(params:)
organization.webhook_url = params[:webhook_url] if params.key?(:webhook_url)
organization.vat_rate = params[:vat_rate] if params.key?(:vat_rate)
organization.country = params[:country] if params.key?(:country)
organization.address_line1 = params[:address_line1] if params.key?(:address_line1)
organization.address_line2 = params[:address_line2] if params.key?(:address_line2)
Expand All @@ -45,8 +44,14 @@ def update_from_api(params:)
organization.city = params[:city] if params.key?(:city)
organization.legal_name = params[:legal_name] if params.key?(:legal_name)
organization.legal_number = params[:legal_number] if params.key?(:legal_number)
organization.invoice_footer = params[:invoice_footer] if params.key?(:invoice_footer)
organization.invoice_grace_period = params[:invoice_grace_period] if params.key?(:invoice_grace_period)

if params.key?(:billing_configuration)
billing = params[:billing_configuration]
organization.invoice_footer = billing[:invoice_footer] if billing.key?(:invoice_footer)
organization.invoice_grace_period = billing[:invoice_grace_period] if billing.key?(:invoice_grace_period)
organization.vat_rate = billing[:vat_rate] if billing.key?(:vat_rate)
end

organization.save!

result.organization = organization
Expand Down
15 changes: 10 additions & 5 deletions spec/requests/api/v1/organizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
let(:update_params) do
{
webhook_url: 'https://test.example',
vat_rate: 20,
country: 'pl',
address_line1: 'address1',
address_line2: 'address2',
Expand All @@ -19,8 +18,11 @@
city: 'test_city',
legal_name: 'test1',
legal_number: '123',
invoice_footer: 'footer',
invoice_grace_period: 3,
billing_configuration: {
invoice_footer: 'footer',
invoice_grace_period: 3,
vat_rate: 20,
},
}
end

Expand All @@ -37,8 +39,11 @@
expect(json[:organization][:name]).to eq(organization.name)
expect(json[:organization][:webhook_url]).to eq(update_params[:webhook_url])
expect(json[:organization][:vat_rate]).to eq(update_params[:vat_rate])
expect(json[:organization][:invoice_footer]).to eq(update_params[:invoice_footer])
expect(json[:organization][:invoice_grace_period]).to eq(update_params[:invoice_grace_period])

billing = json[:organization][:billing_configuration]
expect(billing[:invoice_footer]).to eq('footer')
expect(billing[:invoice_grace_period]).to eq(3)
expect(billing[:vat_rate]).to eq(20)
end
end
end
Expand Down
34 changes: 17 additions & 17 deletions spec/serializers/v1/organization_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
require 'rails_helper'

RSpec.describe ::V1::OrganizationSerializer do
subject(:serializer) { described_class.new(organization, root_name: 'organization') }
subject(:serializer) { described_class.new(org, root_name: 'organization') }

let(:organization) { create(:organization) }
let(:org) { create(:organization) }

it 'serializes the object' do
result = JSON.parse(serializer.to_json)

aggregate_failures do
expect(result['organization']['name']).to eq(organization.name)
expect(result['organization']['created_at']).to eq(organization.created_at.iso8601)
expect(result['organization']['webhook_url']).to eq(organization.webhook_url)
expect(result['organization']['vat_rate']).to eq(organization.vat_rate)
expect(result['organization']['country']).to eq(organization.country)
expect(result['organization']['address_line1']).to eq(organization.address_line1)
expect(result['organization']['address_line2']).to eq(organization.address_line2)
expect(result['organization']['state']).to eq(organization.state)
expect(result['organization']['zipcode']).to eq(organization.zipcode)
expect(result['organization']['email']).to eq(organization.email)
expect(result['organization']['city']).to eq(organization.city)
expect(result['organization']['legal_name']).to eq(organization.legal_name)
expect(result['organization']['legal_number']).to eq(organization.legal_number)
expect(result['organization']['invoice_footer']).to eq(organization.invoice_footer)
expect(result['organization']['invoice_grace_period']).to eq(organization.invoice_grace_period)
expect(result['organization']['name']).to eq(org.name)
expect(result['organization']['created_at']).to eq(org.created_at.iso8601)
expect(result['organization']['webhook_url']).to eq(org.webhook_url)
expect(result['organization']['country']).to eq(org.country)
expect(result['organization']['address_line1']).to eq(org.address_line1)
expect(result['organization']['address_line2']).to eq(org.address_line2)
expect(result['organization']['state']).to eq(org.state)
expect(result['organization']['zipcode']).to eq(org.zipcode)
expect(result['organization']['email']).to eq(org.email)
expect(result['organization']['city']).to eq(org.city)
expect(result['organization']['legal_name']).to eq(org.legal_name)
expect(result['organization']['legal_number']).to eq(org.legal_number)
expect(result['organization']['billing_configuration']['invoice_footer']).to eq(org.invoice_footer)
expect(result['organization']['billing_configuration']['invoice_grace_period']).to eq(org.invoice_grace_period)
expect(result['organization']['billing_configuration']['vat_rate']).to eq(org.vat_rate)
end
end
end
14 changes: 10 additions & 4 deletions spec/services/organizations/update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
let(:update_args) do
{
webhook_url: 'https://test.example',
vat_rate: 20,
country: country,
address_line1: 'address1',
address_line2: 'address2',
Expand All @@ -71,7 +70,11 @@
city: 'test_city',
legal_name: 'test1',
legal_number: '123',
invoice_footer: 'footer',
billing_configuration: {
invoice_footer: 'footer',
invoice_grace_period: 3,
vat_rate: 20,
},
}
end

Expand All @@ -84,8 +87,11 @@
organization_response = result.organization
expect(organization_response.name).to eq(organization.name)
expect(organization_response.webhook_url).to eq(update_args[:webhook_url])
expect(organization_response.vat_rate).to eq(update_args[:vat_rate])
expect(organization_response.invoice_footer).to eq(update_args[:invoice_footer])

billing = update_args[:billing_configuration]
expect(organization_response.invoice_footer).to eq(billing[:invoice_footer])
expect(organization_response.invoice_grace_period).to eq(billing[:invoice_grace_period])
expect(organization_response.vat_rate).to eq(billing[:vat_rate])
end
end

Expand Down

0 comments on commit ee233a5

Please sign in to comment.