diff --git a/lib/registration.rb b/lib/registration.rb index 01e8820a06f662e3133d1384394f9a950bba8bcc..3c78924f1dccdf3631f1e4c2e361f0d0e45c1eef 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 @@ -247,10 +255,13 @@ class Registration def write Command.reply { |reply| - reply.allowed_actions = [:next] + reply.allowed_actions = [:next, :prev] + toob = oob(reply) reply.note_type = :info - reply.note_text = "#{oob(reply).desc}: #{oob(reply).url}" - }.then do + reply.note_text = "#{toob.desc}: #{toob.url}" + }.then do |iq| + next Activation.for(@customer, @tel).then(&:write) if iq.prev? + CreditCard.for(@customer, @tel, finish: @finish).then(&:write) end end @@ -342,12 +353,14 @@ class Registration def write Command.reply { |reply| - reply.allowed_actions = [:next] + reply.allowed_actions = [:next, :prev] add_form(reply) }.then(&method(:parse)) end def parse(iq) + return Activation.for(@customer, @tel).then(&:write) if iq.prev? + guard_too_many_tries.then { verify(iq.form.field("code")&.value&.to_s) }.then { @@ -386,7 +399,9 @@ class Registration class Mail Payment.kinds[:mail] = method(:new) - def initialize(_customer, _tel, final_message: nil, **) + def initialize(customer, tel, final_message: nil, **) + @customer = customer + @tel = tel @final_message = final_message end @@ -412,9 +427,15 @@ class Registration end def write - Command.finish(status: :canceled) do |reply| + Command.reply { |reply| + reply.allowed_actions = [:prev] + reply.status = :canceled reply.command << form - end + }.then { |iq| + raise "Action not allowed" unless iq.prev? + + Activation.for(@customer, @tel).then(&:write) + } end end end diff --git a/lib/tel_selections.rb b/lib/tel_selections.rb index 3006a63a94e760e27ca8f5588593c97df5d4d779..8b309d946da86a119f167b2159c756514910f0ef 100644 --- a/lib/tel_selections.rb +++ b/lib/tel_selections.rb @@ -49,14 +49,16 @@ class TelSelections end def choose_from_list(tns) - if tns.empty? - choose_tel(error: "No numbers found, try another search.") - else - Command.reply { |reply| - reply.allowed_actions = [:next] - reply.command << FormTemplate.render("tn_list", tns: tns) - }.then { |iq| iq.form.field("tel").value.to_s.strip } - end + raise "No numbers found, try another search." if tns.empty? + + Command.reply { |reply| + reply.allowed_actions = [:next, :prev] + reply.command << FormTemplate.render("tn_list", tns: tns) + }.then { |iq| + next choose_tel if iq.prev? + + iq.form.field("tel").value.to_s.strip + } end class AvailableNumber diff --git a/test/test_registration.rb b/test/test_registration.rb index c711411f8963ef911c1d90d87d6aa3b4114aa5cf..d4e3a8f66657cb563d937e19242f1128a6d62af3 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 @@ -391,7 +391,7 @@ class RegistrationTest < Minitest::Test :write, EMPromise.reject(:test_result), [Matching.new do |reply| - assert_equal [:execute, :next], reply.allowed_actions + assert_equal [:execute, :next, :prev], reply.allowed_actions assert_equal( "Add credit card, then return here to continue: " \ "http://creditcard.example.com",