Skip to content

Commit

Permalink
Merge pull request #840 from hooktstudios/improvement/subscription-fi…
Browse files Browse the repository at this point in the history
…lter-current-period

Allow subscription filter on current_period values
  • Loading branch information
alexmamonchik committed Oct 24, 2023
2 parents f2bfb3d + 1d76c10 commit bd095ab
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ def total_items_amount(items)
end
total
end

def filter_by_timestamp(subscriptions, field:, value:)
if value.is_a?(Hash)
operator_mapping = { gt: :>, gte: :>=, lt: :<, lte: :<= }
subscriptions.filter do |sub|
sub[field].public_send(operator_mapping[value.keys[0]], value.values[0])
end
else
subscriptions.filter do |sub|
sub[field] == value
end
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/stripe_mock/request_handlers/subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ def retrieve_subscriptions(route, method_url, params, headers)
else
subs = subs.filter {|subscription| subscription[:status] == params[:status]}
end
if params[:current_period_end]
subs = filter_by_timestamp(subs, field: :current_period_end, value: params[:current_period_end])
end
if params[:current_period_start]
subs = filter_by_timestamp(subs, field: :current_period_start, value: params[:current_period_start])
end

Data.mock_list_object(subs, params)
end
Expand Down
32 changes: 32 additions & 0 deletions spec/shared_stripe_examples/subscription_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,38 @@
expect(list.data).to be_empty
expect(list.data.length).to eq(0)
end

it "filters out subscriptions based on their current_period", live: true do
price = stripe_helper.create_price(recurring: { interval: 'month' })
price2 = stripe_helper.create_price(recurring: { interval: 'year' })

subscription1 = Stripe::Subscription.create(
customer: Stripe::Customer.create(source: gen_card_tk).id,
items: [{ price: price.id, quantity: 1 }]
)
subscription2 = Stripe::Subscription.create(
customer: Stripe::Customer.create(source: gen_card_tk).id,
items: [{ price: price2.id, quantity: 1 }]
)

list = Stripe::Subscription.list({ current_period_end: { gt: subscription1.current_period_end }})
expect(list.data).to contain_exactly(subscription2)

list = Stripe::Subscription.list({ current_period_end: { gte: subscription1.current_period_end }})
expect(list.data).to contain_exactly(subscription1, subscription2)

list = Stripe::Subscription.list({ current_period_end: { lt: subscription1.current_period_end }})
expect(list.data).to be_empty

list = Stripe::Subscription.list({ current_period_end: { lte: subscription1.current_period_end }})
expect(list.data).to contain_exactly(subscription1)

list = Stripe::Subscription.list({ current_period_start: subscription1.current_period_start })
expect(list.data).to contain_exactly(subscription1, subscription2)

list = Stripe::Subscription.list({ current_period_end: subscription2.current_period_end })
expect(list.data).to contain_exactly(subscription2)
end
end

describe "metadata" do
Expand Down

0 comments on commit bd095ab

Please sign in to comment.