1#!/usr/bin/ruby
2# frozen_string_literal: true
3
4require "dhall"
5require "net/http"
6require "pg"
7require "redis"
8require "ruby-bandwidth-iris"
9
10require_relative "../lib/blather_notify"
11
12CONFIG = Dhall.load(ARGV[0]).sync
13
14BandwidthIris::Client.global_options = {
15 account_id: CONFIG[:creds][:account],
16 username: CONFIG[:creds][:username],
17 password: CONFIG[:creds][:password]
18}
19
20REDIS = Redis.new
21DB = PG.connect(dbname: "jmp")
22DB.type_map_for_results = PG::BasicTypeMapForResults.new(DB)
23DB.type_map_for_queries = PG::BasicTypeMapForQueries.new(DB)
24
25def get_location(tel)
26 BandwidthIris::Tn.get(tel).get_sip_peers[:name]
27rescue BandwidthIris::Errors::GenericError
28 nil
29end
30
31DB.exec(
32 <<~SQL
33 select customer_id
34 from customer_plans
35 where expires_at > localtimestamp
36 SQL
37).each do |row|
38 cid = row["customer_id"]
39 next if REDIS.exists?("catapult_cred-customer_#{cid}@jmp.chat")
40
41 jid = REDIS.get("jmp_customer_jid-#{cid}")
42 tel = REDIS.lindex("catapult_cred-#{jid}", 3)
43 location = get_location(tel)
44 if location.nil?
45 # in catapult
46 puts tel
47 elsif location == "location1"
48 # in dashboard, using v1
49 else
50 raise "Unexpected location #{location}"
51 end
52end