~singpolyma/dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2 2

[PATCH 1/2] add billing_customer_id to admin customer info command

Details
Message ID
<20240404015218.27176-1-sourcehut@lazytapir.com>
DKIM signature
pass
Download raw message
Patch: +9 -1
---
 forms/admin_plan_info.rb | 6 ++++++
 lib/customer_info.rb     | 4 +++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/forms/admin_plan_info.rb b/forms/admin_plan_info.rb
index 21c2a3e..871046a 100644
--- a/forms/admin_plan_info.rb
+++ b/forms/admin_plan_info.rb
@@ -4,3 +4,9 @@ field(
	description: @plan_info.relative_start_date,
	value: @plan_info.formatted_start_date
)

field(
	var: "billing_customer_id",
	label: "Billing Customer Id",
	value: @plan_info.billing_customer_id
)
diff --git a/lib/customer_info.rb b/lib/customer_info.rb
index 2369e39..520c252 100644
--- a/lib/customer_info.rb
+++ b/lib/customer_info.rb
@@ -24,7 +24,8 @@ class PlanInfo
		PromiseHash.all(
			customer: customer,
			start_date: customer.activation_date,
			calling_charges_this_month: customer.calling_charges_this_month
			calling_charges_this_month: customer.calling_charges_this_month,
			billing_customer_id: customer.billing_customer_id
		).then(method(:new))
	end

@@ -46,6 +47,7 @@ class PlanInfo
		method_missing :customer, Customer
		start_date Time
		calling_charges_this_month BigDecimal
		billing_customer_id String
	end

	def template
-- 
2.44.0

[PATCH 2/2] show subaccounts in admin_info if any exist

Details
Message ID
<20240404015218.27176-2-sourcehut@lazytapir.com>
In-Reply-To
<20240404015218.27176-1-sourcehut@lazytapir.com> (view parent)
DKIM signature
pass
Download raw message
Patch: +79 -1
---
 forms/admin_plan_info.rb   |  7 ++++++
 lib/customer_info.rb       |  7 +++++-
 lib/subaccount.rb          | 16 ++++++++++++
 test/test_customer_info.rb | 50 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100644 lib/subaccount.rb

diff --git a/forms/admin_plan_info.rb b/forms/admin_plan_info.rb
index 871046a..ee78e8c 100644
--- a/forms/admin_plan_info.rb
+++ b/forms/admin_plan_info.rb
@@ -10,3 +10,10 @@ field(
	label: "Billing Customer Id",
	value: @plan_info.billing_customer_id
)

if !@admin_info.subaccounts.empty?
	table(
		@admin_info.subaccounts,
		customer_id: "Customer Id"
	)
end
diff --git a/lib/customer_info.rb b/lib/customer_info.rb
index 520c252..48817d0 100644
--- a/lib/customer_info.rb
+++ b/lib/customer_info.rb
@@ -12,6 +12,7 @@ require_relative "customer_plan"
require_relative "form_template"
require_relative "promise_hash"
require_relative "proxied_jid"
require_relative "subaccount"

class PlanInfo
	extend Forwardable
@@ -115,6 +116,7 @@ class AdminInfo
		call_info String
		trust_level String
		backend_jid String
		subaccounts ArrayOf(::Subaccount), default: []
	end

	def self.for(
@@ -131,7 +133,10 @@ class AdminInfo
			api: API.for(customer),
			call_info: call_info(customer, call_attempt_repo),
			trust_level: trust_level_repo.find(customer).then(&:to_s),
			backend_jid: backend_repo.get(customer.customer_id).from_jid.to_s
			backend_jid: backend_repo.get(customer.customer_id).from_jid.to_s,
			subaccounts: customer.billing_customer_id == customer.customer_id ?
				Subaccount.get_subaccounts(customer.billing_customer_id) :
				[]
		).then(&method(:new))
	end

diff --git a/lib/subaccount.rb b/lib/subaccount.rb
new file mode 100644
index 0000000..34be2ee
--- /dev/null
+++ b/lib/subaccount.rb
@@ -0,0 +1,16 @@
class Subaccount
    value_semantics do
        customer_id String
    end

    def initialize(customer_id)
        @customer_id = customer_id
    end

    def self.get_subaccounts(parent_id)
		DB.query_one(<<~SQL, parent_id)
			SELECT customer_id FROM customer_plans WHERE parent_customer_id=$1
		SQL
		.then { |rows| rows.map { |customer_id| Subaccount.new(customer_id) } }
	end
end
\ No newline at end of file
diff --git a/test/test_customer_info.rb b/test/test_customer_info.rb
index 5d30722..b062d48 100644
--- a/test/test_customer_info.rb
+++ b/test/test_customer_info.rb
@@ -17,6 +17,7 @@ PlanInfo::DB = FakeDB.new(
		}
	]
)
Subaccount::DB = Minitest::Mock.new

class CustomerInfoTest < Minitest::Test
	def test_info_does_not_crash
@@ -118,6 +119,12 @@ class CustomerInfoTest < Minitest::Test
			[String, "test"]
		)

		Subaccount::DB.expect(
			:query_one,
			EMPromise.resolve({ }),
			[String, "test"]
		)

		cust = customer(sgx: sgx, plan_name: "test_usd")

		trust_repo = Minitest::Mock.new
@@ -147,6 +154,12 @@ class CustomerInfoTest < Minitest::Test
			[String, "test"]
		)

		Subaccount::DB.expect(
			:query_one,
			EMPromise.resolve({ }),
			[String, "test"]
		)

		cust = customer(sgx: sgx, plan_name: "test_usd")

		call_attempt_repo = Minitest::Mock.new
@@ -192,6 +205,12 @@ class CustomerInfoTest < Minitest::Test
		sgx.expect(:registered?, false)
		sgx.expect(:fwd, CustomerFwd::None.new(uri: nil, timeout: nil))

		Subaccount::DB.expect(
			:query_one,
			EMPromise.resolve({ }),
			[String, "test"]
		)

		plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
		cust = Customer.new(
			"test",
@@ -208,4 +227,35 @@ class CustomerInfoTest < Minitest::Test
		assert_mock trust_repo
	end
	em :test_inactive_admin_info_does_not_crash

	def test_admin_info_subaccount_does_not_crash
		sgx = Minitest::Mock.new
		sgx.expect(:registered?, false)
		fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
		sgx.expect(:fwd, fwd)
		sgx.expect(:registered?, false)

		CustomerPlan::DB.expect(
			:query_one,
			EMPromise.resolve({ start_date: Time.now }),
			[String, "test"]
		)

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

		cust = customer(sgx: sgx, plan_name: "test_usd", parent_customer_id: 'parent')

		trust_repo = Minitest::Mock.new
		trust_repo.expect(:find, TrustLevel::Basement, [cust])

		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_subaccount_does_not_crash
end
-- 
2.44.0

Re: [PATCH 2/2] show subaccounts in admin_info if any exist

Details
Message ID
<Zg4J8/Hm1LWX1aXq@singpolyma-beefy.lan>
In-Reply-To
<20240404015218.27176-2-sourcehut@lazytapir.com> (view parent)
DKIM signature
pass
Download raw message
Somebody claiming to be SavagePeanut wrote:
>---
> forms/admin_plan_info.rb   |  7 ++++++
> lib/customer_info.rb       |  7 +++++-
> lib/subaccount.rb          | 16 ++++++++++++
> test/test_customer_info.rb | 50 ++++++++++++++++++++++++++++++++++++++
> 4 files changed, 79 insertions(+), 1 deletion(-)
> create mode 100644 lib/subaccount.rb
>
>diff --git a/forms/admin_plan_info.rb b/forms/admin_plan_info.rb
>index 871046a..ee78e8c 100644
>--- a/forms/admin_plan_info.rb
>+++ b/forms/admin_plan_info.rb
>@@ -10,3 +10,10 @@ field(
> 	label: "Billing Customer Id",
> 	value: @plan_info.billing_customer_id
> )
>+
>+if !@admin_info.subaccounts.empty?
>+	table(
>+		@admin_info.subaccounts,
>+		customer_id: "Customer Id"
>+	)
>+end
>diff --git a/lib/customer_info.rb b/lib/customer_info.rb
>index 520c252..48817d0 100644
>--- a/lib/customer_info.rb
>+++ b/lib/customer_info.rb
>@@ -12,6 +12,7 @@ require_relative "customer_plan"
> require_relative "form_template"
> require_relative "promise_hash"
> require_relative "proxied_jid"
>+require_relative "subaccount"
>
> class PlanInfo
> 	extend Forwardable
>@@ -115,6 +116,7 @@ class AdminInfo
> 		call_info String
> 		trust_level String
> 		backend_jid String
>+		subaccounts ArrayOf(::Subaccount), default: []
> 	end
>
> 	def self.for(
>@@ -131,7 +133,10 @@ class AdminInfo
> 			api: API.for(customer),
> 			call_info: call_info(customer, call_attempt_repo),
> 			trust_level: trust_level_repo.find(customer).then(&:to_s),
>-			backend_jid: backend_repo.get(customer.customer_id).from_jid.to_s
>+			backend_jid: backend_repo.get(customer.customer_id).from_jid.to_s,
>+			subaccounts: customer.billing_customer_id == customer.customer_id ?
>+				Subaccount.get_subaccounts(customer.billing_customer_id) :
>+				[]

For an admin command like this, probably doesn't hurt to just always do the 
query by customer.customer_id.  Yes, we know it will always be empty in this 
special case, but maybe not worth the conditional to check it
Reply to thread Export thread (mbox)