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 new(@commands.select { |c| c.list_for?(**kwargs) })
14 end
15 end
16
17 def self.args_for(customer, from_jid)
18 args = {
19 from_jid: from_jid, customer: customer,
20 tel: customer&.registered? ? customer&.registered?&.phone : nil,
21 fwd: customer&.fwd, feature_flags: customer&.feature_flags || [],
22 payment_methods: []
23 }
24 return EMPromise.resolve(args) unless customer&.plan_name
25
26 EMPromise.all([
27 customer.payment_methods,
28 customer.commands
29 ]).then do |(payment_methods, sgx_commands)|
30 args.merge(payment_methods: payment_methods, sgx_commands: sgx_commands)
31 end
32 end
33
34 def initialize(commands)
35 @commands = commands
36 end
37
38 def each(&blk)
39 @commands.map { |c| { node: c.node, name: c.name } }.each(&blk)
40 end
41end