diff --git a/lib/customer_info.rb b/lib/customer_info.rb index ebe13a8c90f6ad6a100e614f70354d21cfda3193..353f59dfce318afe29f76c952543cf286bf3b126 100644 --- a/lib/customer_info.rb +++ b/lib/customer_info.rb @@ -7,6 +7,7 @@ require "value_semantics/monkey_patched" require_relative "proxied_jid" require_relative "customer_plan" require_relative "form_template" +require_relative "promise_hash" class PlanInfo extend Forwardable @@ -78,14 +79,12 @@ class CustomerInfo end def self.for(customer, plan) - PlanInfo.for(plan).then do |plan_info| - new( - plan_info: plan_info, - tel: customer.registered? ? customer.registered?.phone : nil, - balance: customer.balance, - cnam: customer.tndetails.dig(:features, :lidb, :subscriber_information) - ) - end + PromiseHash.all( + plan_info: PlanInfo.for(plan), + tel: customer.registered? ? customer.registered?.phone : nil, + balance: customer.balance, + cnam: customer.tndetails.dig(:features, :lidb, :subscriber_information) + ).then(&method(:new)) end def form @@ -103,16 +102,13 @@ class AdminInfo end def self.for(customer, plan) - EMPromise.all([ - CustomerInfo.for(customer, plan), - customer.api - ]).then do |info, api_value| - new( - jid: customer.jid, - customer_id: customer.customer_id, - fwd: customer.fwd, info: info, api: api_value - ) - end + PromiseHash.all( + jid: customer.jid, + customer_id: customer.customer_id, + fwd: customer.fwd, + info: CustomerInfo.for(customer, plan), + api: customer.api + ).then(&method(:new)) end def form diff --git a/lib/promise_hash.rb b/lib/promise_hash.rb new file mode 100644 index 0000000000000000000000000000000000000000..3228497788bf86707366eef7463387edc93df808 --- /dev/null +++ b/lib/promise_hash.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "em_promise" + +module PromiseHash + def self.all(**kwargs) + keys = kwargs.keys + EMPromise.all(kwargs.values).then { |results| + Hash[keys.zip(results)] + } + end +end