Skip to content

Commit

Permalink
fix(dates-service): Fix terminated dates service for min commitment c…
Browse files Browse the repository at this point in the history
…alculations (#2260)

## Context

Minimum commitment calculation was using incorrect dates service in when
refreshing draft invoices for terminated subscriptions.

## Description

Fix terminated dates service to be able to skip calling
`matching_invoice_subscription?` when creating the dates service.
  • Loading branch information
ivannovosad authored Jul 9, 2024
1 parent c34a3cc commit bc7b5af
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/services/commitments/dates_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def call
Subscriptions::TerminatedDatesService.new(
subscription: invoice_subscription.subscription,
invoice: invoice_subscription.invoice,
date_service: ds
date_service: ds,
match_invoice_subscription: invoice_subscription.subscription.plan.pay_in_advance?
).call
end

Expand Down
7 changes: 5 additions & 2 deletions app/services/subscriptions/terminated_dates_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

module Subscriptions
class TerminatedDatesService
def initialize(subscription:, invoice:, date_service:)
def initialize(subscription:, invoice:, date_service:, match_invoice_subscription: true)
@subscription = subscription
@timestamp = invoice.invoice_subscriptions.first&.timestamp
@date_service = date_service
@match_invoice_subscription = match_invoice_subscription
end

def call
Expand All @@ -26,12 +27,14 @@ def call
# We should calculate boundaries as if subscription was not terminated
new_dates_service = Subscriptions::DatesService.new_instance(duplicate, timestamp, current_usage: false)

return new_dates_service unless match_invoice_subscription

matching_invoice_subscription?(subscription, new_dates_service) ? date_service : new_dates_service
end

private

attr_reader :subscription, :timestamp, :date_service
attr_reader :subscription, :timestamp, :date_service, :match_invoice_subscription

def matching_invoice_subscription?(subscription, date_service)
base_query = InvoiceSubscription
Expand Down

0 comments on commit bc7b5af

Please sign in to comment.