~singpolyma/sgx-jmp

8b80a1f7996432ac9c2fc1575944c0f01e44d9f9 — Stephen Paul Weber 1 year, 11 months ago 73c1d57
Get settled amount from billing customer for TrustLevelRepo
4 files changed, 31 insertions(+), 11 deletions(-)

M lib/customer.rb
M lib/customer_repo.rb
M lib/trust_level_repo.rb
M test/test_trust_level_repo.rb
M lib/customer.rb => lib/customer.rb +21 -1
@@ 21,6 21,7 @@ class Customer
	extend Forwardable

	attr_reader :customer_id, :balance, :jid, :tndetails
	alias billing_customer_id customer_id

	def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
	               :currency, :merchant_account, :plan_name, :minute_limit,


@@ 34,7 35,7 @@ class Customer
	               :transactions

	def self.extract(customer_id, jid, **kwargs)
		Customer.new(
		(kwargs[:parent_customer_id] ? ChildCustomer : Customer).new(
			customer_id, jid,
			plan: CustomerPlan.extract(customer_id, kwargs),
			**kwargs.slice(:balance, :sgx, :tndetails)


@@ 75,6 76,10 @@ class Customer
		)
	end

	def billing_customer(*)
		EMPromise.resolve(self)
	end

	def unused_invites
		InvitesRepo.new(DB).unused_invites(customer_id)
	end


@@ 128,4 133,19 @@ class Customer
	end

	protected def_delegator :@plan, :expires_at

	class ChildCustomer < Customer
		def initialize(*args, parent_customer_id:, **kwargs)
			super(*args, **kwargs)
			@parent_customer_id = parent_customer_id
		end

		def billing_customer_id
			@parent_customer_id
		end

		def billing_customer(repo=CustomerRepo.new)
			repo.find(billing_customer_id)
		end
	end
end

M lib/customer_repo.rb => lib/customer_repo.rb +1 -1
@@ 173,7 173,7 @@ protected
	end

	SQL = <<~SQL
		SELECT COALESCE(balance,0) AS balance, plan_name, expires_at
		SELECT COALESCE(balance,0) AS balance, plan_name, expires_at, parent_customer_id
		FROM customer_plans LEFT JOIN balances USING (customer_id)
		WHERE customer_id=$1 LIMIT 1
	SQL

M lib/trust_level_repo.rb => lib/trust_level_repo.rb +1 -1
@@ 14,7 14,7 @@ class TrustLevelRepo
	def find(customer)
		EMPromise.all([
			find_manual(customer.customer_id),
			fetch_settled_amount(customer.customer_id)
			fetch_settled_amount(customer.billing_customer_id)
		]).then do |(manual, row)|
			TrustLevel.for(
				manual: manual,

M test/test_trust_level_repo.rb => test/test_trust_level_repo.rb +8 -8
@@ 9,7 9,7 @@ class TrustLevelRepoTest < Minitest::Test
			redis: FakeRedis.new(
				"jmp_customer_trust_level-test" => "Tomb"
			)
		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
		).find(customer(plan_name: "test_usd")).sync
		assert_equal "Manual(Tomb)", trust_level.to_s
	end
	em :test_manual_tomb


@@ 20,7 20,7 @@ class TrustLevelRepoTest < Minitest::Test
			redis: FakeRedis.new(
				"jmp_customer_trust_level-test" => "Basement"
			)
		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
		).find(customer(plan_name: "test_usd")).sync
		assert_equal "Manual(Basement)", trust_level.to_s
	end
	em :test_manual_basement


@@ 31,7 31,7 @@ class TrustLevelRepoTest < Minitest::Test
			redis: FakeRedis.new(
				"jmp_customer_trust_level-test" => "Customer"
			)
		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
		).find(customer(plan_name: "test_usd")).sync
		assert_equal "Manual(Customer)", trust_level.to_s
	end
	em :test_manual_customer


@@ 42,7 42,7 @@ class TrustLevelRepoTest < Minitest::Test
			redis: FakeRedis.new(
				"jmp_customer_trust_level-test" => "Paragon"
			)
		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
		).find(customer(plan_name: "test_usd")).sync
		assert_equal "Manual(Paragon)", trust_level.to_s
	end
	em :test_manual_paragon


@@ 53,7 53,7 @@ class TrustLevelRepoTest < Minitest::Test
			redis: FakeRedis.new(
				"jmp_customer_trust_level-test" => "UNKNOWN"
			)
		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
		).find(customer(plan_name: "test_usd")).sync
		assert_equal "Manual(Customer)", trust_level.to_s
	end
	em :test_manual_unknown


@@ 62,7 62,7 @@ class TrustLevelRepoTest < Minitest::Test
		trust_level = TrustLevelRepo.new(
			db: FakeDB.new,
			redis: FakeRedis.new
		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
		).find(customer(plan_name: "test_usd")).sync
		assert_kind_of TrustLevel::Basement, trust_level
	end
	em :test_new_customer


@@ 71,7 71,7 @@ class TrustLevelRepoTest < Minitest::Test
		trust_level = TrustLevelRepo.new(
			db: FakeDB.new(["test"] => [{ "settled_amount" => 15 }]),
			redis: FakeRedis.new
		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
		).find(customer(plan_name: "test_usd")).sync
		assert_kind_of TrustLevel::Customer, trust_level
	end
	em :test_regular_customer


@@ 80,7 80,7 @@ class TrustLevelRepoTest < Minitest::Test
		trust_level = TrustLevelRepo.new(
			db: FakeDB.new(["test"] => [{ "settled_amount" => 61 }]),
			redis: FakeRedis.new
		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
		).find(customer(plan_name: "test_usd")).sync
		assert_kind_of TrustLevel::Paragon, trust_level
	end
	em :test_settled_customer