sgx-jmp
created pr with
sgx-jmp/resume-orders.1
commented
lgtm
cmds
checkout latest patchset:
ssh pr.pico.sh pull sgx-jmp:resume-orders | git am -3checkout specific patchset revision:
ssh pr.pico.sh pull sgx-jmp:resume-orders [rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh sgx-jmp:resume-ordersadd comment to patch request:
ssh pr.pico.sh comment sgx-jmp:resume-orders "lgtm!"
Patchset
sgx-jmp/resume-orders.1
Resume in progress order if there is one
Stephen Paul Weber
2026-11-23T18:28:34ZIf an order is made but then the user reset the command or the app or whatever, don't start over but just finish the existing order and setup.
Diff stat
7 files changed, 212 insertions(+), 8 deletions(-)
Semantic diff summary
0 added,
0 modified,
0 signature changed,
0 removed
across 0 analyzed files
(7 files skipped: unsupported file type)
+25
-0
lib/bandwidth_tn_order.rb
#
| ... | ... | @@ -68,6 +68,31 @@ class BandwidthTNOrder | |
| 68 | 68 | raise "Unknown order status: #{status}" | |
| 69 | 69 | end | |
| 70 | 70 | ||
| 71 | + | class Resume | |
| 72 | + | attr_reader :tel | |
| 73 | + | ||
| 74 | + | def initialize(order_id) | |
| 75 | + | @order_id = order_id | |
| 76 | + | end | |
| 77 | + | ||
| 78 | + | def price | |
| 79 | + | 0 | |
| 80 | + | end | |
| 81 | + | ||
| 82 | + | def charge(*); end | |
| 83 | + | ||
| 84 | + | def order(_, customer) | |
| 85 | + | BandwidthTNOrder.get(@order_id).then(&:poll).then { |order| | |
| 86 | + | @tel = order.tel | |
| 87 | + | customer | |
| 88 | + | } | |
| 89 | + | end | |
| 90 | + | ||
| 91 | + | def to_s | |
| 92 | + | @tel ? @tel.to_s : "selected" | |
| 93 | + | end | |
| 94 | + | end | |
| 95 | + | ||
| 71 | 96 | class Received < BandwidthTNOrder | |
| 72 | 97 | def status | |
| 73 | 98 | :received |
+29
-0
lib/bandwidth_tn_order_repo.rb
#
| ... | ... | @@ -0,0 +1,29 @@ | |
| 1 | + | # frozen_string_literal: true | |
| 2 | + | ||
| 3 | + | class BandwidthTnOrderRepo | |
| 4 | + | TEN_MINUTES = 60 * 10 | |
| 5 | + | ||
| 6 | + | def initialize(redis: REDIS) | |
| 7 | + | @redis = redis | |
| 8 | + | end | |
| 9 | + | ||
| 10 | + | def set(customer, order_id) | |
| 11 | + | @redis.setex(key(customer), TEN_MINUTES, order_id) | |
| 12 | + | end | |
| 13 | + | ||
| 14 | + | def get(customer) | |
| 15 | + | @redis.get(key(customer)).then do |order_id| | |
| 16 | + | next unless order_id | |
| 17 | + | ||
| 18 | + | BandwidthTNOrder::Resume.new(order_id) | |
| 19 | + | end | |
| 20 | + | end | |
| 21 | + | ||
| 22 | + | def delete(customer) | |
| 23 | + | @redis.del(key(customer)) | |
| 24 | + | end | |
| 25 | + | ||
| 26 | + | def key(customer) | |
| 27 | + | "jmp_customer_tn_order-#{customer.customer_id}" | |
| 28 | + | end | |
| 29 | + | end |
+17
-7
lib/registration.rb
#
| ... | ... | @@ -122,10 +123,18 @@ class Registration | |
| 122 | 123 | if (reg = customer.registered?) | |
| 123 | 124 | Registered.for(customer, reg.phone) | |
| 124 | 125 | else | |
| 125 | - | tel_selections[customer].then(&:choose_tel_or_data).then do |product| | |
| 126 | - | reserve_and_continue(tel_selections, customer, product).then do | |
| 127 | - | RegistrationType.for(customer, google_play_userid, product) | |
| 128 | - | end | |
| 126 | + | BandwidthTnOrderRepo.new.get(customer).then do |resume| | |
| 127 | + | next Finish.new(customer, resume) if resume | |
| 128 | + | ||
| 129 | + | start_registration(customer, google_play_userid, tel_selections) | |
| 130 | + | end | |
| 131 | + | end | |
| 132 | + | end | |
| 133 | + | ||
| 134 | + | def self.start_registration(customer, google_play_userid, tel_selections) | |
| 135 | + | tel_selections[customer].then(&:choose_tel_or_data).then do |product| | |
| 136 | + | reserve_and_continue(tel_selections, customer, product).then do | |
| 137 | + | RegistrationType.for(customer, google_play_userid, product) | |
| 129 | 138 | end | |
| 130 | 139 | end | |
| 131 | 140 | end |
| ... | ... | @@ -752,7 +761,8 @@ class Registration | |
| 752 | 761 | ||
| 753 | 762 | def number_purchase_error(e) | |
| 754 | 763 | Command.log.error "number_purchase_error", e | |
| 755 | - | TEL_SELECTIONS.delete(@customer.jid) | |
| 764 | + | BandwidthTnOrderRepo.new.delete(@customer) | |
| 765 | + | .then { TEL_SELECTIONS.delete(@customer.jid) } | |
| 756 | 766 | .then { TEL_SELECTIONS[@customer] } | |
| 757 | 767 | .then { |c| | |
| 758 | 768 | c.choose_tel_or_data( |
| ... | ... | @@ -798,6 +807,7 @@ class Registration | |
| 798 | 807 | customer.register!(@tel.tel).catch(&method(:raise_setup_error)).then { | |
| 799 | 808 | EMPromise.all([ | |
| 800 | 809 | TEL_SELECTIONS.delete(customer.jid), | |
| 810 | + | BandwidthTnOrderRepo.new.delete(customer), | |
| 801 | 811 | put_default_fwd, | |
| 802 | 812 | use_referral_code, | |
| 803 | 813 | @tel.charge(customer) |
+4
-1
lib/tel_selections.rb
#
| ... | ... | @@ -326,13 +326,16 @@ class TelSelections | |
| 326 | 326 | end | |
| 327 | 327 | ||
| 328 | 328 | def order(_, customer) | |
| 329 | + | repo = BandwidthTnOrderRepo.new | |
| 329 | 330 | BandwidthTnReservationRepo.new.get(customer, tel).then { |rid| | |
| 330 | 331 | BandwidthTNOrder.create( | |
| 331 | 332 | tel, | |
| 332 | 333 | customer_order_id: customer.customer_id, | |
| 333 | 334 | reservation_id: rid | |
| 334 | 335 | ).then(&:poll) | |
| 335 | - | }.then { customer } | |
| 336 | + | }.then { |order| | |
| 337 | + | repo.set(customer, order.id).then { customer } | |
| 338 | + | } | |
| 336 | 339 | end | |
| 337 | 340 | end | |
| 338 | 341 |
+28
-0
test/test_bandwidth_tn_order.rb
#
| ... | ... | @@ -93,6 +93,34 @@ class BandwidthTNOrderTest < Minitest::Test | |
| 93 | 93 | end | |
| 94 | 94 | em :test_poll | |
| 95 | 95 | ||
| 96 | + | class TestResume < Minitest::Test | |
| 97 | + | def test_order | |
| 98 | + | req = stub_request( | |
| 99 | + | :get, | |
| 100 | + | "https://dashboard.bandwidth.com/v1.0/accounts//orders/oid" | |
| 101 | + | ).to_return(status: 200, body: <<~RESPONSE) | |
| 102 | + | <OrderResponse> | |
| 103 | + | <OrderStatus>COMPLETE</OrderStatus> | |
| 104 | + | <CompletedNumbers> | |
| 105 | + | <TelephoneNumber> | |
| 106 | + | <FullNumber>5555550000</FullNumber> | |
| 107 | + | </TelephoneNumber> | |
| 108 | + | </CompletedNumbers> | |
| 109 | + | </OrderResponse> | |
| 110 | + | RESPONSE | |
| 111 | + | customer = Object.new | |
| 112 | + | resume = BandwidthTNOrder::Resume.new("oid") | |
| 113 | + | assert_equal customer, resume.order(nil, customer).sync | |
| 114 | + | assert_equal "+15555550000", resume.tel | |
| 115 | + | assert_requested req | |
| 116 | + | end | |
| 117 | + | em :test_order | |
| 118 | + | ||
| 119 | + | def test_price | |
| 120 | + | assert_equal 0, BandwidthTNOrder::Resume.new("oid").price | |
| 121 | + | end | |
| 122 | + | end | |
| 123 | + | ||
| 96 | 124 | class TestReceived < Minitest::Test | |
| 97 | 125 | def setup | |
| 98 | 126 | @order = BandwidthTNOrder::Received.new( |
+39
-0
test/test_registration.rb
#
| ... | ... | @@ -10,12 +10,14 @@ require "sim_order" | |
| 10 | 10 | require "statsd" | |
| 11 | 11 | ||
| 12 | 12 | BandwidthTnReservationRepo::REDIS = FakeRedis.new | |
| 13 | + | BandwidthTnOrderRepo::REDIS = FakeRedis.new | |
| 13 | 14 | Snikket::REDIS = FakeRedis.new | |
| 14 | 15 | Snikket::IQ_MANAGER = Minitest::Mock.new | |
| 15 | 16 | ||
| 16 | 17 | class RegistrationTest < Minitest::Test | |
| 17 | 18 | def teardown | |
| 18 | 19 | BandwidthTnReservationRepo::REDIS.reset! | |
| 20 | + | BandwidthTnOrderRepo::REDIS.reset! | |
| 19 | 21 | WebMock.reset! | |
| 20 | 22 | end | |
| 21 | 23 |
| ... | ... | @@ -80,6 +82,27 @@ class RegistrationTest < Minitest::Test | |
| 80 | 82 | end | |
| 81 | 83 | em :test_for_activated | |
| 82 | 84 | ||
| 85 | + | def test_for_resumes_order | |
| 86 | + | web_manager = TelSelections.new( | |
| 87 | + | redis: FakeRedis.new, db: FakeDB.new, memcache: FakeMemcache.new | |
| 88 | + | ) | |
| 89 | + | result = execute_command do | |
| 90 | + | cust = customer(sgx: OpenStruct.new(registered?: false)) | |
| 91 | + | BandwidthTnOrderRepo.new.set(cust, "test_order").sync | |
| 92 | + | Registration.for(cust, nil, web_manager) | |
| 93 | + | end | |
| 94 | + | assert_kind_of Registration::Finish, result | |
| 95 | + | assert_not_requested( | |
| 96 | + | :post, | |
| 97 | + | "https://dashboard.bandwidth.com/v1.0/accounts//tnreservation" | |
| 98 | + | ) | |
| 99 | + | assert_not_requested( | |
| 100 | + | :post, | |
| 101 | + | "https://dashboard.bandwidth.com/v1.0/accounts//orders" | |
| 102 | + | ) | |
| 103 | + | end | |
| 104 | + | em :test_for_resumes_order | |
| 105 | + | ||
| 83 | 106 | def test_for_not_activated_approved | |
| 84 | 107 | sgx = OpenStruct.new(registered?: false) | |
| 85 | 108 | web_manager = TelSelections.new( |
| ... | ... | @@ -1391,6 +1414,7 @@ class RegistrationTest < Minitest::Test | |
| 1391 | 1414 | Transaction::DB = Minitest::Mock.new | |
| 1392 | 1415 | ||
| 1393 | 1416 | def setup | |
| 1417 | + | BandwidthTnOrderRepo::REDIS.reset! | |
| 1394 | 1418 | @sgx = Minitest::Mock.new(mksgx) | |
| 1395 | 1419 | iq = Blather::Stanza::Iq::Command.new | |
| 1396 | 1420 | iq.from = "test\\40example.com@cheogram.com" |
| ... | ... | @@ -1497,6 +1526,11 @@ class RegistrationTest < Minitest::Test | |
| 1497 | 1526 | @finish.write | |
| 1498 | 1527 | end | |
| 1499 | 1528 | assert_requested create_order | |
| 1529 | + | assert_nil( | |
| 1530 | + | BandwidthTnOrderRepo.new.get( | |
| 1531 | + | OpenStruct.new(customer_id: "test") | |
| 1532 | + | ).sync | |
| 1533 | + | ) | |
| 1500 | 1534 | assert_mock @sgx | |
| 1501 | 1535 | assert_mock Registration::Finish::REDIS | |
| 1502 | 1536 | assert_mock Bwmsgsv2Repo::REDIS |
| ... | ... | @@ -2102,6 +2136,11 @@ class RegistrationTest < Minitest::Test | |
| 2102 | 2136 | ] | |
| 2103 | 2137 | ) | |
| 2104 | 2138 | assert_requested create_order | |
| 2139 | + | assert_nil( | |
| 2140 | + | BandwidthTnOrderRepo.new.get( | |
| 2141 | + | OpenStruct.new(customer_id: "test") | |
| 2142 | + | ).sync | |
| 2143 | + | ) | |
| 2105 | 2144 | end | |
| 2106 | 2145 | em :test_write_tn_fail | |
| 2107 | 2146 |
+70
-0
test/test_tel_selections.rb
#
| ... | ... | @@ -208,6 +211,73 @@ class TelSelectionsTest < Minitest::Test | |
| 208 | 211 | em :test_local_inventory_whole_number_query | |
| 209 | 212 | end | |
| 210 | 213 | ||
| 214 | + | class BandwidthTnTest < Minitest::Test | |
| 215 | + | BandwidthTnOrderRepo::REDIS = FakeRedis.new | |
| 216 | + | BandwidthTnReservationRepo::REDIS = FakeRedis.new | |
| 217 | + | ||
| 218 | + | def setup | |
| 219 | + | BandwidthTnOrderRepo::REDIS.reset! | |
| 220 | + | BandwidthTnReservationRepo::REDIS.reset! | |
| 221 | + | @customer = OpenStruct.new(customer_id: "test") | |
| 222 | + | @tn = TelSelections::ChooseTel::Tn::Bandwidth.new( | |
| 223 | + | TelSelections::ChooseTel::Tn.new("+15555550000") | |
| 224 | + | ) | |
| 225 | + | end | |
| 226 | + | ||
| 227 | + | def teardown | |
| 228 | + | BandwidthTnOrderRepo::REDIS.reset! | |
| 229 | + | BandwidthTnReservationRepo::REDIS.reset! | |
| 230 | + | WebMock.reset! | |
| 231 | + | end | |
| 232 | + | ||
| 233 | + | def test_order_repo_get_without_order | |
| 234 | + | assert_nil BandwidthTnOrderRepo.new.get(@customer).sync | |
| 235 | + | end | |
| 236 | + | em :test_order_repo_get_without_order | |
| 237 | + | ||
| 238 | + | def test_order_repo_get_with_order | |
| 239 | + | BandwidthTnOrderRepo.new.set(@customer, "order-id").sync | |
| 240 | + | assert_kind_of( | |
| 241 | + | BandwidthTNOrder::Resume, | |
| 242 | + | BandwidthTnOrderRepo.new.get(@customer).sync | |
| 243 | + | ) | |
| 244 | + | end | |
| 245 | + | em :test_order_repo_get_with_order | |
| 246 | + | ||
| 247 | + | def test_order_stores_order_id | |
| 248 | + | stub_request( | |
| 249 | + | :post, | |
| 250 | + | "https://dashboard.bandwidth.com/v1.0/accounts//orders" | |
| 251 | + | ).to_return(status: 200, body: <<~RESPONSE) | |
| 252 | + | <OrderResponse> | |
| 253 | + | <Order> | |
| 254 | + | <id>order-id</id> | |
| 255 | + | </Order> | |
| 256 | + | </OrderResponse> | |
| 257 | + | RESPONSE | |
| 258 | + | stub_request( | |
| 259 | + | :get, | |
| 260 | + | "https://dashboard.bandwidth.com/v1.0/accounts//orders/order-id" | |
| 261 | + | ).to_return(status: 200, body: <<~RESPONSE) | |
| 262 | + | <OrderResponse> | |
| 263 | + | <OrderStatus>COMPLETE</OrderStatus> | |
| 264 | + | <CompletedNumbers> | |
| 265 | + | <TelephoneNumber> | |
| 266 | + | <FullNumber>5555550000</FullNumber> | |
| 267 | + | </TelephoneNumber> | |
| 268 | + | </CompletedNumbers> | |
| 269 | + | </OrderResponse> | |
| 270 | + | RESPONSE | |
| 271 | + | result = @tn.order(FakeDB.new, @customer).sync | |
| 272 | + | assert_equal @customer, result | |
| 273 | + | assert_kind_of( | |
| 274 | + | BandwidthTNOrder::Resume, | |
| 275 | + | BandwidthTnOrderRepo.new.get(@customer).sync | |
| 276 | + | ) | |
| 277 | + | end | |
| 278 | + | em :test_order_stores_order_id | |
| 279 | + | end | |
| 280 | + | ||
| 211 | 281 | class TnOptionTest < Minitest::Test | |
| 212 | 282 | def setup | |
| 213 | 283 | @tn = TelSelections::ChooseTel::Tn::Option.new( |