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		registered = customer&.registered?
23		args = {
24			from_jid: from_jid, customer: customer,
25			tel: registered&.phone,
26			fwd: customer&.fwd, feature_flags: customer&.feature_flags || [],
27			payment_methods: [],
28			tn_portable: false
29		}
30		return EMPromise.resolve(args) unless customer&.plan_name
31
32		customer.payment_methods.then do |payment_methods|
33			args.merge(payment_methods: payment_methods)
34		end
35	end
36
37	def initialize(commands)
38		@commands = commands
39	end
40
41	def each(&blk)
42		@commands.map { |c| { node: c.node, name: c.name } }.each(&blk)
43	end
44end