admin_command.rb

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