diff --git a/forms/customer_info_partial.rb b/forms/customer_info_partial.rb index 44a0d36f113e95ed21f133eb38c3c27ef46ec086..51d51b48fb0e3b4b23a04b825115834f30a17a9b 100644 --- a/forms/customer_info_partial.rb +++ b/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 diff --git a/forms/plan_info.rb b/forms/plan_info.rb index f5c1e18c12626c952998ea98bc7367ed5a340c2f..18d68e843e5475e4beeb378649dbe6f668d09dc9 100644 --- a/forms/plan_info.rb +++ b/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") ) diff --git a/lib/customer_info.rb b/lib/customer_info.rb index a21eb83a8a0c8ecd7bb26365db4d0f9073538399..a9acde2ab870013fee3e8aeb322d8d8ecff00c4d 100644 --- a/lib/customer_info.rb +++ b/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 diff --git a/test/test_customer_info.rb b/test/test_customer_info.rb index c3305efa771c600a3570a5985bec69f9ee7cb6dd..5d30722ba20806534ee78d22ba9a8b3b19f18bc5 100644 --- a/test/test_customer_info.rb +++ b/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)