~singpolyma/sgx-jmp

e1e1383b000cb790f9162c9fd0985fd718ed32c4 — Stephen Paul Weber 1 year, 9 months ago e033c87 + 5ad2c9e
Merge branch 'multi-account-billing'

* multi-account-billing:
  Use billing customer for LowBalance notification
  Get settled amount from billing customer for TrustLevelRepo
  Use the multi account billing schema
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/low_balance.rb => lib/low_balance.rb +2 -2
@@ 8,10 8,10 @@ class LowBalance
		return Locked.new unless customer.registered?

		ExpiringLock.new(
			"jmp_customer_low_balance-#{customer.customer_id}",
			"jmp_customer_low_balance-#{customer.billing_customer_id}",
			expiry: 60 * 60 * 24 * 7
		).with(-> { Locked.new }) do
			for_no_lock(customer)
			customer.billing_customer.then(&method(:for_no_lock))
		end
	end


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 schemas => schemas +1 -1
@@ 1,1 1,1 @@
Subproject commit 69c1efe09190872e00b0522b3e0d9d5ad5ac5235
Subproject commit 2aef72250750b85e344d3d87d789f002db0a7579

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