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 customer.payment_methods.then do |payment_methods|
27 args.merge(payment_methods: payment_methods)
28 end
29 end
30
31 def initialize(commands)
32 @commands = commands
33 end
34
35 def each(&blk)
36 @commands.map { |c| { node: c.node, name: c.name } }.each(&blk)
37 end
38end