Go to BillPlan after setting parent, not to Finish

Stephen Paul Weber created

Change summary

lib/registration.rb       | 10 +++---
test/test_registration.rb | 55 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+), 5 deletions(-)

Detailed changes

lib/registration.rb 🔗

@@ -470,9 +470,9 @@ class Registration
 			def parse(iq)
 				return Activation.for(@customer, nil, @tel).then(&:write) if iq.prev?
 
-				verify(iq.form.field("code")&.value&.to_s).then {
-					Finish.new(@customer, @tel)
-				}.catch_only(InvitesRepo::Invalid, &method(:invalid_code)).then(&:write)
+				verify(iq.form.field("code")&.value&.to_s)
+					.catch_only(InvitesRepo::Invalid, &method(:invalid_code))
+					.then(&:write)
 			end
 
 		protected
@@ -490,9 +490,9 @@ class Registration
 					if parent_customer_id
 						set_parent(parent_customer_id)
 					else
-						InvitesRepo.new(DB, REDIS).claim_code(customer_id, code) do
+						InvitesRepo.new(DB, REDIS).claim_code(customer_id, code) {
 							@customer.activate_plan_starting_now
-						end
+						}.then { Finish.new(@customer, @tel) }
 					end
 				end
 			end

test/test_registration.rb 🔗

@@ -732,6 +732,9 @@ class RegistrationTest < Minitest::Test
 			Command::COMMAND_MANAGER = Minitest::Mock.new
 			Registration::Payment::InviteCode::Finish =
 				Minitest::Mock.new
+			Registration::Payment::InviteCode::BillPlan =
+				Minitest::Mock.new
+
 			def test_write
 				customer = customer(plan_name: "test_usd")
 				Registration::Payment::InviteCode::DB.expect(:transaction, true, [])
@@ -779,6 +782,58 @@ class RegistrationTest < Minitest::Test
 			end
 			em :test_write
 
+			def test_write_parent_code
+				customer = customer(plan_name: "test_usd")
+				Registration::Payment::InviteCode::BillPlan.expect(
+					:new,
+					OpenStruct.new(write: nil)
+				) { |*| true }
+				execute_command do
+					Registration::Payment::InviteCode::REDIS.expect(
+						:hget,
+						EMPromise.resolve("parent_customer"),
+						["jmp_parent_codes", "pabc"]
+					)
+					CustomerPlan::DB.expect(
+						:query,
+						[{ "plan_name" => "test_usd" }],
+						[String, ["parent_customer"]]
+					)
+					CustomerPlan::DB.expect(
+						:exec_defer,
+						EMPromise.resolve(nil),
+						[String, ["test", "test_usd", "parent_customer"]]
+					)
+					Command.execution.customer_repo.expect(
+						:find,
+						customer.with_balance(10000),
+						["test"]
+					)
+					Command::COMMAND_MANAGER.expect(
+						:write,
+						EMPromise.resolve(
+							Blather::Stanza::Iq::Command.new.tap { |iq|
+								iq.form.fields = [{ var: "code", value: "pabc" }]
+							}
+						),
+						[Matching.new do |reply|
+							assert_equal :form, reply.form.type
+							assert_nil reply.form.instructions
+						end]
+					)
+
+					Registration::Payment::InviteCode.new(
+						customer,
+						"+15555550000"
+					).write
+				end
+				assert_mock Command::COMMAND_MANAGER
+				assert_mock Registration::Payment::InviteCode::DB
+				assert_mock Registration::Payment::InviteCode::REDIS
+				assert_mock Registration::Payment::InviteCode::BillPlan
+			end
+			em :test_write_parent_code
+
 			def test_write_bad_code
 				result = execute_command do
 					customer = customer(plan_name: "test_usd")