~singpolyma/sgx-jmp

c97470e1a2c3853c2b933655645bcfa133e95bab — Stephen Paul Weber 1 year, 2 months ago 67b11b2
Add auto-top-up to customer info report
2 files changed, 44 insertions(+), 32 deletions(-)

M lib/customer_info.rb
M test/test_customer_info.rb
M lib/customer_info.rb => lib/customer_info.rb +23 -1
@@ 8,15 8,17 @@ require_relative "customer_plan"
class CustomerInfo
	value_semantics do
		plan CustomerPlan
		auto_top_up_amount Integer
		tel Either(String, nil)
		balance BigDecimal
		expires_at Either(Time, nil)
	end

	def self.for(customer, plan, expires_at)
		customer.registered?.then do |registration|
		fetch_inputs(customer, plan).then do |(auto_top_up_amount, registration)|
			new(
				plan: plan,
				auto_top_up_amount: auto_top_up_amount,
				tel: registration ? registration.phone : nil,
				balance: customer.balance,
				expires_at: expires_at


@@ 24,6 26,13 @@ class CustomerInfo
		end
	end

	def self.fetch_inputs(customer, plan)
		EMPromise.all([
			plan.auto_top_up_amount,
			customer.registered?
		])
	end

	def account_status
		if plan.plan_name.nil?
			"Transitional"


@@ 43,6 52,18 @@ class CustomerInfo
		{ var: "Renewal", value: "$%.4f / month" % plan.monthly_price }
	end

	def auto_top_up
		{
			label: "Auto Top-up"
		}.merge(
			if auto_top_up_amount.positive?
				{ value: auto_top_up_amount.to_s, var: "auto_top_up_amount" }
			else
				{ value: "No" }
			end
		)
	end

	def fields
		[
			{ var: "Account Status", value: account_status },


@@ 50,6 71,7 @@ class CustomerInfo
			{ var: "Balance", value: "$%.4f" % balance },
			monthly_amount,
			next_renewal,
			auto_top_up,
			{ var: "Currency", value: (plan.currency || "No Currency").to_s }
		].compact
	end

M test/test_customer_info.rb => test/test_customer_info.rb +21 -31
@@ 1,14 1,22 @@
# frozen_string_literal: true

require "test_helper"
require "customer_info"

API::REDIS = Minitest::Mock.new
API::REDIS = FakeRedis.new
CustomerPlan::REDIS = Minitest::Mock.new

class CustomerInfoTest < Minitest::Test
	def test_info_does_not_crash
		sgx = Minitest::Mock.new
		sgx.expect(:registered?, EMPromise.resolve(nil))

		CustomerPlan::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["jmp_customer_auto_top_up_amount-test"]
		)

		cust = customer(sgx: sgx)
		assert cust.info.sync.fields
		assert_mock sgx


@@ 19,16 27,10 @@ class CustomerInfoTest < Minitest::Test
		sgx = Minitest::Mock.new
		sgx.expect(:registered?, EMPromise.resolve(nil))

		API::REDIS.expect(
			:exists,
		CustomerPlan::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["catapult_cred-customer_test@jmp.chat"]
		)

		API::REDIS.expect(
			:lindex,
			EMPromise.resolve(nil),
			["catapult_cred-test@example.net", 0]
			["jmp_customer_auto_top_up_amount-test"]
		)

		cust = customer(sgx: sgx)


@@ 41,6 43,12 @@ class CustomerInfoTest < Minitest::Test
		sgx = Minitest::Mock.new
		sgx.expect(:registered?, EMPromise.resolve(nil))

		CustomerPlan::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["jmp_customer_auto_top_up_amount-test"]
		)

		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
		cust = Customer.new(
			"test",


@@ 57,16 65,10 @@ class CustomerInfoTest < Minitest::Test
		sgx = Minitest::Mock.new
		sgx.expect(:registered?, EMPromise.resolve(nil))

		API::REDIS.expect(
			:exists,
		CustomerPlan::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["catapult_cred-customer_test@jmp.chat"]
		)

		API::REDIS.expect(
			:lindex,
			EMPromise.resolve(nil),
			["catapult_cred-test@example.net", 0]
			["jmp_customer_auto_top_up_amount-test"]
		)

		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)


@@ 92,18 94,6 @@ class CustomerInfoTest < Minitest::Test
	em :test_legacy_customer_info_does_not_crash

	def test_legacy_customer_admin_info_does_not_crash
		API::REDIS.expect(
			:exists,
			EMPromise.resolve(nil),
			["catapult_cred-customer_@jmp.chat"]
		)

		API::REDIS.expect(
			:lindex,
			EMPromise.resolve(nil),
			["catapult_cred-legacy@example.com", 0]
		)

		cust = LegacyCustomer.new(
			Blather::JID.new("legacy@example.com"),
			"+12223334444"