Disconnect Number on Swap

Christopher Vollick created

I decided that when undoing this action I shouldn't delete the new
number automatically, and when redoing I probably shouldn't re-delete
the new number.

This situations are weird, and so the human should probably think
criticially here.

So what I do instead is only delete the number the first time the action
gets run, and then in the other two cases I warn the operator that I
didn't delete anything so they can go figure stuff out.

I put this `first_time?` logic in the parent because I already know of
another place where I'm planning on using it in a different PR.

Change summary

forms/admin_number_change.rb       |  7 +++++++
lib/admin_action.rb                |  7 +++++++
lib/admin_actions/number_change.rb | 25 +++++++++++++++++++++++--
3 files changed, 37 insertions(+), 2 deletions(-)

Detailed changes

forms/admin_number_change.rb 🔗

@@ -8,3 +8,10 @@ field(
 	label: "Number to change to?",
 	value: ""
 )
+
+field(
+	var: "should_delete",
+	type: "boolean",
+	label: "Should we delete the old number?",
+	value: 0
+)

lib/admin_action.rb 🔗

@@ -99,6 +99,13 @@ class AdminAction
 		@attributes[:parent_id]
 	end
 
+	# This tells us if this is the first time we're running this command.
+	# This can be used by actions which take destructive or annoying actions
+	# and don't want to redo those parts on an undo or redo.
+	def first_time?
+		!parent_id
+	end
+
 	def actor_id
 		@attributes[:actor_id]
 	end

lib/admin_actions/number_change.rb 🔗

@@ -56,6 +56,10 @@ class AdminAction
 			@attributes[:new_tel]
 		end
 
+		def should_delete?
+			["1", "true"].include?(@attributes[:should_delete])
+		end
+
 		def check_forward
 			EMPromise.all([
 				check_noop,
@@ -67,7 +71,8 @@ class AdminAction
 			sgx = TrivialBackendSgxRepo.new.get(customer_id)
 			EMPromise.all([
 				REDIS.rename("catapult_fwd-#{old_tel}", "catapult_fwd-#{new_tel}"),
-				sgx.register!(new_tel)
+				sgx.register!(new_tel),
+				should_delete? && first_time? ? disconnect_number : nil
 			]).then { self }
 		end
 
@@ -79,11 +84,27 @@ class AdminAction
 		end
 
 		def to_s
-			"number_change(#{customer_id}): #{old_tel} -> #{new_tel}"
+			"number_change(#{customer_id}): #{old_tel} -> #{new_tel}#{delete_warning}"
 		end
 
 	protected
 
+		def disconnect_number
+			# Order name is limited to 40 characters
+			# Assuming 12 chars for new_tel and 12 for customer_id, this is tight
+			# but ok
+			BandwidthTnRepo.new.disconnect(
+				old_tel,
+				"cust #{customer_id} swap to #{new_tel}"
+			)
+		end
+
+		def delete_warning
+			return "" unless should_delete? && !first_time?
+
+			" * NOT DELETING"
+		end
+
 		def check_noop
 			EMPromise.reject(NoOp.new) if new_tel == old_tel
 		end