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_or_done }
15 end
16
17 def reply(form)
18 Command.reply { |reply|
19 reply.allowed_actions = [:next, :complete]
20 reply.command << form
21 }
22 end
23
24 def menu_or_done(command_action=:execute)
25 return Command.finish("Done") if command_action == :complete
26
27 reply(FormTemplate.render("admin_menu")).then do |response|
28 if response.form.field("action")
29 handle(response.form.field("action").value, response.action)
30 end
31 end
32 end
33
34 def handle(action, command_action)
35 if respond_to?("action_#{action}")
36 send("action_#{action}")
37 else
38 new_context(action)
39 end.then { menu_or_done(command_action) }
40 end
41
42 def new_context(q)
43 CustomerInfoForm.new.parse_something(q).then do |new_customer|
44 if new_customer.respond_to?(:customer_id)
45 AdminCommand.new(new_customer).start
46 else
47 reply(new_customer.form)
48 end
49 end
50 end
51
52 def action_info
53 @target_customer.admin_info.then do |info|
54 reply(info.form)
55 end
56 end
57
58 def action_financial
59 AdminFinancialInfo.for(@target_customer).then do |financial_info|
60 reply(FormTemplate.render(
61 "admin_financial_info",
62 info: financial_info
63 )).then {
64 pay_methods(financial_info)
65 }.then {
66 transactions(financial_info)
67 }
68 end
69 end
70
71 def action_bill_plan
72 BillPlanCommand.for(@target_customer).call
73 end
74
75 def pay_methods(financial_info)
76 reply(FormTemplate.render(
77 "admin_payment_methods",
78 **financial_info.to_h
79 ))
80 end
81
82 def transactions(financial_info)
83 reply(FormTemplate.render(
84 "admin_transaction_list",
85 transactions: financial_info.transactions
86 ))
87 end
88end