From 3ede87fcac97f55902716695a20abb1c96c2a212 Mon Sep 17 00:00:00 2001 From: LovroColic Date: Thu, 30 Nov 2023 11:11:44 +0100 Subject: [PATCH] feat (recurring-transaction-rules): add support for wallet recurring transaction rules (#155) --- lib/lago/api/resources/wallet.rb | 44 +++++++++++++++++++------- spec/factories/wallet.rb | 10 ++++++ spec/lago/api/resources/wallet_spec.rb | 3 ++ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/lib/lago/api/resources/wallet.rb b/lib/lago/api/resources/wallet.rb index ab21ccc..2d1ab95 100644 --- a/lib/lago/api/resources/wallet.rb +++ b/lib/lago/api/resources/wallet.rb @@ -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 diff --git a/spec/factories/wallet.rb b/spec/factories/wallet.rb index 75d3434..5bdbc37 100644 --- a/spec/factories/wallet.rb +++ b/spec/factories/wallet.rb @@ -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 diff --git a/spec/lago/api/resources/wallet_spec.rb b/spec/lago/api/resources/wallet_spec.rb index 0c6f806..e54d9fe 100644 --- a/spec/lago/api/resources/wallet_spec.rb +++ b/spec/lago/api/resources/wallet_spec.rb @@ -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 @@ -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