From e33ce01ecd9ddfc6c304be83c92f2b2c3a4ffd9c Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 13 Sep 2022 11:44:54 -0500 Subject: [PATCH] Preserve previous parent id when re-activating plan --- lib/customer_plan.rb | 13 ++++++++----- test/test_customer.rb | 36 ++++++++++++++++++++++++++++++++++-- test/test_helper.rb | 7 ++++--- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/lib/customer_plan.rb b/lib/customer_plan.rb index b7cb59d..4891826 100644 --- a/lib/customer_plan.rb +++ b/lib/customer_plan.rb @@ -22,7 +22,7 @@ class CustomerPlan self.for( customer_id, **kwargs.slice( - :plan_name, :expires_at, + :plan_name, :expires_at, :parent_customer_id, :auto_top_up_amount, :monthly_overage_limit ) ) @@ -33,13 +33,15 @@ class CustomerPlan plan: nil, expires_at: Time.now, auto_top_up_amount: 0, - monthly_overage_limit: 0 + monthly_overage_limit: 0, + parent_customer_id: nil ) @customer_id = customer_id @plan = plan || OpenStruct.new @expires_at = expires_at @auto_top_up_amount = auto_top_up_amount || 0 @monthly_overage_limit = monthly_overage_limit || 0 + @parent_customer_id = parent_customer_id end def active? @@ -82,11 +84,12 @@ class CustomerPlan end def activate_plan_starting_now - activated = DB.exec(<<~SQL, [@customer_id, plan_name]).cmd_tuples.positive? - INSERT INTO plan_log (customer_id, plan_name, date_range) - VALUES ($1, $2, tsrange(LOCALTIMESTAMP, LOCALTIMESTAMP + '1 month')) + activated = DB.exec(<<~SQL, [@customer_id, plan_name, @parent_customer_id]) + INSERT INTO plan_log (customer_id, plan_name, date_range, parent_customer_id) + VALUES ($1, $2, tsrange(LOCALTIMESTAMP, LOCALTIMESTAMP + '1 month'), $3) ON CONFLICT DO NOTHING SQL + activated = activated.cmd_tuples.positive? return false unless activated DB.exec(<<~SQL, [@customer_id]) diff --git a/test/test_customer.rb b/test/test_customer.rb index 7ca8bee..08f351f 100644 --- a/test/test_customer.rb +++ b/test/test_customer.rb @@ -36,7 +36,7 @@ class CustomerTest < Minitest::Test CustomerPlan::DB.expect( :exec, OpenStruct.new(cmd_tuples: 1), - [String, ["test", "test_usd"]] + [String, ["test", "test_usd", nil]] ) CustomerPlan::DB.expect( :exec, @@ -48,6 +48,38 @@ class CustomerTest < Minitest::Test end em :test_bill_plan_activate + def test_bill_plan_reactivate_child + CustomerPlan::DB.expect(:transaction, nil) do |&block| + block.call + true + end + CustomerPlan::DB.expect( + :exec, + nil, + [ + String, + Matching.new do |params| + params[0] == "test" && + params[1].is_a?(String) && + BigDecimal(-1) == params[2] + end + ] + ) + CustomerPlan::DB.expect( + :exec, + OpenStruct.new(cmd_tuples: 1), + [String, ["test", "test_usd", "parent"]] + ) + CustomerPlan::DB.expect( + :exec, + OpenStruct.new(cmd_tuples: 0), + [String, ["test"]] + ) + customer(plan_name: "test_usd", parent_customer_id: "parent").bill_plan.sync + CustomerPlan::DB.verify + end + em :test_bill_plan_reactivate_child + def test_bill_plan_update CustomerPlan::DB.expect(:transaction, nil) do |&block| block.call @@ -68,7 +100,7 @@ class CustomerTest < Minitest::Test CustomerPlan::DB.expect( :exec, OpenStruct.new(cmd_tuples: 0), - [String, ["test", "test_usd"]] + [String, ["test", "test_usd", nil]] ) CustomerPlan::DB.expect(:exec, nil, [String, ["test"]]) customer(plan_name: "test_usd").bill_plan.sync diff --git a/test/test_helper.rb b/test/test_helper.rb index c774145..470810d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -47,13 +47,14 @@ def customer( auto_top_up_amount: 0, **kwargs ) - plan = CustomerPlan.for( + Customer.extract( customer_id, + jid, plan_name: plan_name, expires_at: expires_at, - auto_top_up_amount: auto_top_up_amount + auto_top_up_amount: auto_top_up_amount, + **kwargs ) - Customer.new(customer_id, jid, plan: plan, **kwargs) end CONFIG = { -- 2.38.4