admin_command.rb

 1# frozen_string_literal: true
 2
 3require_relative "customer_info_form"
 4require_relative "financial_info"
 5require_relative "form_template"
 6
 7class AdminCommand
 8	def initialize(target_customer)
 9		@target_customer = target_customer
10	end
11
12	def start
13		action_info.then { menu }
14	end
15
16	def reply(form)
17		Command.reply { |reply|
18			reply.allowed_actions = [:next]
19			reply.command << form
20		}
21	end
22
23	def menu
24		reply(FormTemplate.render("admin_menu")).then do |response|
25			if response.form.field("action")
26				handle(response.form.field("action").value)
27			end
28		end
29	end
30
31	def handle(action)
32		if respond_to?("action_#{action}")
33			send("action_#{action}")
34		else
35			new_context(action)
36		end.then { menu }
37	end
38
39	def new_context(q)
40		CustomerInfoForm.new.parse_something(q).then do |new_customer|
41			if new_customer.respond_to?(:customer_id)
42				AdminCommand.new(new_customer).start
43			else
44				reply(new_customer.form)
45			end
46		end
47	end
48
49	def action_info
50		@target_customer.admin_info.then do |info|
51			reply(info.form)
52		end
53	end
54
55	def action_financial
56		AdminFinancialInfo.for(@target_customer).then do |financial_info|
57			reply(FormTemplate.render(
58				"admin_financial_info",
59				info: financial_info
60			)).then {
61				pay_methods(financial_info)
62			}.then {
63				transactions(financial_info)
64			}
65		end
66	end
67
68	def pay_methods(financial_info)
69		reply(FormTemplate.render(
70			"admin_payment_methods",
71			**financial_info.to_h
72		))
73	end
74
75	def transactions(financial_info)
76		reply(FormTemplate.render(
77			"admin_transaction_list",
78			transactions: financial_info.transactions
79		))
80	end
81end