From a3f4e2701ae66bda07c998bbb1391ebbb12fb755 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 18 May 2021 13:40:50 -0500 Subject: [PATCH] Create customer_id if it does not exist before we start registration --- 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(-) diff --git a/lib/customer.rb b/lib/customer.rb index 9f087de..d558488 100644 --- a/lib/customer.rb +++ b/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 diff --git a/lib/registration.rb b/lib/registration.rb index 8935505..c22add5 100644 --- a/lib/registration.rb +++ b/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 diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 2d825c9..810153e 100644 --- a/sgx_jmp.rb +++ b/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, diff --git a/test/test_customer.rb b/test/test_customer.rb index 4225b58..400c85d 100644 --- a/test/test_customer.rb +++ b/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 diff --git a/test/test_registration.rb b/test/test_registration.rb index 7117111..89376e4 100644 --- a/test/test_registration.rb +++ b/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 -- 2.38.5