~singpolyma/sgx-jmp

acd82262de8eca3036c2e6d92ef878e5789fc296 — Stephen Paul Weber 2 years ago f571b5e
CustomerPlan.extract

To allow building from a hash with extra keys. Makes the CustomerRepo more readable.
2 files changed, 16 insertions(+), 7 deletions(-)

M lib/customer_plan.rb
M lib/customer_repo.rb
M lib/customer_plan.rb => lib/customer_plan.rb +10 -0
@@ 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,

M lib/customer_repo.rb => lib/customer_repo.rb +6 -7
@@ 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