~singpolyma/sgx-jmp

eb16aef36a7b29aa9ef1ebbc2a07f60dc5648784 — root21 4 months ago 30485ca
Added number change Admin command to bot.

V3 commit.

	modified:   forms/admin_menu.rb
	new file:   forms/admin_number_change.rb
	new file:   lib/admin_actions/number_change.rb
	modified:   lib/admin_command.rb
M forms/admin_menu.rb => forms/admin_menu.rb +2 -1
@@ 22,6 22,7 @@ field(
		{ value: "undo", label: "Undo" },
		{ value: "reset_declines", label: "Reset Declines" },
		{ value: "set_trust_level", label: "Set Trust Level" },
		{ value: "add_invites", label: "Add Invites" }
		{ value: "add_invites", label: "Add Invites" },
		{ value: "number_change", label: "Number Change" }
	]
)

A forms/admin_number_change.rb => forms/admin_number_change.rb +10 -0
@@ 0,0 1,10 @@
form!
instructions "Number Change"

field(
	var: "new_tel",
	type: "text-single",
	datatype: "html:tel",
	label: "Number to change to?",
	value: ""
)

A lib/admin_actions/number_change.rb => lib/admin_actions/number_change.rb +98 -0
@@ 0,0 1,98 @@
# frozen_string_literal: true

require "value_semantics/monkey_patched"
require_relative "../admin_action"
require_relative "../form_to_h"

class AdminAction
	class NumberChange < AdminAction
		include Isomorphic
		class Command
			using FormToH

			def self.for(target_customer, reply:)
				EMPromise.resolve(
					new(
						customer_id: target_customer.customer_id,
						old_tel: target_customer.registered?.phone
					)
				).then { |x|
					reply.call(x.form).then(&x.method(:change))
				}
			end

			def initialize(**bag)
				@bag = bag
			end

			def form
				FormTemplate.render("admin_number_change")
			end

			def change(result)
				AdminAction::NumberChange.for(
					**@bag,
					**result.form.to_h
						.reject { |_k, v| v == "nil" }.transform_keys(&:to_sym)
				)
			end
		end

		NilKey = Struct.new(:key) {
			def to_s
			 "Expected a key with a value, but #{key} has no value."
			end
		}

		def customer_id
			@attributes[:customer_id]
		end

		def old_tel
			@attributes[:old_tel]
		end

		def new_tel
			@attributes[:new_tel]
		end

		def check_forward
			EMPromise.all([
				check_noop,
				check_exist
			])
		end

		def forward
			sgx = TrivialBackendSgxRepo.new.get(customer_id)
			EMPromise.all([
				REDIS.rename("catapult_fwd-#{old_tel}", "catapult_fwd-#{new_tel}"),
				sgx.register!(new_tel)
			]).then { self }
		end

		def to_reverse
			with(
			 old_tel: new_tel,
			 new_tel: old_tel
			)
		end

		def to_s
			"number_change(#{customer_id}): #{old_tel} -> #{new_tel}"
		end

	protected

		def check_noop
		 EMPromise.reject(NoOp.new) if new_tel == old_tel
		end

		def check_exist
			cat_jid = "catapult_jid-#{old_tel}"
			REDIS.exists(cat_jid).then { |v|
				EMPromise.reject(NilKey.new(cat_jid)) unless v == 1
			}
		end
	end
end

M lib/admin_command.rb => lib/admin_command.rb +3 -1
@@ 6,6 6,7 @@ require_relative "admin_actions/cancel"
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 "bill_plan_command"
require_relative "customer_info_form"
require_relative "financial_info"


@@ 176,7 177,8 @@ class AdminCommand
		[:undo, Undoable.new(Undo)],
		[:reset_declines, Undoable.new(AdminAction::ResetDeclines::Command)],
		[:set_trust_level, Undoable.new(AdminAction::SetTrustLevel::Command)],
		[:add_invites, Undoable.new(AdminAction::AddInvites::Command)]
		[:add_invites, Undoable.new(AdminAction::AddInvites::Command)],
		[:number_change, Undoable.new(AdminAction::NumberChange::Command)]
	].each do |action, handler|
		define_method("action_#{action}") do
			handler.call(