diff --git a/lib/customer.rb b/lib/customer.rb index ae36f35109ea2b8f2d3659dfa1fe46b41854bbdb..e309ddb6faef1b27819119a099d8aec4464984c1 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -33,6 +33,14 @@ class Customer :add_btc_address, :declines, :mark_decline, :transactions + def self.extract(customer_id, jid, **kwargs) + Customer.new( + customer_id, jid, + plan: CustomerPlan.extract(customer_id, kwargs), + **kwargs.slice(:balance, :sgx, :tndetails) + ) + end + def initialize( customer_id, jid, diff --git a/lib/customer_repo.rb b/lib/customer_repo.rb index 8858d73d18c275b44c85b45b128eccb9f7fd0e2b..35a3ec38bf28a273805a461f70b1c2d4052df8a2 100644 --- a/lib/customer_repo.rb +++ b/lib/customer_repo.rb @@ -38,7 +38,7 @@ class CustomerRepo redis.get("jmp_customer_jid-#{customer_id}").then do |jid| raise NotFound, "No jid" unless jid - [customer_id, jid] + [customer_id, Blather::JID.new(jid)] end end end @@ -56,7 +56,7 @@ class CustomerRepo redis.get("jmp_customer_id-#{jid}").then do |customer_id| raise NotFound, "No customer" unless customer_id - [customer_id, jid] + [customer_id, Blather::JID.new(jid)] end end end @@ -173,29 +173,20 @@ protected WHERE customer_id=$1 LIMIT 1 SQL - def fetch_all(customer_id) - EMPromise.all([ - @sgx_repo.get(customer_id).then { |sgx| { sgx: sgx } }, - @db.query_one(SQL, customer_id, default: {}), - fetch_redis(customer_id) - ]).then { |all| all.reduce(&:merge) } - end - def tndetails(sgx) return {} unless sgx.registered? LazyObject.new { @bandwidth_tn_repo.find(sgx.registered?.phone) || {} } end - def find_inner(customer_id, jid) - set_user.call(customer_id: customer_id, jid: jid) - fetch_all(customer_id).then do |data| - Customer.new( - customer_id, Blather::JID.new(jid), - tndetails: tndetails(data[:sgx]), - plan: CustomerPlan.extract(customer_id, data), - **data.slice(:balance, :sgx, :tndetails) - ) + def find_inner(cid, jid) + set_user.call(customer_id: cid, jid: jid) + EMPromise.all([ + @sgx_repo.get(cid).then { |sgx| { sgx: sgx } }, + @db.query_one(SQL, cid, default: {}), + fetch_redis(cid) + ]).then { |all| all.reduce(&:merge) }.then do |data| + Customer.extract(cid, jid, tndetails: tndetails(data[:sgx]), **data) end end end