Skip to content

Commit

Permalink
feat(tresholds): Add serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Aug 5, 2024
1 parent 597252b commit a9773ac
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/serializers/v1/plan_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def serialize
}

payload.merge!(charges) if include?(:charges)
payload.merge!(progressive_billing_tresholds) if include?(:progressive_billing_tresholds)
payload.merge!(taxes) if include?(:taxes)
payload.merge!(minimum_commitment) if include?(:minimum_commitment) && model.minimum_commitment

Expand All @@ -40,6 +41,14 @@ def charges
).serialize
end

def progressive_billing_tresholds
::CollectionSerializer.new(
model.progressive_billing_tresholds,
::V1::ProgressiveBillingTresholdSerializer,
collection_name: 'progressive_billing_tresholds'
).serialize
end

def minimum_commitment
{
minimum_commitment: V1::CommitmentSerializer.new(
Expand Down
17 changes: 17 additions & 0 deletions app/serializers/v1/progressive_billing_treshold_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module V1
class ProgressiveBillingTresholdSerializer < ModelSerializer
def serialize
{
lago_id: model.id,
treshold_display_name: model.treshold_display_name,
amount_cents: model.amount_cents,
amount_currency: model.amount_currency,
recurring: model.recurring,
created_at: model.created_at.iso8601,
updated_at: model.updated_at.iso8601
}
end
end
end
31 changes: 29 additions & 2 deletions spec/serializers/v1/plan_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
require 'rails_helper'

RSpec.describe ::V1::PlanSerializer do
subject(:serializer) { described_class.new(plan, root_name: 'plan', includes: %i[charges taxes minimum_commitment]) }
subject(:serializer) do
described_class.new(
plan,
root_name: 'plan',
includes: %i[charges taxes minimum_commitment progressive_billing_tresholds]
)
end

let(:plan) { create(:plan) }
let(:customer) { create(:customer, organization: plan.organization) }
let(:subscription) { create(:subscription, customer:, plan:) }
let(:charge) { create(:standard_charge, plan:) }
let(:progressive_billing_treshold) { create(:progressive_billing_treshold, plan:) }

before { subscription && charge }
before { subscription && charge && progressive_billing_treshold }

context 'when plan has one minimium commitment' do
let(:commitment) { create(:commitment, plan:) }
Expand Down Expand Up @@ -48,6 +55,16 @@
'lago_id' => charge.id
)

expect(result['plan']['progressive_billing_tresholds'].first).to include(
'lago_id' => progressive_billing_treshold.id,
'treshold_display_name' => progressive_billing_treshold.treshold_display_name,
'amount_cents' => progressive_billing_treshold.amount_cents,
'amount_currency' => progressive_billing_treshold.amount_currency,
'recurring' => progressive_billing_treshold.recurring?,
'created_at' => progressive_billing_treshold.created_at.iso8601,
'updated_at' => progressive_billing_treshold.updated_at.iso8601
)

expect(result['plan']['minimum_commitment']).to include(
'lago_id' => commitment.id,
'plan_code' => commitment.plan.code,
Expand Down Expand Up @@ -96,6 +113,16 @@
'lago_id' => charge.id
)

expect(result['plan']['progressive_billing_tresholds'].first).to include(
'lago_id' => progressive_billing_treshold.id,
'treshold_display_name' => progressive_billing_treshold.treshold_display_name,
'amount_cents' => progressive_billing_treshold.amount_cents,
'amount_currency' => progressive_billing_treshold.amount_currency,
'recurring' => progressive_billing_treshold.recurring?,
'created_at' => progressive_billing_treshold.created_at.iso8601,
'updated_at' => progressive_billing_treshold.updated_at.iso8601
)

expect(result['plan']['minimum_commitment']).to be_nil
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ::V1::ProgressiveBillingTresholdSerializer do
subject(:serializer) { described_class.new(progressive_billing_treshold, root_name: 'progressive_billing_treshold') }

let(:progressive_billing_treshold) { create(:progressive_billing_treshold) }

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

aggregate_failures do
expect(result['progressive_billing_treshold']).to include(
'lago_id' => progressive_billing_treshold.id,
'treshold_display_name' => progressive_billing_treshold.treshold_display_name,
'amount_cents' => progressive_billing_treshold.amount_cents,
'amount_currency' => progressive_billing_treshold.amount_currency,
'recurring' => progressive_billing_treshold.recurring?,
'created_at' => progressive_billing_treshold.created_at.iso8601,
'updated_at' => progressive_billing_treshold.updated_at.iso8601
)
end
end
end

0 comments on commit a9773ac

Please sign in to comment.