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)
12 EMPromise.resolve(customer&.registered?).catch { nil }.then do |reg|
13 args_for(customer, reg).then do |kwargs|
14 new(@commands.select { |c| c.list_for?(**kwargs) })
15 end
16 end
17 end
18
19 def self.args_for(customer, reg)
20 args = { customer: customer, tel: reg ? reg.phone : nil }
21 return EMPromise.resolve(args) unless args[:tel]
22
23 EMPromise.all([
24 REDIS.get("catapult_fwd-#{args[:tel]}"),
25 customer.plan_name ? customer.payment_methods : []
26 ]).then do |(fwd, payment_methods)|
27 args.merge(fwd: fwd, 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