command_list.rb

 1# frozen_string_literal: true
 2
 3class CommandList
 4	include Enumerable
 5
 6	def self.register(command)
 7		@commands ||= []
 8		@commands << command
 9	end
10
11	def self.for(customer, from_jid)
12		args_for(customer, from_jid).then do |kwargs|
13			EMPromise.all(
14				@commands.select { |c|
15					c.list_for?(**kwargs)
16				}.then(&method(:new))
17			)
18		end
19	end
20
21	def self.args_for(customer, from_jid)
22		args = {
23			from_jid: from_jid, customer: customer,
24			tel: customer&.registered? ? customer&.registered?&.phone : nil,
25			fwd: customer&.fwd, feature_flags: customer&.feature_flags || [],
26			payment_methods: [],
27			tn_portable: false
28		}
29		return EMPromise.resolve(args) unless customer&.plan_name
30
31		customer.payment_methods.then do |payment_methods|
32			args.merge(payment_methods: payment_methods)
33		end
34	end
35
36	def initialize(commands)
37		@commands = commands
38	end
39
40	def each(&blk)
41		@commands.map { |c| { node: c.node, name: c.name } }.each(&blk)
42	end
43end