From 78c1b39c6b32f768d2134c8f9e43c1d1536f13e5 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 19 Oct 2022 12:31:08 -0500 Subject: [PATCH] Command to show details (currently just remaining data) for account SIMs --- config-schema.dhall | 1 + config.dhall.sample | 3 ++- forms/sim_details.rb | 4 ++++ lib/sim.rb | 24 ++++++++++++++++++++++++ lib/sim_repo.rb | 35 +++++++++++++++++++++++++++++++++++ schemas | 2 +- sgx_jmp.rb | 22 ++++++++++++++++++++++ test/test_helper.rb | 3 ++- test/test_sim_repo.rb | 39 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 forms/sim_details.rb create mode 100644 lib/sim.rb create mode 100644 lib/sim_repo.rb create mode 100644 test/test_sim_repo.rb diff --git a/config-schema.dhall b/config-schema.dhall index da931fe7d27f0568989c4a5217a4dc0b6646d396..7d69f3ad342032288381f20f21797e0b4ed12554 100644 --- a/config-schema.dhall +++ b/config-schema.dhall @@ -21,6 +21,7 @@ , interac : Text , keep_area_codes : List Text , keep_area_codes_in : { account : Text, sip_peer_id : Text, site_id : Text } +, keepgo : Optional { access_token : Text, api_key : Text } , notify_admin : Text , notify_from : Text , ogm_path : Text diff --git a/config.dhall.sample b/config.dhall.sample index 51a73368110d77646b7c79f2d4a56ba041cd80fd..71251c5dd4bb52576e30d319ed9d3f169dbadd20 100644 --- a/config.dhall.sample +++ b/config.dhall.sample @@ -82,5 +82,6 @@ in snikket_hosting_api = "", rev_ai_token = "", upstream_domain = "example.net", - approved_domains = toMap { `example.com` = Some "customer_id" } + approved_domains = toMap { `example.com` = Some "customer_id" }, + keepgo = Some { api_key = "", access_token = "" } } diff --git a/forms/sim_details.rb b/forms/sim_details.rb new file mode 100644 index 0000000000000000000000000000000000000000..5da27cdbc08d4ad4ceb420eb2eccb2d8af1b9508 --- /dev/null +++ b/forms/sim_details.rb @@ -0,0 +1,4 @@ +result! +title "(e)SIM Details" + +table(@sims, iccid: "ICCID", remaining_usage_mb: "MB Remaining") diff --git a/lib/sim.rb b/lib/sim.rb new file mode 100644 index 0000000000000000000000000000000000000000..d8dea7a86a9398efb8c1bc92da1eb5ceb29910be --- /dev/null +++ b/lib/sim.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "value_semantics/monkey_patched" + +class SIM + value_semantics do + iccid(/\A\d+\Z/) + lpa_code(/\ALPA:/) + remaining_usage_kb Integer + remaining_days Integer + notes String + end + + def self.extract(**kwargs) + kwargs = kwargs.transform_keys(&:to_sym) + new(kwargs.slice( + :iccid, :lpa_code, :remaining_usage_kb, :remaining_days, :notes + )) + end + + def remaining_usage_mb + (remaining_usage_kb / 1024.0).round(2) + end +end diff --git a/lib/sim_repo.rb b/lib/sim_repo.rb new file mode 100644 index 0000000000000000000000000000000000000000..b976d40710508fcbfd8f4855c8a1cd0cbfbfc2a7 --- /dev/null +++ b/lib/sim_repo.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require "lazy_object" +require "value_semantics/monkey_patched" + +require_relative "sim" + +class SIMRepo + value_semantics do + db Anything(), default: LazyObject.new { DB } + end + + def find(iccid) + EM::HttpRequest.new( + "https://myaccount.keepgo.com/api/v2/line/#{iccid}/get_details", + tls: { verify_peer: true } + ).aget( + head: { + "Accept" => "application/json", + "apiKey" => CONFIG[:keepgo][:api_key], + "accessToken" => CONFIG[:keepgo][:access_token] + } + ).then { |req| SIM.extract(**JSON.parse(req.response)["sim_card"]) } + end + + def owned_by(customer) + customer = customer.customer_id if customer.respond_to?(:customer_id) + promise = db.query_defer(<<~SQL, [customer]) + SELECT iccid FROM sims WHERE customer_id=$1 + SQL + promise.then { |result| + EMPromise.all(result.map { |row| find(row["iccid"]) }) + } + end +end diff --git a/schemas b/schemas index 2aef72250750b85e344d3d87d789f002db0a7579..a449672f5cad37e63e6339e4577ed27ff8289df9 160000 --- a/schemas +++ b/schemas @@ -1 +1 @@ -Subproject commit 2aef72250750b85e344d3d87d789f002db0a7579 +Subproject commit a449672f5cad37e63e6339e4577ed27ff8289df9 diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 5f532818a6c1fc27c40a46ab062561da087a61be..9bcfebbf260d1b664a6d99096a7b29bf9f7b70bf 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -100,6 +100,7 @@ require_relative "lib/postgres" require_relative "lib/registration" require_relative "lib/transaction" require_relative "lib/tel_selections" +require_relative "lib/sim_repo" require_relative "lib/snikket" require_relative "web" require_relative "lib/statsd" @@ -672,6 +673,27 @@ Command.new( end }.register(self).then(&CommandList.method(:register)) +Command.new( + "sims", + "(e)SIM Details", + list_for: ->(customer:, **) { CONFIG[:keepgo] && !!customer&.currency } +) { + Command.customer.then(&SIMRepo.new.method(:owned_by)).then do |sims| + if sims.empty? + next Command.finish( + "You have no (e)SIMs, you can get on the waitlist at https://jmp.chat/sim" + ) + end + + Command.finish do |reply| + reply.command << FormTemplate.render( + "sim_details", + sims: sims + ) + end + end +}.register(self).then(&CommandList.method(:register)) + Command.new( "reset sip account", "Create or Reset SIP Account", diff --git a/test/test_helper.rb b/test/test_helper.rb index d9bb6c61ef5325852930d85229c3689a08fe9291..0011f422f9c9f8460f991952ea3c538805ef2345 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -111,7 +111,8 @@ CONFIG = { "refer.example.com": "refer_to" }, bandwidth_site: "test_site", - bandwidth_peer: "test_peer" + bandwidth_peer: "test_peer", + keepgo: { api_key: "keepgokey", access_token: "keepgotoken" } }.freeze def panic(e) diff --git a/test/test_sim_repo.rb b/test/test_sim_repo.rb new file mode 100644 index 0000000000000000000000000000000000000000..c04429493dbdf63de77ad315eb7a9dedf5826d06 --- /dev/null +++ b/test/test_sim_repo.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require "test_helper" +require "sim_repo" + +class SIMRepoTest < Minitest::Test + def setup + @repo = SIMRepo.new( + db: FakeDB.new( + ["test"] => [{ "iccid" => "1234" }] + ) + ) + end + + def test_owned_by + req = stub_request( + :get, + "https://myaccount.keepgo.com/api/v2/line/1234/get_details" + ).with( + headers: { + "Accept" => "application/json", + "Accesstoken" => "keepgotoken", + "Apikey" => "keepgokey" + } + ).to_return(status: 200, body: { sim_card: { + iccid: "1234", + lpa_code: "LPA:dummy", + remaining_usage_kb: 1024, + remaining_days: 365, + notes: "" + } }.to_json) + + sims = @repo.owned_by("test").sync + assert_equal 1, sims.length + assert_equal "1234", sims[0].iccid + assert_requested req + end + em :test_owned_by +end