~singpolyma/sgx-jmp

e2844680cb010bc3a28e3699914046f3853be65f — Christopher Vollick 7 months ago a15d64c
Admin Command Flash

Having a second response here just to show the result, and then have to
skip by it to get back to the menu is dumb.

So instead we just added a thing here so we can tag some info to show up
on the next form, and then go to it.

Much smoother, but it does depend on a change in the adhoc bot which
previously didn't show notes when there was also a form.
3 files changed, 46 insertions(+), 36 deletions(-)

M forms/admin_menu.rb
M forms/customer_picker.rb
M lib/admin_command.rb
M forms/admin_menu.rb => forms/admin_menu.rb +7 -0
@@ 1,6 1,13 @@
form!
title "Menu"

if @notice
	field(
		type: "fixed",
		value: @notice
	)
end

field(
	var: "action",
	type: "list-single",

M forms/customer_picker.rb => forms/customer_picker.rb +7 -0
@@ 6,6 6,13 @@ instructions(
	"information for you"
)

if @notice
	field(
		type: "fixed",
		value: @notice
	)
end

field(
	var: "q",
	type: "text-single",

M lib/admin_command.rb => lib/admin_command.rb +32 -36
@@ 20,29 20,28 @@ class AdminCommand
		if target_customer
			new(target_customer, customer_repo, admin_action_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, admin_action_repo) }
			NoUser.new(customer_repo, admin_action_repo, notice: "Customer Not Found")
		end
	end

	class NoUser
		def initialize(customer_repo, admin_action_repo=AdminActionRepo.new)
	class NoUser < AdminCommand
		def initialize(
			customer_repo,
			admin_action_repo=AdminActionRepo.new,
			notice: nil
		)
			@customer_repo = customer_repo
			@admin_action_repo = admin_action_repo
			@notice = notice
		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, @admin_action_repo)
					.then(&:start)
		def start(command_action=:execute)
			return Command.finish(@notice || "Done") if command_action == :complete

			reply(
				FormTemplate.render("customer_picker", notice: @notice)
			).then { |response|
				new_context(response.form.field("q").value, response.action)
			}
		end
	end


@@ 57,10 56,14 @@ class AdminCommand
		@admin_action_repo = admin_action_repo
	end

	def start
	def start(command_action=:execute)
		@target_customer.admin_info.then { |info|
			reply(info.form)
		}.then { menu_or_done }
			if command_action == :complete
				Command.finish { |iq| iq.command << info.form }
			else
				reply(info.form)
			end
		}.then { |response| menu_or_done(response.action) }
	end

	def reply(form)


@@ 70,10 73,10 @@ class AdminCommand
		}
	end

	def menu_or_done(command_action=:execute)
	def menu_or_done(command_action=:execute, notice: nil)
		return Command.finish("Done") if command_action == :complete

		reply(FormTemplate.render("admin_menu")).then do |response|
		reply(FormTemplate.render("admin_menu", notice: notice)).then do |response|
			if response.form.field("action")
				handle(response.form.field("action").value, response.action)
			end


@@ 82,17 85,19 @@ class AdminCommand

	def handle(action, command_action)
		if respond_to?("action_#{action}")
			send("action_#{action}")
			send("action_#{action}").then do |notice|
				menu_or_done(command_action, notice: notice)
			end
		else
			new_context(action)
		end.then { menu_or_done(command_action) }
		end
	end

	def new_context(q)
	def new_context(q, command_action=:execute)
		CustomerInfoForm.new(@customer_repo)
			.parse_something(q).then do |new_customer|
				AdminCommand.for(new_customer, @customer_repo, @admin_action_repo)
					.then(&:start)
					.then { |ac| ac.start(command_action) }
			end
	end



@@ 117,7 122,7 @@ class AdminCommand
						admin_action_repo.create(performed)
					end
				}
			}.then(method(:success), method(:failure))
			}.then { |action| "Action #{action.id}: #{action}" }
		end

		def reply(form=nil, note_type: nil, note_text: nil)


@@ 128,15 133,6 @@ class AdminCommand
				reply.note_text = note_text if note_text
			}
		end

		def success(action)
			reply(note_type: :info, note_text: "Action #{action.id}: #{action}")
		end

		def failure(err)
			LOG.error "Action Failure", err
			reply(note_type: :error, note_text: "Action Failed: #{err}")
		end
	end

	class Simple


@@ 149,7 145,7 @@ class AdminCommand
				customer_id,
				reply: method(:reply),
				customer_repo: customer_repo
			)
			).then { nil }
		end

		def reply(form=nil, note_type: nil, note_text: nil)