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