1# frozen_string_literal: true
2
3require_relative "admin_action_repo"
4require_relative "admin_actions/add_invites"
5require_relative "admin_actions/add_transaction"
6require_relative "admin_actions/cancel"
7require_relative "admin_actions/financial"
8require_relative "admin_actions/launch_snikket"
9require_relative "admin_actions/number_change"
10require_relative "admin_actions/register_with_sgx"
11require_relative "admin_actions/reset_declines"
12require_relative "admin_actions/set_trust_level"
13require_relative "bill_plan_command"
14require_relative "customer_info_form"
15require_relative "financial_info"
16require_relative "form_template"
17
18class AdminCommand
19 def self.for(
20 target_customer,
21 customer_repo,
22 admin_action_repo=AdminActionRepo.new,
23 snikket_repo=Snikket::Repo.new
24 )
25 if target_customer
26 new(target_customer, customer_repo, admin_action_repo, snikket_repo)
27 else
28 NoUser.new(customer_repo, admin_action_repo, notice: "Customer Not Found")
29 end
30 end
31
32 class NoUser < AdminCommand
33 def initialize(
34 customer_repo,
35 admin_action_repo=AdminActionRepo.new,
36 snikket_repo=Snikket::Repo.new,
37 notice: nil
38 )
39 @customer_repo = customer_repo
40 @admin_action_repo = admin_action_repo
41 @snikket_repo = snikket_repo
42 @notice = notice
43 end
44
45 def start(command_action=:execute)
46 return Command.finish(@notice || "Done") if command_action == :complete
47
48 reply(
49 FormTemplate.render("customer_picker", notice: @notice)
50 ).then { |response|
51 new_context(response.form.field("q").value, response.action)
52 }
53 end
54 end
55
56 def initialize(
57 target_customer,
58 customer_repo,
59 admin_action_repo=AdminActionRepo.new,
60 snikket_repo=Snikket::Repo.new
61 )
62 @target_customer = target_customer
63 @customer_repo = customer_repo || (raise "No cutomer repo")
64 @admin_action_repo = admin_action_repo || (raise "No admin action repo")
65 @snikket_repo = snikket_repo || (raise "No snikket repo")
66 end
67
68 def start(command_action=:execute)
69 AdminInfo.for(@target_customer).then { |info|
70 if command_action == :complete
71 Command.finish { |iq| iq.command << info.form }
72 else
73 reply(info.form)
74 end
75 }.then { |response| menu_or_done(response.action) }
76 end
77
78 def reply(form)
79 Command.reply { |reply|
80 reply.allowed_actions = [:next, :complete]
81 reply.command << form
82 }
83 end
84
85 def menu_or_done(command_action=:execute, notice: nil)
86 return Command.finish("Done") if command_action == :complete
87
88 reply(FormTemplate.render("admin_menu", notice: notice)).then do |response|
89 if response.form.field("action")
90 handle(response.form.field("action").value, response.action)
91 end
92 end
93 end
94
95 def handle(action, command_action)
96 if respond_to?("action_#{action}")
97 send("action_#{action}").then do |notice|
98 menu_or_done(command_action, notice: notice)
99 end
100 else
101 new_context(action)
102 end
103 end
104
105 def new_context(q, command_action=:execute)
106 CustomerInfoForm.new(@customer_repo)
107 .parse_something(q).then do |new_customer|
108 AdminCommand.for(
109 new_customer,
110 @customer_repo,
111 @admin_action_repo,
112 @snikket_repo
113 ).then { |ac| ac.start(command_action) }
114 end
115 end
116
117 def action_info
118 # Refresh the data
119 new_context(@target_customer.customer_id)
120 end
121
122 def action_bill_plan
123 BillPlanCommand.for(@target_customer).call
124 end
125
126 class Undoable
127 def initialize(klass)
128 @klass = klass
129 end
130
131 def call(customer, admin_action_repo:, **)
132 @klass.for(customer, reply: method(:reply)).then { |action|
133 Command.customer.then { |actor|
134 action.with(actor_id: actor.customer_id).perform.then do |performed|
135 admin_action_repo.create(performed)
136 end
137 }
138 }.then { |action| "Action #{action.id}: #{action}" }
139 end
140
141 def reply(form=nil, note_type: nil, note_text: nil)
142 Command.reply { |reply|
143 reply.allowed_actions = [:next, :complete]
144 reply.command << form if form
145 reply.note_type = note_type if note_type
146 reply.note_text = note_text if note_text
147 }
148 end
149 end
150
151 class Simple
152 def initialize(klass)
153 @klass = klass
154 end
155
156 def call(customer_id, customer_repo:, snikket_repo:, **)
157 @klass.call(
158 customer_id,
159 reply: method(:reply),
160 customer_repo: customer_repo,
161 snikket_repo: snikket_repo
162 ).then { nil }
163 end
164
165 def reply(form=nil, note_type: nil, note_text: nil)
166 Command.reply { |reply|
167 reply.allowed_actions = [:next, :complete]
168 reply.command << form if form
169 reply.note_type = note_type if note_type
170 reply.note_text = note_text if note_text
171 }
172 end
173 end
174
175 class Undo
176 def self.for(target_customer, **)
177 AdminActionRepo.new
178 .find(1, customer_id: target_customer.customer_id)
179 .then { |actions|
180 raise "No actions found" if actions.empty?
181
182 actions.first.undo
183 }
184 end
185 end
186
187 [
188 [:cancel_account, Simple.new(AdminAction::CancelCustomer)],
189 [:financial, Simple.new(AdminAction::Financial)],
190 [:undo, Undoable.new(Undo)],
191 [:reset_declines, Undoable.new(AdminAction::ResetDeclines::Command)],
192 [:set_trust_level, Undoable.new(AdminAction::SetTrustLevel::Command)],
193 [:add_invites, Undoable.new(AdminAction::AddInvites::Command)],
194 [:number_change, Undoable.new(AdminAction::NumberChange::Command)],
195 [:add_transaction, Undoable.new(AdminAction::AddTransaction::Command)],
196 [:launch_snikket, Simple.new(AdminAction::LaunchSnikket)],
197 [:register_with_sgx, Simple.new(AdminAction::RegisterWithSgx)]
198 ].each do |action, handler|
199 define_method("action_#{action}") do
200 handler.call(
201 @target_customer,
202 admin_action_repo: @admin_action_repo,
203 customer_repo: @customer_repo,
204 snikket_repo: @snikket_repo
205 )
206 end
207 end
208end