diff --git a/forms/subaccount.rb b/forms/subaccount.rb new file mode 100644 index 0000000000000000000000000000000000000000..4ad9751c836239dde12e253a8b72dc3da8a059f2 --- /dev/null +++ b/forms/subaccount.rb @@ -0,0 +1,31 @@ +result! + +title "Create Subaccount" + +instructions( + "To create a new phone number linked to your account balance, " \ + "you can sign up for JMP using a new Jabber ID and the referral code below." +) + +field( + type: "text-single", + var: "code", + label: "Referral Code", + value: @code +) + +if @cheogram + field( + type: "text-single", + datatype: "xs:anyURI", + label: "Watch this guide for creating an account using a snikket instance", + value: "https://kumi.tube/w/jzYKJzdhtdBfPbFCH2VnDZ?start=14s" + ) + + field( + type: "text-single", + datatype: "xs:anyURI", + label: "Watch this guide for creating an account using a public server", + value: "https://kumi.tube/w/eKG2zVY8kU9wJR3PKqM7kp?start=14s" + ) +end diff --git a/lib/parent_code_repo.rb b/lib/parent_code_repo.rb index beff9ac20da880d33145939e31caf1d15064bc51..4cc4d7d235aceffe86be69f847da2b676870246b 100644 --- a/lib/parent_code_repo.rb +++ b/lib/parent_code_repo.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require "multibases" +require "securerandom" + class ParentCodeRepo def initialize(redis=REDIS) @redis = redis @@ -8,4 +11,16 @@ class ParentCodeRepo def find(code) @redis.hget("jmp_parent_codes", code) end + + def find_or_create(customer_id) + @redis.get("jmp_customer_parent_code-#{customer_id}").then do |code| + next code if code + + code = "p#{Multibases.pack('base32upper', SecureRandom.bytes(4))}" + EMPromise.all([ + @redis.set("jmp_customer_group_code-#{customer_id}", code), + @redis.hset("jmp_group_codes", code, customer_id) + ]).then { code } + end + end end diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 2fc11dee6805ed8f81694a58b665072b684530f9..ae6956631b51deacd893f296f444bc0b8963f9d2 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -768,6 +768,26 @@ Command.new( end }.register(self).then(&CommandList.method(:register)) +Command.new( + "subaccount", + "➕️ Create a new phone number linked to this balance", + list_for: lambda do |customer:, **| + !!customer&.currency && + customer&.billing_customer_id == customer&.customer_id + end +) { + cheogram = Command.execution.iq.from.resource =~ /\ACheogram/ + Command.customer.then do |customer| + ParentCodeRepo.new.find_or_create(customer.customer_id).then do |code| + Command.finish { |reply| + reply.command << FormTemplate.render( + "subaccount", code: code, cheogram: cheogram + ) + } + end + end +}.register(self).then(&CommandList.method(:register)) + Command.new( "reset sip account", "☎️ Create or Reset SIP Account",