Merge branch 'preserve-parent'

Stephen Paul Weber created

* preserve-parent:
  Preserve previous parent id when re-activating plan

Change summary

lib/customer_plan.rb  | 13 ++++++++-----
test/test_customer.rb | 36 ++++++++++++++++++++++++++++++++++--
test/test_helper.rb   |  7 ++++---
3 files changed, 46 insertions(+), 10 deletions(-)

Detailed changes

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])

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

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 = {