Merge branch 'fix-info-when-no-plan'

Stephen Paul Weber created

* fix-info-when-no-plan:
  Fix rendering info when there is no plan

Change summary

forms/customer_info_partial.rb |  6 -----
forms/plan_info.rb             | 10 +++++++-
lib/customer_info.rb           | 37 +++++++++++++++++------------------
test/test_customer_info.rb     | 17 ++++-----------
4 files changed, 31 insertions(+), 39 deletions(-)

Detailed changes

forms/customer_info_partial.rb 🔗

@@ -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

forms/plan_info.rb 🔗

@@ -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")
 )
 

lib/customer_info.rb 🔗

@@ -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

test/test_customer_info.rb 🔗

@@ -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)