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			handle(response.form.field("action").value)
26		end
27	end
28
29	def handle(action)
30		if respond_to?("action_#{action}")
31			send("action_#{action}")
32		else
33			new_context(action)
34		end.then { menu }
35	end
36
37	def new_context(q)
38		CustomerInfoForm.new.parse_something(q).then do |new_customer|
39			if new_customer.respond_to?(:customer_id)
40				AdminCommand.new(new_customer).start
41			else
42				reply(new_customer.form)
43			end
44		end
45	end
46
47	def action_info
48		@target_customer.admin_info.then do |info|
49			reply(info.form)
50		end
51	end
52
53	def action_financial
54		AdminFinancialInfo.for(@target_customer).then do |financial_info|
55			reply(FormTemplate.render(
56				"admin_financial_info",
57				info: financial_info
58			)).then {
59				pay_methods(financial_info)
60			}.then {
61				transactions(financial_info)
62			}
63		end
64	end
65
66	def pay_methods(financial_info)
67		reply(FormTemplate.render(
68			"admin_payment_methods",
69			**financial_info.to_h
70		))
71	end
72
73	def transactions(financial_info)
74		reply(FormTemplate.render(
75			"admin_transaction_list",
76			transactions: financial_info.transactions
77		))
78	end
79end