add new admin command to launch snikket instance for existing customer

SavagePeanut created

Change summary

forms/admin_menu.rb                 |  3 ++-
lib/admin_actions/launch_snikket.rb | 27 +++++++++++++++++++++++++++
lib/admin_command.rb                | 23 +++++++++++++++--------
3 files changed, 44 insertions(+), 9 deletions(-)

Detailed changes

forms/admin_menu.rb 🔗

@@ -24,6 +24,7 @@ field(
 		{ value: "set_trust_level", label: "Set Trust Level" },
 		{ value: "add_invites", label: "Add Referral Codes" },
 		{ value: "number_change", label: "Number Change" },
-		{ value: "add_transaction", label: "Add Transaction" }
+		{ value: "add_transaction", label: "Add Transaction" },
+		{ value: "launch_snikket", label: "Launch Snikket" }
 	]
 )

lib/admin_actions/launch_snikket.rb 🔗

@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+class AdminAction
+	class LaunchSnikket
+		def self.call(customer_id, reply:, snikket_repo:, **)
+			reply.call(
+				FormTemplate.render("snikket_launch")
+			).then { |response|
+				domain = response.form.field("domain").value.to_s
+				IQ_MANAGER.write(Snikket::Launch.new(
+					nil, CONFIG[:snikket_hosting_api], domain: domain
+				)).then do |launched|
+					Snikket::CustomerInstance.for(customer_id, domain, launched)
+				end
+			}.then { |instance| save(snikket_repo, reply, instance) }
+		end
+
+		def self.save(snikket_repo, reply, instance)
+			snikket_repo.put(instance).then do
+				reply.call(FormTemplate.render(
+					"snikket_launched",
+					instance: instance
+				))
+			end
+		end
+	end
+end

lib/admin_command.rb 🔗

@@ -8,6 +8,7 @@ require_relative "admin_actions/financial"
 require_relative "admin_actions/reset_declines"
 require_relative "admin_actions/set_trust_level"
 require_relative "admin_actions/number_change"
+require_relative "admin_actions/launch_snikket"
 require_relative "bill_plan_command"
 require_relative "customer_info_form"
 require_relative "financial_info"
@@ -17,10 +18,11 @@ class AdminCommand
 	def self.for(
 		target_customer,
 		customer_repo,
-		admin_action_repo=AdminActionRepo.new
+		admin_action_repo=AdminActionRepo.new,
+		snikket_repo=Snikket::Repo.new
 	)
 		if target_customer
-			new(target_customer, customer_repo, admin_action_repo)
+			new(target_customer, customer_repo, admin_action_repo, snikket_repo)
 		else
 			NoUser.new(customer_repo, admin_action_repo, notice: "Customer Not Found")
 		end
@@ -51,11 +53,13 @@ class AdminCommand
 	def initialize(
 		target_customer,
 		customer_repo,
-		admin_action_repo=AdminActionRepo.new
+		admin_action_repo=AdminActionRepo.new,
+		snikket_repo=Snikket::Repo.new
 	)
 		@target_customer = target_customer
 		@customer_repo = customer_repo
 		@admin_action_repo = admin_action_repo
+		@snikket_repo = snikket_repo
 	end
 
 	def start(command_action=:execute)
@@ -98,7 +102,7 @@ class AdminCommand
 	def new_context(q, command_action=:execute)
 		CustomerInfoForm.new(@customer_repo)
 			.parse_something(q).then do |new_customer|
-				AdminCommand.for(new_customer, @customer_repo, @admin_action_repo)
+				AdminCommand.for(new_customer, @customer_repo, @admin_action_repo, @snikket_repo)
 					.then { |ac| ac.start(command_action) }
 			end
 	end
@@ -142,11 +146,12 @@ class AdminCommand
 			@klass = klass
 		end
 
-		def call(customer_id, customer_repo:, **)
+		def call(customer_id, customer_repo:, snikket_repo:, **)
 			@klass.call(
 				customer_id,
 				reply: method(:reply),
-				customer_repo: customer_repo
+				customer_repo: customer_repo,
+				snikket_repo: snikket_repo
 			).then { nil }
 		end
 
@@ -180,13 +185,15 @@ class AdminCommand
 		[:set_trust_level, Undoable.new(AdminAction::SetTrustLevel::Command)],
 		[:add_invites, Undoable.new(AdminAction::AddInvites::Command)],
 		[:number_change, Undoable.new(AdminAction::NumberChange::Command)],
-		[:add_transaction, Undoable.new(AdminAction::AddTransaction::Command)]
+		[:add_transaction, Undoable.new(AdminAction::AddTransaction::Command)],
+		[:launch_snikket, Simple.new(AdminAction::LaunchSnikket)]
 	].each do |action, handler|
 		define_method("action_#{action}") do
 			handler.call(
 				@target_customer,
 				admin_action_repo: @admin_action_repo,
-				customer_repo: @customer_repo
+				customer_repo: @customer_repo,
+				snikket_repo: @snikket_repo
 			)
 		end
 	end