~singpolyma/sgx-jmp

ref: bbc0bb6a11c1d459a1511e88bd80997d158e2603 sgx-jmp/lib/command_list.rb -rw-r--r-- 1.4 KiB
bbc0bb6aStephen Paul Weber Refactor command list to use composition 2 years 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
# frozen_string_literal: true

class CommandList
	include Enumerable

	def self.for(jid)
		Customer.for_jid(jid).catch { nil }.then do |customer|
			EMPromise.resolve(customer&.registered?).catch { nil }.then do |reg|
				next Registered.for(customer, reg.phone) if reg
				CommandList.new
			end
		end
	end

	def each
		yield node: "jabber:iq:register", name: "Register"
	end

	class Registered < CommandList
		def self.for(customer, tel)
			EMPromise.all([
				REDIS.get("catapult_fwd-#{tel}"),
				customer.plan_name ? customer.payment_methods : []
			]).then do |(fwd, payment_methods)|
				Registered.new(*[
					(HAS_CREDIT_CARD unless payment_methods.empty?),
					(HAS_FORWARDING if fwd)
				].compact)
			end
		end

		def initialize(*args)
			@extra = args
		end

		ALWAYS = [
			{ node: "number-display", name: "Display JMP Number" },
			{ node: "configure-calls", name: "Configure Calls" },
			{ node: "usage", name: "Show Monthly Usage" },
			{ node: "reset sip account", name: "Create or Reset SIP Account" },
			{
				node: "credit cards",
				name: "Credit Card Settings and Management"
			}
		].freeze

		def each
			super
			([ALWAYS] + @extra).each do |commands|
				commands.each { |x| yield x }
			end
		end
	end


	HAS_FORWARDING = [
		node: "record-voicemail-greeting",
		name: "Record Voicemail Greeting"
	].freeze

	HAS_CREDIT_CARD = [
		node: "top up", name: "Buy Account Credit by Credit Card"
	].freeze
end