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 self.for(target_customer, customer_repo)
10 if target_customer
11 new(target_customer, customer_repo)
12 else
13 Command.reply { |reply|
14 reply.allowed_actions = [:next, :complete]
15 reply.note_type = :error
16 reply.note_text = "Customer Not Found"
17 }.then { NoUser.new(customer_repo) }
18 end
19 end
20
21 class NoUser
22 def initialize(customer_repo)
23 @customer_repo = customer_repo
24 end
25
26 def start
27 Command.reply { |reply|
28 reply.allowed_actions = [:next]
29 reply.command << FormTemplate.render("customer_picker")
30 }.then { |response|
31 CustomerInfoForm.new(@customer_repo).find_customer(response)
32 }.then { |customer|
33 AdminCommand.for(customer, @customer_repo).then(&:start)
34 }
35 end
36 end
37
38 def initialize(target_customer, customer_repo)
39 @target_customer = target_customer
40 @customer_repo = customer_repo
41 end
42
43 def start
44 @target_customer.admin_info.then { |info|
45 reply(info.form)
46 }.then { menu_or_done }
47 end
48
49 def reply(form)
50 Command.reply { |reply|
51 reply.allowed_actions = [:next, :complete]
52 reply.command << form
53 }
54 end
55
56 def menu_or_done(command_action=:execute)
57 return Command.finish("Done") if command_action == :complete
58
59 reply(FormTemplate.render("admin_menu")).then do |response|
60 if response.form.field("action")
61 handle(response.form.field("action").value, response.action)
62 end
63 end
64 end
65
66 def handle(action, command_action)
67 if respond_to?("action_#{action}")
68 send("action_#{action}")
69 else
70 new_context(action)
71 end.then { menu_or_done(command_action) }
72 end
73
74 def new_context(q)
75 CustomerInfoForm.new(@customer_repo)
76 .parse_something(q).then do |new_customer|
77 AdminCommand.for(new_customer, @customer_repo).then(&:start)
78 end
79 end
80
81 def action_info
82 # Refresh the data
83 new_context(@target_customer.customer_id)
84 end
85
86 def action_financial
87 AdminFinancialInfo.for(@target_customer).then do |financial_info|
88 reply(FormTemplate.render(
89 "admin_financial_info",
90 info: financial_info
91 )).then {
92 pay_methods(financial_info)
93 }.then {
94 transactions(financial_info)
95 }
96 end
97 end
98
99 def action_bill_plan
100 BillPlanCommand.for(@target_customer).call
101 end
102
103 def notify_customer(body)
104 m = Blather::Stanza::Message.new
105 m.from = CONFIG[:notify_from]
106 m.body = body
107 @target_customer.stanza_to(m)
108 end
109
110 def action_cancel_account
111 notify_customer("Your JMP account has been cancelled.").then {
112 EMPromise.all([
113 @target_customer.stanza_to(
114 Blather::Stanza::Iq::IBR.new(:set).tap(&:remove!)
115 ),
116 @target_customer.deregister!,
117 @customer_repo.disconnect_tel(@target_customer)
118 ])
119 }
120 end
121
122 def pay_methods(financial_info)
123 reply(FormTemplate.render(
124 "admin_payment_methods",
125 **financial_info.to_h
126 ))
127 end
128
129 def transactions(financial_info)
130 reply(FormTemplate.render(
131 "admin_transaction_list",
132 transactions: financial_info.transactions
133 ))
134 end
135end