From 039a86fe066840a6b1757b1c690224b872298e05 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 29 Aug 2022 13:52:22 -0500 Subject: [PATCH] Allow prev from Bitcoin registration --- lib/registration.rb | 36 ++++++++++++++++++++++-------------- test/test_registration.rb | 6 +++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/registration.rb b/lib/registration.rb index 9bb03b7d1e20ee22e02aa7ca502b7f1dc346c2c0..e5ad59c1bccfc11fe4d37178ad42ca77039f174c 100644 --- a/lib/registration.rb +++ b/lib/registration.rb @@ -185,7 +185,8 @@ class Registration REDIS.setex("pending_tel_for-#{@customer.jid}", THIRTY_DAYS, tel) end - def note_text(amount, addr) + def note_text(rate, addr) + amount = CONFIG[:activation_amount] / rate <<~NOTE Activate your account by sending at least #{'%.6f' % amount} BTC to #{addr} @@ -195,24 +196,31 @@ class Registration end def write - EMPromise.all([ - addr, - save, - BTC_SELL_PRICES.public_send(@customer.currency.to_s.downcase) - ]).then do |(addr, _, rate)| - min = CONFIG[:activation_amount] / rate - Command.finish( - note_text(min, addr) + @final_message.to_s, status: :canceled - ) + EMPromise.all([addr_and_rate, save]).then do |((addr, rate), _)| + Command.reply { |reply| + reply.allowed_actions = [:prev] + reply.status = :canceled + reply.note_type = :info + reply.note_text = note_text(rate, addr) + @final_message.to_s + }.then(&method(:handle_possible_prev)) end end protected - def addr - @addr ||= @customer.btc_addresses.then { |addrs| - addrs.first || @customer.add_btc_address - } + def handle_possible_prev(iq) + raise "Action not allowed" unless iq.prev? + + Activation.for(@customer, @tel).then(&:write) + end + + def addr_and_rate + EMPromise.all([ + @customer.btc_addresses.then { |addrs| + addrs.first || @customer.add_btc_address + }, + BTC_SELL_PRICES.public_send(@customer.currency.to_s.downcase) + ]) end end diff --git a/test/test_registration.rb b/test/test_registration.rb index c2561d3af146680540921bd6288a86f88f1816f6..e970451820f8031f381faf385b425ed59ea7b440 100644 --- a/test/test_registration.rb +++ b/test/test_registration.rb @@ -337,9 +337,9 @@ class RegistrationTest < Minitest::Test You will receive a notification when your payment is complete. NOTE blather = Minitest::Mock.new - blather.expect( - :<<, - nil, + Command::COMMAND_MANAGER.expect( + :write, + EMPromise.reject(SessionManager::Timeout.new), [Matching.new do |reply| assert_equal :canceled, reply.status assert_equal :info, reply.note_type