Create customer_id if it does not exist before we start registration

Stephen Paul Weber created

Change summary

lib/customer.rb           | 13 +++++++++++++
lib/registration.rb       |  9 +++------
sgx_jmp.rb                |  2 +-
test/test_customer.rb     | 21 +++++++++++++++++++++
test/test_registration.rb |  7 -------
5 files changed, 38 insertions(+), 14 deletions(-)

Detailed changes

lib/customer.rb 🔗

@@ -27,6 +27,19 @@ class Customer
 		end
 	end
 
+	def self.create(jid)
+		BRAINTREE.customer.create.then do |result|
+			raise "Braintree customer create failed" unless result.success?
+			cid = result.customer.id
+			REDIS.msetnx(
+				"jmp_customer_id-#{jid}", cid, "jmp_customer_jid-#{cid}", jid
+			).then do |redis_result|
+				raise "Saving new customer to redis failed" unless redis_result == 1
+				new(cid)
+			end
+		end
+	end
+
 	extend Forwardable
 
 	attr_reader :customer_id, :balance

lib/registration.rb 🔗

@@ -6,7 +6,7 @@ require_relative "./oob"
 
 class Registration
 	def self.for(iq, customer, web_register_manager)
-		EMPromise.resolve(customer&.registered?).then do |registered|
+		customer.registered?.then do |registered|
 			if registered
 				Registered.new(iq, registered.phone)
 			else
@@ -36,13 +36,10 @@ class Registration
 
 	class Activation
 		def self.for(iq, customer, tel)
-			if customer&.active?
+			if customer.active?
 				Finish.new(iq, customer, tel)
-			elsif customer
-				EMPromise.resolve(new(iq, customer, tel))
 			else
-				# Create customer_id
-				raise "TODO"
+				EMPromise.resolve(new(iq, customer, tel))
 			end
 		end
 

sgx_jmp.rb 🔗

@@ -177,7 +177,7 @@ end
 
 command :execute?, node: "jabber:iq:register", sessionid: nil do |iq|
 	Customer.for_jid(iq.from.stripped).catch {
-		nil
+		Customer.create(iq.from.stripped)
 	}.then { |customer|
 		Registration.for(
 			iq,

test/test_customer.rb 🔗

@@ -3,6 +3,7 @@
 require "test_helper"
 require "customer"
 
+Customer::BRAINTREE = Minitest::Mock.new
 Customer::REDIS = Minitest::Mock.new
 Customer::DB = Minitest::Mock.new
 CustomerPlan::DB = Minitest::Mock.new
@@ -49,6 +50,26 @@ class CustomerTest < Minitest::Test
 	end
 	em :test_for_customer_id_not_found
 
+	def test_create
+		braintree_customer = Minitest::Mock.new
+		Customer::BRAINTREE.expect(:customer, braintree_customer)
+		braintree_customer.expect(:create, EMPromise.resolve(
+			OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
+		))
+		Customer::REDIS.expect(
+			:msetnx,
+			EMPromise.resolve(1),
+			[
+				"jmp_customer_id-test@example.com", "test",
+				"jmp_customer_jid-test", "test@example.com"
+			]
+		)
+		assert_kind_of Customer, Customer.create("test@example.com").sync
+		braintree_customer.verify
+		Customer::REDIS.verify
+	end
+	em :test_create
+
 	def test_bill_plan_activate
 		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 			block.call

test/test_registration.rb 🔗

@@ -54,13 +54,6 @@ class RegistrationTest < Minitest::Test
 	end
 	em :test_for_not_activated_with_customer_id
 
-	def test_for_not_activated_without_customer_id
-		skip "customer_id creation not implemented yet"
-		iq = Blather::Stanza::Iq::Command.new
-		Registration.for(iq, nil, Minitest::Mock.new).sync
-	end
-	em :test_for_not_activated_without_customer_id
-
 	class ActivationTest < Minitest::Test
 		Registration::Activation::COMMAND_MANAGER = Minitest::Mock.new
 		def setup