Skip to content

Commit

Permalink
feat (recurring-transaction-rules): add support for wallet recurring …
Browse files Browse the repository at this point in the history
…transaction rules (#155)
  • Loading branch information
lovrocolic authored Nov 30, 2023
1 parent ef67ba1 commit 3ede87f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
44 changes: 33 additions & 11 deletions lib/lago/api/resources/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,39 @@ def root_name
end

def whitelist_params(params)
{
root_name => {
external_customer_id: params[:external_customer_id],
rate_amount: params[:rate_amount],
name: params[:name],
paid_credits: params[:paid_credits],
granted_credits: params[:granted_credits],
currency: params[:currency],
expiration_at: params[:expiration_at],
}.compact,
}
result_hash = {
external_customer_id: params[:external_customer_id],
rate_amount: params[:rate_amount],
name: params[:name],
paid_credits: params[:paid_credits],
granted_credits: params[:granted_credits],
currency: params[:currency],
expiration_at: params[:expiration_at],
}.compact

recurring_rules = whitelist_recurring_rules(params[:recurring_transaction_rules])
result_hash[:recurring_transaction_rules] = recurring_rules unless recurring_rules.empty?

{ root_name => result_hash }
end

def whitelist_recurring_rules(rules)
processed_rules = []

(rules || []).each do |r|
result = (r || {}).slice(
:lago_id,
:paid_credits,
:granted_credits,
:rule_type,
:threshold_credits,
:interval,
)

processed_rules << result unless result.empty?
end

processed_rules
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@
rate_amount { '1' }
paid_credits { '100' }
granted_credits { '100' }
recurring_transaction_rules do
[
{
rule_type: 'interval',
interval: 'monthly',
paid_credits: '105',
granted_credits: '105',
},
]
end
end
end
3 changes: 3 additions & 0 deletions spec/lago/api/resources/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'balance_cents' => 10_000,
'rate_amount' => factory_wallet.rate_amount,
'created_at' => '2022-04-29T08:59:51Z',
'recurring_transaction_rules' => factory_wallet.recurring_transaction_rules,
}
}.to_json
end
Expand Down Expand Up @@ -48,6 +49,8 @@

expect(wallet.lago_id).to eq('this-is-lago-id')
expect(wallet.name).to eq(factory_wallet.name)
expect(wallet.recurring_transaction_rules.first.rule_type).to eq('interval')
expect(wallet.recurring_transaction_rules.first.interval).to eq('monthly')
end
end

Expand Down

0 comments on commit 3ede87f

Please sign in to comment.