Allow prev from Bitcoin registration

Stephen Paul Weber created

Change summary

lib/registration.rb       | 36 ++++++++++++++++++++++--------------
test/test_registration.rb |  6 +++---
2 files changed, 25 insertions(+), 17 deletions(-)

Detailed changes

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
 

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