From eb16aef36a7b29aa9ef1ebbc2a07f60dc5648784 Mon Sep 17 00:00:00 2001 From: root21 Date: Mon, 19 Sep 2022 10:20:53 -0600 Subject: [PATCH] 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 --- forms/admin_menu.rb | 3 +- forms/admin_number_change.rb | 10 +++ lib/admin_actions/number_change.rb | 98 ++++++++++++++++++++++++++++++ lib/admin_command.rb | 4 +- 4 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 forms/admin_number_change.rb create mode 100644 lib/admin_actions/number_change.rb diff --git a/forms/admin_menu.rb b/forms/admin_menu.rb index bb963bc..180a0bc 100644 --- a/forms/admin_menu.rb +++ b/forms/admin_menu.rb @@ -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" } ] ) diff --git a/forms/admin_number_change.rb b/forms/admin_number_change.rb new file mode 100644 index 0000000..0f695e9 --- /dev/null +++ b/forms/admin_number_change.rb @@ -0,0 +1,10 @@ +form! +instructions "Number Change" + +field( + var: "new_tel", + type: "text-single", + datatype: "html:tel", + label: "Number to change to?", + value: "" +) diff --git a/lib/admin_actions/number_change.rb b/lib/admin_actions/number_change.rb new file mode 100644 index 0000000..2c830ea --- /dev/null +++ b/lib/admin_actions/number_change.rb @@ -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 diff --git a/lib/admin_command.rb b/lib/admin_command.rb index 6359641..4fbce24 100644 --- a/lib/admin_command.rb +++ b/lib/admin_command.rb @@ -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( -- 2.45.2