Cleanup Admin Form

Christopher Vollick created

It used to handle the initial failure differently than internal failure.
Now I've moved the outside bits inside, so it can run again when it
encounters an unknown character.

Change summary

lib/admin_command.rb            | 35 ++++++++++++++++++++++++++++++-----
lib/customer_info_form.rb       | 19 ++-----------------
sgx_jmp.rb                      |  9 +--------
test/test_customer_info.rb      |  6 ------
test/test_customer_info_form.rb | 15 +++------------
5 files changed, 36 insertions(+), 48 deletions(-)

Detailed changes

lib/admin_command.rb 🔗

@@ -6,6 +6,35 @@ require_relative "financial_info"
 require_relative "form_template"
 
 class AdminCommand
+	def self.for(target_customer, customer_repo)
+		if target_customer
+			new(target_customer, customer_repo)
+		else
+			Command.reply { |reply|
+				reply.allowed_actions = [:next, :complete]
+				reply.note_type = :error
+				reply.note_text = "Customer Not Found"
+			}.then { NoUser.new(customer_repo) }
+		end
+	end
+
+	class NoUser
+		def initialize(customer_repo)
+			@customer_repo = customer_repo
+		end
+
+		def start
+			Command.reply { |reply|
+				reply.allowed_actions = [:next]
+				reply.command << FormTemplate.render("customer_picker")
+			}.then { |response|
+				CustomerInfoForm.new(@customer_repo).find_customer(response)
+			}.then { |customer|
+				AdminCommand.for(customer, @customer_repo).then(&:start)
+			}
+		end
+	end
+
 	def initialize(target_customer, customer_repo)
 		@target_customer = target_customer
 		@customer_repo = customer_repo
@@ -45,11 +74,7 @@ class AdminCommand
 	def new_context(q)
 		CustomerInfoForm.new(@customer_repo)
 			.parse_something(q).then do |new_customer|
-				if new_customer.respond_to?(:customer_id)
-					AdminCommand.new(new_customer, @customer_repo).start
-				else
-					reply(new_customer.form)
-				end
+				AdminCommand.for(new_customer, @customer_repo).then(&:start)
 			end
 	end
 

lib/customer_info_form.rb 🔗

@@ -13,29 +13,14 @@ class CustomerInfoForm
 		parse_something(response.form.field("q").value)
 	end
 
-	class NoCustomer
-		def form
-			FormTemplate.render("no_customer_info")
-		end
-
-		def admin_info
-			self
-		end
-
-		def registered?
-			false
-		end
-	end
-
 	def parse_something(value)
-		return EMPromise.resolve(NoCustomer.new) if value.to_s.empty?
+		return EMPromise.resolve(nil) if value.to_s.empty?
 
 		EMPromise.all([
 			find_customer_one(value),
 			find_customer_one(Blather::JID.new(value)),
 			find_customer_one(ProxiedJID.proxy(value)),
-			find_customer_by_phone(value),
-			EMPromise.resolve(NoCustomer.new)
+			find_customer_by_phone(value)
 		]).then { |approaches| approaches.compact.first }
 	end
 

sgx_jmp.rb 🔗

@@ -746,14 +746,7 @@ Command.new(
 			bandwidth_tn_repo: EmptyRepo.new(BandwidthTnRepo.new) # No CNAM in admin
 		)
 
-		Command.reply { |reply|
-			reply.allowed_actions = [:next]
-			reply.command << FormTemplate.render("customer_picker")
-		}.then { |response|
-			CustomerInfoForm.new(customer_repo).find_customer(response)
-		}.then do |target_customer|
-			AdminCommand.new(target_customer, customer_repo).start
-		end
+		AdminCommand::NoUser.new(customer_repo).start
 	end
 }.register(self).then(&CommandList.method(:register))
 

test/test_customer_info.rb 🔗

@@ -129,10 +129,4 @@ class CustomerInfoTest < Minitest::Test
 		assert_mock trust_repo
 	end
 	em :test_inactive_admin_info_does_not_crash
-
-	def test_missing_customer_admin_info_does_not_crash
-		cust = CustomerInfoForm::NoCustomer.new
-		assert cust.admin_info.form
-	end
-	em :test_missing_customer_admin_info_does_not_crash
 end

test/test_customer_info_form.rb 🔗

@@ -48,10 +48,7 @@ class CustomerInfoFormTest < Minitest::Test
 	end
 
 	def test_nothing
-		assert_kind_of(
-			CustomerInfoForm::NoCustomer,
-			@info_form.parse_something("").sync
-		)
+		assert_nil(@info_form.parse_something("").sync)
 	end
 	em :test_nothing
 
@@ -101,19 +98,13 @@ class CustomerInfoFormTest < Minitest::Test
 
 	def test_missing_customer_by_phone
 		result = @info_form.parse_something("+17778889999").sync
-		assert_kind_of(
-			CustomerInfoForm::NoCustomer,
-			result
-		)
+		assert_nil(result)
 	end
 	em :test_missing_customer_by_phone
 
 	def test_garbage
 		result = @info_form.parse_something("garbage").sync
-		assert_kind_of(
-			CustomerInfoForm::NoCustomer,
-			result
-		)
+		assert_nil(result)
 	end
 	em :test_garbage
 end