From 54189f8b387fe0e4a44a763c14300ac174698bdc Mon Sep 17 00:00:00 2001 From: Christopher Vollick <0@psycoti.ca> Date: Tue, 10 May 2022 13:45:23 -0400 Subject: [PATCH] Cleanup Admin Form 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. --- 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(-) diff --git a/lib/admin_command.rb b/lib/admin_command.rb index ac7659b1a863583a52d100ecae49f2f5937e5939..1ce24ac7ae54ca11fca5bf51e1726113bedca107 100644 --- a/lib/admin_command.rb +++ b/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 diff --git a/lib/customer_info_form.rb b/lib/customer_info_form.rb index b4db45e16d3fb658694e60e1d7f526316b2091d3..0b3ca108caa5c15d29eca318ebc038b26ec9e9a4 100644 --- a/lib/customer_info_form.rb +++ b/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 diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 2c6dcd9db52dc3c0e91f9212a111aac51553c508..4233fc0df807c13543fcebf7e3b5c92fb9ea13df 100644 --- a/sgx_jmp.rb +++ b/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)) diff --git a/test/test_customer_info.rb b/test/test_customer_info.rb index 19e2454043001e958927285981e0b5b1da48d18c..98144d4a9b420f8cb6a5baaf8d6bbcaebeea39f3 100644 --- a/test/test_customer_info.rb +++ b/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 diff --git a/test/test_customer_info_form.rb b/test/test_customer_info_form.rb index 8d1d0cf2a4de3d2f686044a315ccbbdf2e6e419e..7d19e1ab617a31dc795b09f3401b9e595e372b17 100644 --- a/test/test_customer_info_form.rb +++ b/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