Move Cancel and Financial Admin Commands

Christopher Vollick created

This class gets pretty tight later, so I have to move these out to make
room in the class for the other commands.

This Simple class is a bit weird on its own here, but it makes a bit
more sense later when compared to another class.

Change summary

lib/admin_actions/cancel.rb    | 18 +++++++++
lib/admin_actions/financial.rb | 45 ++++++++++++++++++++++
lib/admin_command.rb           | 72 ++++++++++++++++--------------------
lib/financial_info.rb          |  2 +
4 files changed, 97 insertions(+), 40 deletions(-)

Detailed changes

lib/admin_actions/cancel.rb 🔗

@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class AdminAction
+	class CancelCustomer
+		def self.call(customer, customer_repo:, **)
+			m = Blather::Stanza::Message.new
+			m.from = CONFIG[:notify_from]
+			m.body = "Your JMP account has been cancelled."
+			customer.stanza_to(m).then {
+				EMPromise.all([
+					customer.stanza_to(Blather::Stanza::Iq::IBR.new(:set).tap(&:remove!)),
+					customer.deregister!,
+					customer_repo.disconnect_tel(customer)
+				])
+			}
+		end
+	end
+end

lib/admin_actions/financial.rb 🔗

@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+require_relative "../admin_action"
+require_relative "../financial_info"
+require_relative "../form_template"
+
+class AdminAction
+	class Financial
+		def self.call(customer_id, reply:, **)
+			new(customer_id, reply: reply).call
+		end
+
+		def initialize(customer_id, reply:)
+			@customer_id = customer_id
+			@reply = reply
+		end
+
+		def call
+			AdminFinancialInfo.for(@customer_id).then do |financial_info|
+				@reply.call(FormTemplate.render(
+					"admin_financial_info",
+					info: financial_info
+				)).then {
+					pay_methods(financial_info)
+				}.then {
+					transactions(financial_info)
+				}
+			end
+		end
+
+		def pay_methods(financial_info)
+			@reply.call(FormTemplate.render(
+				"admin_payment_methods",
+				**financial_info.to_h
+			))
+		end
+
+		def transactions(financial_info)
+			@reply.call(FormTemplate.render(
+				"admin_transaction_list",
+				transactions: financial_info.transactions
+			))
+		end
+	end
+end

lib/admin_command.rb 🔗

@@ -1,5 +1,7 @@
 # frozen_string_literal: true
 
+require_relative "admin_actions/cancel"
+require_relative "admin_actions/financial"
 require_relative "bill_plan_command"
 require_relative "customer_info_form"
 require_relative "financial_info"
@@ -83,53 +85,43 @@ class AdminCommand
 		new_context(@target_customer.customer_id)
 	end
 
-	def action_financial
-		AdminFinancialInfo.for(@target_customer).then do |financial_info|
-			reply(FormTemplate.render(
-				"admin_financial_info",
-				info: financial_info
-			)).then {
-				pay_methods(financial_info)
-			}.then {
-				transactions(financial_info)
-			}
-		end
-	end
-
 	def action_bill_plan
 		BillPlanCommand.for(@target_customer).call
 	end
 
-	def notify_customer(body)
-		m = Blather::Stanza::Message.new
-		m.from = CONFIG[:notify_from]
-		m.body = body
-		@target_customer.stanza_to(m)
-	end
+	class Simple
+		def initialize(klass)
+			@klass = klass
+		end
 
-	def action_cancel_account
-		notify_customer("Your JMP account has been cancelled.").then {
-			EMPromise.all([
-				@target_customer.stanza_to(
-					Blather::Stanza::Iq::IBR.new(:set).tap(&:remove!)
-				),
-				@target_customer.deregister!,
-				@customer_repo.disconnect_tel(@target_customer)
-			])
-		}
-	end
+		def call(customer_id, customer_repo:, **)
+			@klass.call(
+				customer_id,
+				reply: method(:reply),
+				customer_repo: customer_repo
+			)
+		end
 
-	def pay_methods(financial_info)
-		reply(FormTemplate.render(
-			"admin_payment_methods",
-			**financial_info.to_h
-		))
+		def reply(form=nil, note_type: nil, note_text: nil)
+			Command.reply { |reply|
+				reply.allowed_actions = [:next, :complete]
+				reply.command << form if form
+				reply.note_type = note_type if note_type
+				reply.note_text = note_text if note_text
+			}
+		end
 	end
 
-	def transactions(financial_info)
-		reply(FormTemplate.render(
-			"admin_transaction_list",
-			transactions: financial_info.transactions
-		))
+	[
+		[:cancel_account, Simple.new(AdminAction::CancelCustomer)],
+		[:financial, Simple.new(AdminAction::Financial)]
+	].each do |action, handler|
+		define_method("action_#{action}") do
+			handler.call(
+				@target_customer,
+				admin_action_repo: @admin_action_repo,
+				customer_repo: @customer_repo
+			)
+		end
 	end
 end

lib/financial_info.rb 🔗

@@ -1,6 +1,8 @@
 # frozen_string_literal: true
 
 require "value_semantics/monkey_patched"
+require_relative "customer_finacials"
+require_relative "payment_methods"
 
 class AdminFinancialInfo
 	value_semantics do