From acd82262de8eca3036c2e6d92ef878e5789fc296 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 18 Apr 2022 22:14:34 -0500 Subject: [PATCH] CustomerPlan.extract To allow building from a hash with extra keys. Makes the CustomerRepo more readable. --- lib/customer_plan.rb | 10 ++++++++++ lib/customer_repo.rb | 13 ++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/customer_plan.rb b/lib/customer_plan.rb index b0b78984de6da88ea89e603704ebe5990071cbdb..c349a546bfb572ee808ef36a427874bb7032f485 100644 --- a/lib/customer_plan.rb +++ b/lib/customer_plan.rb @@ -18,6 +18,16 @@ class CustomerPlan new(customer_id, plan: plan_name&.then(&Plan.method(:for)), **kwargs) end + def self.extract(customer_id, **kwargs) + self.for( + customer_id, + **kwargs.slice( + :plan_name, :expires_at, + :auto_top_up_amount, :monthly_overage_limit + ) + ) + end + def initialize( customer_id, plan: nil, diff --git a/lib/customer_repo.rb b/lib/customer_repo.rb index 28a171dd49bfae34719fdb1b4c767387850fda08..e9070945a9d8665d52262bf84edfb745bb482308 100644 --- a/lib/customer_repo.rb +++ b/lib/customer_repo.rb @@ -152,10 +152,10 @@ protected def fetch_all(customer_id) EMPromise.all([ - @sgx_repo.get(customer_id), + @sgx_repo.get(customer_id).then { |sgx| { sgx: sgx } }, @db.query_one(SQL, customer_id, default: {}), fetch_redis(customer_id) - ]).then { |sgx, sql, redis| [sgx, sql.merge(redis)] } + ]).then { |all| all.reduce(&:merge) } end def tndetails(sgx) @@ -166,13 +166,12 @@ protected def find_inner(customer_id, jid) set_user.call(customer_id: customer_id, jid: jid) - fetch_all(customer_id).then do |(sgx, data)| + fetch_all(customer_id).then do |data| Customer.new( customer_id, Blather::JID.new(jid), - sgx: sgx, tndetails: tndetails(sgx), plan: CustomerPlan.for( - customer_id, - **data.reject { |(k, _)| k == :balance } - ), **data.slice(:balance) + tndetails: tndetails(data[:sgx]), + plan: CustomerPlan.extract(customer_id, data), + **data.slice(:balance, :sgx, :tndetails) ) end end