From 4cf1b6d9585d861fa8da54b2e332b2480391b4da Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 11 Apr 2023 12:10:25 -0500 Subject: [PATCH] New parented customer should save plan and get balance from parent --- lib/customer.rb | 6 ++++-- lib/customer_plan.rb | 5 +++-- lib/customer_repo.rb | 2 +- test/test_customer_repo.rb | 38 +++++++++++++++++++++++++++++++++++--- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index f48fa951f66024fb00c05d9eefaa79a3afee2dce..8dd8c9bd74a33934e9dda4ea05ff5b5f75799120 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -42,10 +42,12 @@ class Customer ) end - def self.created(customer_id, jid, **kwargs) + def self.created(customer_id, jid, repo:, **kwargs) + jid = Blather::JID.new(jid) plan = CustomerPlan.default(customer_id, jid) if plan.parent_customer_id - ChildCustomer.new(customer_id, jid, plan: plan, **kwargs) + # Parent may have a balance, so look it up + plan.save_plan!.then { repo.find(customer_id) } else new(customer_id, jid, plan: plan, **kwargs) end diff --git a/lib/customer_plan.rb b/lib/customer_plan.rb index 55c13da516bf561d85f15302460cf0b39a4197c2..8ca01653ddd193276f4e2813295aa997b9dba616 100644 --- a/lib/customer_plan.rb +++ b/lib/customer_plan.rb @@ -71,12 +71,13 @@ class CustomerPlan end def save_plan! - DB.exec_defer(<<~SQL, [@customer_id, plan_name]) + DB.exec_defer(<<~SQL, [@customer_id, plan_name, @parent_customer_id]) INSERT INTO plan_log - (customer_id, plan_name, date_range) + (customer_id, plan_name, parent_customer_id, date_range) VALUES ( $1, $2, + $3, tsrange( LOCALTIMESTAMP - '2 seconds'::interval, LOCALTIMESTAMP - '1 second'::interval diff --git a/lib/customer_repo.rb b/lib/customer_repo.rb index 004dacc6547ed08edbcf689f60c0f74001ed8fdf..e9216e4fe86fdec063d1e3ce0f4385b0c22da9da 100644 --- a/lib/customer_repo.rb +++ b/lib/customer_repo.rb @@ -102,7 +102,7 @@ class CustomerRepo ).then do |redis_result| raise "Saving new customer to redis failed" unless redis_result == 1 - Customer.created(cid, Blather::JID.new(jid), sgx: new_sgx(cid)) + Customer.created(cid, jid, sgx: new_sgx(cid), repo: self) end end end diff --git a/test/test_customer_repo.rb b/test/test_customer_repo.rb index 007ce57e8d1b7224ed64520bb8ecfc05ec8219fc..1d84993728615f3647451ab7c5fb5c4cd99dec80 100644 --- a/test/test_customer_repo.rb +++ b/test/test_customer_repo.rb @@ -7,6 +7,8 @@ class CustomerRepo attr_reader :sgx_repo end +CustomerPlan::DB = Minitest::Mock.new + class CustomerRepoTest < Minitest::Test FAKE_REDIS = FakeRedis.new( # sgx-jmp customer @@ -38,6 +40,12 @@ class CustomerRepoTest < Minitest::Test "plan_name" => "test_usd", "expires_at" => Time.now + 100 }], + ["testp"] => [{ + "balance" => BigDecimal(1234), + "plan_name" => "test_usd", + "expires_at" => Time.now + 100, + "parent_customer_id" => "1234" + }], ["test_v2"] => [{ "balance" => BigDecimal(2345), "plan_name" => "test_usd", @@ -157,23 +165,47 @@ class CustomerRepoTest < Minitest::Test braintree_customer.expect( :create, EMPromise.resolve( - OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test")) + OpenStruct.new(success?: true, customer: OpenStruct.new(id: "testp")) ) ) redis.expect( :msetnx, EMPromise.resolve(1), [ - "jmp_customer_id-test@parented.example.com", "test", - "jmp_customer_jid-test", "test@parented.example.com" + "jmp_customer_id-test@parented.example.com", "testp", + "jmp_customer_jid-testp", "test@parented.example.com" + ] + ) + redis.expect( + :get, + EMPromise.resolve("test@parented.example.com"), + ["jmp_customer_jid-testp"] + ) + redis.expect( + :mget, + EMPromise.resolve([nil, nil]), + [ + "jmp_customer_auto_top_up_amount-testp", + "jmp_customer_monthly_overage_limit-testp" ] ) + redis.expect( + :smembers, + EMPromise.resolve([]), + ["jmp_customer_feature_flags-testp"] + ) + CustomerPlan::DB.expect( + :exec_defer, + EMPromise.resolve(nil), + [String, ["testp", "test_usd", "1234"]] + ) result = repo.create("test@parented.example.com").sync assert_kind_of Customer::ChildCustomer, result assert_equal "1234", result.billing_customer_id assert_mock braintree assert_mock braintree_customer assert_mock redis + assert_mock CustomerPlan::DB end em :test_create_parented