Skip to content

Commit

Permalink
feat: Expose billing periods in invoice serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Jan 17, 2025
1 parent b28b40d commit f9ee2d1
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def render_invoice(invoice)
json: ::V1::InvoiceSerializer.new(
invoice,
root_name: 'invoice',
includes: %i[customer integration_customers subscriptions fees credits metadata applied_taxes error_details applied_invoice_custom_sections]
includes: %i[customer integration_customers billing_periods subscriptions fees credits metadata applied_taxes error_details applied_invoice_custom_sections]
)
)
end
Expand Down
9 changes: 9 additions & 0 deletions app/serializers/v1/invoice_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def serialize

payload.merge!(customer) if include?(:customer)
payload.merge!(subscriptions) if include?(:subscriptions)
payload.merge!(billing_periods) if include?(:billing_periods)
payload.merge!(fees) if include?(:fees)
payload.merge!(credits) if include?(:credits)
payload.merge!(metadata) if include?(:metadata)
Expand Down Expand Up @@ -121,5 +122,13 @@ def applied_invoice_custom_sections
collection_name: 'applied_invoice_custom_sections'
).serialize
end

def billing_periods
::CollectionSerializer.new(
model.invoice_subscriptions,
::V1::Invoices::BillingPeriodSerializer,
collection_name: 'billing_periods'
).serialize
end
end
end
20 changes: 20 additions & 0 deletions app/serializers/v1/invoices/billing_period_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module V1
module Invoices
class BillingPeriodSerializer < ModelSerializer
def serialize
{
lago_subscription_id: model.subscription_id,
external_subscription_id: model.subscription&.external_id,
lago_plan_id: model.subscription&.plan_id,
subscription_from_datetime: model.from_datetime.iso8601,
subscription_to_datetime: model.to_datetime.iso8601,
charges_from_datetime: model.charges_from_datetime.iso8601,
charges_to_datetime: model.charges_to_datetime.iso8601,
invoicing_reason: model.invoicing_reason
}
end
end
end
end
2 changes: 1 addition & 1 deletion app/services/webhooks/invoices/created_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def object_serializer
::V1::InvoiceSerializer.new(
object,
root_name: 'invoice',
includes: %i[customer subscriptions fees credits applied_taxes applied_invoice_custom_sections]
includes: %i[customer subscriptions billing_periods fees credits applied_taxes applied_invoice_custom_sections]
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/webhooks/invoices/drafted_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def object_serializer
::V1::InvoiceSerializer.new(
object,
root_name: 'invoice',
includes: %i[customer subscriptions fees credits applied_taxes error_details]
includes: %i[customer subscriptions billing_periods fees credits applied_taxes error_details]
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/webhooks/invoices/resynced_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def object_serializer
::V1::InvoiceSerializer.new(
object,
root_name: 'invoice',
includes: %i[customer integration_customers subscriptions fees credits applied_taxes]
includes: %i[customer billing_periods integration_customers subscriptions fees credits applied_taxes]
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/webhooks/invoices/voided_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def object_serializer
::V1::InvoiceSerializer.new(
object,
root_name: 'invoice',
includes: %i[customer subscriptions fees credits applied_taxes]
includes: %i[customer billing_periods subscriptions fees credits applied_taxes]
)
end

Expand Down
9 changes: 9 additions & 0 deletions spec/factories/invoice_subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@
invoice

recurring { false }

trait :boundaries do
timestamp { Time.current }

from_datetime { timestamp.beginning_of_month }
to_datetime { timestamp.end_of_month }
charges_from_datetime { from_datetime - 1.month }
charges_to_datetime { to_datetime.end_of_month }
end
end
end
17 changes: 16 additions & 1 deletion spec/serializers/v1/invoice_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
require "rails_helper"

RSpec.describe ::V1::InvoiceSerializer do
subject(:serializer) { described_class.new(invoice, root_name: "invoice", includes: %i[metadata error_details]) }
subject(:serializer) { described_class.new(invoice, root_name: "invoice", includes:) }

let(:includes) { %i[metadata error_details] }

let(:invoice) { create(:invoice) }
let(:metadata) { create(:invoice_metadata, invoice:) }
Expand Down Expand Up @@ -75,4 +77,17 @@
expect(result["invoice"]["applied_usage_thresholds"].count).to eq(1)
end
end

context 'when including billing periods' do
let(:includes) { %i[billing_periods] }
let(:invoice_subscription) { create(:invoice_subscription, :boundaries, invoice:) }

before { invoice_subscription }

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

expect(result['invoice']['billing_periods']).to be_present
end
end
end
24 changes: 24 additions & 0 deletions spec/serializers/v1/invoices/billing_period_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ::V1::Invoices::BillingPeriodSerializer do
subject(:serializer) { described_class.new(invoice_subscription, root_name: 'billing_period') }

let(:invoice_subscription) { build(:invoice_subscription, :boundaries) }

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

aggregate_failures do
expect(result['billing_period']['lago_subscription_id']).to eq(invoice_subscription.subscription_id)
expect(result['billing_period']['external_subscription_id']).to eq(invoice_subscription.subscription.external_id)
expect(result['billing_period']['lago_plan_id']).to eq(invoice_subscription.subscription.plan_id)
expect(result['billing_period']['subscription_from_datetime']).to eq(invoice_subscription.from_datetime.iso8601)
expect(result['billing_period']['subscription_to_datetime']).to eq(invoice_subscription.to_datetime.iso8601)
expect(result['billing_period']['charges_from_datetime']).to eq(invoice_subscription.charges_from_datetime.iso8601)
expect(result['billing_period']['charges_to_datetime']).to eq(invoice_subscription.charges_to_datetime.iso8601)
expect(result['billing_period']['invoicing_reason']).to eq(invoice_subscription.invoicing_reason)
end
end
end

0 comments on commit f9ee2d1

Please sign in to comment.