~singpolyma/sgx-jmp

ref: 1c36e692925bc827b2e3417c7f30b3255c423e55 sgx-jmp/lib/admin_command.rb -rw-r--r-- 1.5 KiB
1c36e692Christopher Vollick Admin Command Menu + Admin Financial View 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

require_relative "customer_info_form"
require_relative "financial_info"
require_relative "form_template"

class AdminCommand
	def initialize(target_customer)
		@target_customer = target_customer
	end

	def start
		action_info.then { menu }
	end

	def reply(form)
		Command.reply { |reply|
			reply.allowed_actions = [:next]
			reply.command << form
		}
	end

	def menu
		reply(FormTemplate.render("admin_menu")).then do |response|
			handle(response.form.field("action").value)
		end
	end

	def handle(action)
		if respond_to?("action_#{action}")
			send("action_#{action}")
		else
			new_context(action)
		end.then { menu }
	end

	def new_context(q)
		CustomerInfoForm.new.parse_something(q).then do |new_customer|
			if new_customer.respond_to?(:customer_id)
				AdminCommand.new(new_customer).start
			else
				reply(new_customer.form)
			end
		end
	end

	def action_info
		@target_customer.admin_info.then do |info|
			reply(info.form)
		end
	end

	def action_financial
		AdminFinancialInfo.for(@target_customer).then do |financial_info|
			reply(FormTemplate.render(
				"admin_financial_info",
				info: financial_info
			)).then {
				pay_methods(financial_info)
			}.then {
				transactions(financial_info)
			}
		end
	end

	def pay_methods(financial_info)
		reply(FormTemplate.render(
			"admin_payment_methods",
			**financial_info.to_h
		))
	end

	def transactions(financial_info)
		reply(FormTemplate.render(
			"admin_transaction_list",
			transactions: financial_info.transactions
		))
	end
end