diff --git a/forms/jid_switch.rb b/forms/jid_switch.rb new file mode 100644 index 0000000000000000000000000000000000000000..e21bcf9d0a931b733df7559ea87355dd9dd299b5 --- /dev/null +++ b/forms/jid_switch.rb @@ -0,0 +1,8 @@ +form! +title "JID Switch" + +field( + var: "jid", + type: "jid-single", + label: "JID to Switch To" +) diff --git a/lib/customer_repo.rb b/lib/customer_repo.rb index e9070945a9d8665d52262bf84edfb745bb482308..8858d73d18c275b44c85b45b128eccb9f7fd0e2b 100644 --- a/lib/customer_repo.rb +++ b/lib/customer_repo.rb @@ -125,6 +125,29 @@ class CustomerRepo @redis.set(k, limit) end + def change_jid(customer, new_jid) + @redis.set("jmp_customer_id-#{new_jid}", customer.customer_id).then { + @redis.set("jmp_customer_jid-#{customer.customer_id}", new_jid) + }.then { + SwapDefaultFwd.new.do(self, customer, new_jid) + }.then do + @redis.del("jmp_customer_id-#{customer.jid}") + end + end + + # I've put this here to hide the lines from rubocop + # After we sort out where call routing should live, this whole process will + # no longer be necessary + class SwapDefaultFwd + def do(repo, customer, new_jid) + unless customer.fwd.uri == "xmpp:#{customer.jid}" + return EMPromise.resolve(nil) + end + + repo.put_fwd(customer, customer.fwd.with(uri: "xmpp:#{new_jid}")) + end + end + protected def new_sgx(customer_id) diff --git a/sgx_jmp.rb b/sgx_jmp.rb index f6ab1058c4617f9ae2cecfa86fa119c9d75108e7..5cc1d1da03d39d9f53a8be967e623f80c9797053 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -760,6 +760,35 @@ def reply_with_note(iq, text, type: :info) self << reply end +Command.new( + "https://ns.cheogram.com/sgx/jid-switch", + "Change JID", + list_for: ->(customer: nil, **) { customer }, + customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new) +) { + Command.customer.then { |customer| + Command.reply { |reply| + reply.command << FormTemplate.render("jid_switch") + }.then { |response| + new_jid = response.form.field("jid").value + repo = Command.execution.customer_repo + repo.find_by_jid(new_jid) + .catch_only(CustomerRepo::NotFound) { nil } + .then { |cust| + next EMPromise.reject("Customer Already Exists") if cust + + repo.change_jid(customer, new_jid) + } + }.then { + StatsD.increment("changejid.completed") + Command.finish { |reply| + reply.note_type = :info + reply.note_text = "Customer JID Changed" + } + } + } +}.register(self).then(&CommandList.method(:register)) + Command.new( "web-register", "Initiate Register from Web",