~singpolyma/sgx-jmp

08b2f8b5092fd9444ee4ffa70969b7663b6b16b0 — Stephen Paul Weber 1 year, 12 days ago 6e0b6b1 + e1e0ddb
Merge branch 'fix-info-when-no-plan'

* fix-info-when-no-plan:
  Fix rendering info when there is no plan
4 files changed, 31 insertions(+), 39 deletions(-)

M forms/customer_info_partial.rb
M forms/plan_info.rb
M lib/customer_info.rb
M test/test_customer_info.rb
M forms/customer_info_partial.rb => forms/customer_info_partial.rb +0 -6
@@ 26,10 26,4 @@ field(
	value: "$%.4f" % @info.balance
)

field(
	var: "remaining_included_calling_credit",
	label: "Remaining Included Calling Credit",
	value: "$%.4f" % @info.remaining_included_calling_credit
)

render @info.plan_info.template

M forms/plan_info.rb => forms/plan_info.rb +8 -2
@@ 1,8 1,14 @@
field(
	var: "remaining_included_calling_credit",
	label: "Remaining Included Calling Credit",
	value: "$%.4f" % @plan_info.remaining_included_calling_credit
)

if @admin_info
	field(
		var: "plan",
		label: "Plan",
		value: @plan_info.plan.plan_name
		value: @plan_info.customer.plan_name
	)
end



@@ 20,7 26,7 @@ field(

field(
	var: "expires_at",
	label: @plan_info.plan.active? ? "Next renewal" : "Expired at",
	label: @plan_info.customer.active? ? "Next renewal" : "Expired at",
	value: @plan_info.expires_at.strftime("%Y-%m-%d")
)


M lib/customer_info.rb => lib/customer_info.rb +18 -19
@@ 16,14 16,16 @@ require_relative "proxied_jid"
class PlanInfo
	extend Forwardable

	def_delegators :plan, :expires_at, :auto_top_up_amount
	def_delegators :customer, :expires_at, :auto_top_up_amount

	def self.for(plan_or_customer)
		return EMPromise.resolve(NoPlan.new) unless plan_or_customer&.plan_name
	def self.for(customer)
		return EMPromise.resolve(NoPlan.new) unless customer&.plan_name

		plan_or_customer.activation_date.then do |adate|
			new(plan: plan_or_customer, start_date: adate)
		end
		PromiseHash.all(
			customer: customer,
			start_date: customer.activation_date,
			calling_charges_this_month: customer.calling_charges_this_month
		).then(method(:new))
	end

	class NoPlan


@@ 41,8 43,9 @@ class PlanInfo
	end

	value_semantics do
		plan Either(CustomerPlan, Customer)
		method_missing :customer, Customer
		start_date Time
		calling_charges_this_month BigDecimal
	end

	def template


@@ 54,7 57,7 @@ class PlanInfo
	end

	def monthly_price
		"$%.4f / month" % plan.monthly_price
		"$%.4f / month" % customer.monthly_price
	end

	def relative_start_date


@@ 66,11 69,15 @@ class PlanInfo
	end

	def currency
		(plan.currency || "No Currency").to_s
		(customer.currency || "No Currency").to_s
	end

	def status
		plan.active? ? "Active" : "Expired"
		customer.active? ? "Active" : "Expired"
	end

	def remaining_included_calling_credit
		[customer.minute_limit.to_d - calling_charges_this_month, 0].max
	end
end



@@ 80,8 87,6 @@ class CustomerInfo
		tel Either(String, nil)
		balance BigDecimal
		cnam Either(String, nil)
		calling_credit BigDecimal
		calling_charges_this_month BigDecimal
	end

	def self.for(customer)


@@ 89,16 94,10 @@ class CustomerInfo
			plan_info: PlanInfo.for(customer),
			tel: customer.registered? ? customer.registered?.phone : nil,
			balance: customer.balance,
			cnam: customer.tndetails.dig(:features, :lidb, :subscriber_information),
			calling_credit: customer.minute_limit.to_d,
			calling_charges_this_month: customer.calling_charges_this_month
			cnam: customer.tndetails.dig(:features, :lidb, :subscriber_information)
		).then(&method(:new))
	end

	def remaining_included_calling_credit
		[calling_credit - calling_charges_this_month, 0].max
	end

	def form
		FormTemplate.render("customer_info", info: self)
	end

M test/test_customer_info.rb => test/test_customer_info.rb +5 -12
@@ 39,6 39,7 @@ class CustomerInfoTest < Minitest::Test

		assert CustomerInfo.for(cust).sync.form
		assert_mock sgx
		assert_mock CustomerUsage::DB
	end
	em :test_info_does_not_crash



@@ 66,6 67,7 @@ class CustomerInfoTest < Minitest::Test
				.field("remaining_included_calling_credit").value
		)
		assert_mock sgx
		assert_mock CustomerUsage::DB
	end
	em :test_info_has_remaining_included_calling_credit



@@ 93,6 95,7 @@ class CustomerInfoTest < Minitest::Test
				.field("remaining_included_calling_credit").value
		)
		assert_mock sgx
		assert_mock CustomerUsage::DB
	end
	em :test_info_out_of_remaining_included_calling_credit



@@ 123,6 126,7 @@ class CustomerInfoTest < Minitest::Test
		assert AdminInfo.for(cust, trust_level_repo: trust_repo).sync.form
		assert_mock sgx
		assert_mock trust_repo
		assert_mock CustomerUsage::DB
	end
	em :test_admin_info_does_not_crash



@@ 162,16 166,11 @@ class CustomerInfoTest < Minitest::Test
		).sync.form
		assert_mock call_attempt_repo
		assert_mock trust_repo
		assert_mock CustomerUsage::DB
	end
	em :test_admin_info_with_tel_does_not_crash

	def test_inactive_info_does_not_crash
		CustomerUsage::DB.expect(
			:query_one,
			EMPromise.resolve({ charges: 0.to_d }),
			[String, "test"]
		)

		sgx = Minitest::Mock.new
		sgx.expect(:registered?, false)



@@ 188,12 187,6 @@ class CustomerInfoTest < Minitest::Test
	em :test_inactive_info_does_not_crash

	def test_inactive_admin_info_does_not_crash
		CustomerUsage::DB.expect(
			:query_one,
			EMPromise.resolve({ charges: 0.to_d }),
			[String, "test"]
		)

		sgx = Minitest::Mock.new
		sgx.expect(:registered?, false)
		sgx.expect(:registered?, false)