cancel_expired_customers

  1#!/usr/bin/ruby
  2# frozen_string_literal: true
  3
  4require "date"
  5require "dhall"
  6require "em_promise"
  7require "pg"
  8require "ruby-bandwidth-iris"
  9require "set"
 10
 11require_relative "../lib/blather_notify"
 12require_relative "../lib/to_form"
 13
 14CONFIG = Dhall.load(<<-DHALL).sync
 15	(#{ARGV[0]}) : {
 16		sgx_jmp: Text,
 17		creds: {
 18			account: Text,
 19			username: Text,
 20			password: Text
 21		},
 22		notify_using: {
 23			jid: Text,
 24			password: Text,
 25			target: Text -> Text,
 26			body: Text -> Text -> Text
 27		}
 28	}
 29DHALL
 30
 31Faraday.default_adapter = :em_synchrony
 32BandwidthIris::Client.global_options = {
 33	account_id: CONFIG[:creds][:account],
 34	username: CONFIG[:creds][:username],
 35	password: CONFIG[:creds][:password]
 36}
 37
 38using ToForm
 39
 40db = PG.connect(dbname: "jmp")
 41db.type_map_for_results = PG::BasicTypeMapForResults.new(db)
 42db.type_map_for_queries = PG::BasicTypeMapForQueries.new(db)
 43
 44BlatherNotify.start(
 45	CONFIG[:notify_using][:jid],
 46	CONFIG[:notify_using][:password]
 47)
 48
 49def format(item)
 50	if item.respond_to?(:note) && item.note && item.note.text != ""
 51		item.note.text
 52	elsif item.respond_to?(:to_xml)
 53		item.to_xml
 54	else
 55		item.inspect
 56	end
 57end
 58
 59ported_in_promise = Promise.new
 60
 61EM.schedule do
 62	Fiber.new {
 63		begin
 64			tns = Set.new
 65			page = BandwidthIris::PortIn.list(
 66				page: 1,
 67				size: 1000,
 68				status: :complete
 69			)
 70			while page
 71				page.each_slice(250) do |orders|
 72					EMPromise.all(
 73						orders.map { |order|
 74							EMPromise.resolve(nil).then { order.tns }
 75						}
 76					).sync.each { |chunk| tns += chunk.map { |tn| "+1#{tn}" } }
 77				end
 78				page = page.next
 79			end
 80			ported_in_promise.fulfill(tns)
 81		rescue StandardError
 82			ported_in_promise.reject($!)
 83		end
 84	}.resume
 85end
 86
 87class ExpiringCustomer
 88	def initialize(customer_id)
 89		@customer_id = customer_id
 90	end
 91
 92	def info
 93		BlatherNotify.execute(
 94			"customer info",
 95			{ q: @customer_id }.to_form(:submit)
 96		).then do |iq|
 97			@sessionid = iq.sessionid
 98			unless iq.form.field("customer_id")
 99				raise "#{@customer_id} not found"
100			end
101
102			iq
103		end
104	end
105
106	def next
107		raise "Call info first" unless @sessionid
108
109		BlatherNotify.write_with_promise(BlatherNotify.command(
110			"customer info",
111			@sessionid
112		))
113	end
114
115	def cancel_account
116		raise "Call info first" unless @sessionid
117
118		BlatherNotify.write_with_promise(BlatherNotify.command(
119			"customer info",
120			@sessionid,
121			action: :complete,
122			form: { action: "cancel_account" }.to_form(:submit)
123		))
124	end
125end
126
127one = Queue.new
128
129ported_in_promise.then { |ported_in|
130	EM::Iterator.new(db.exec(
131		<<-SQL
132		SELECT customer_id, expires_at FROM customer_plans
133		WHERE expires_at < LOCALTIMESTAMP - INTERVAL '1 month'
134		SQL
135	), 3).each(nil, -> { one << :done }) do |row, iter|
136		customer = ExpiringCustomer.new(row["customer_id"])
137		customer.info.then { |iq|
138			if ported_in.include?(iq.form.field("tel")&.value&.to_s) &&
139			   row["expires_at"] > (Date.today << 12)
140				puts "#{row['customer_id']} ported in, skipping"
141				EMPromise.reject(:skip)
142			else
143				customer.next
144			end
145		}.then {
146			customer.cancel_account
147		}.then { |result|
148			puts format(result)
149			iter.next
150		}.catch do |err|
151			next iter.next if err == :skip
152
153			one << (err.is_a?(Exception) ? err : RuntimeError.new(format(err)))
154		end
155	end
156}.catch do |err|
157	one << (err.is_a?(Exception) ? err : RuntimeError.new(format(err)))
158end
159
160result = one.pop
161raise result if result.is_a?(Exception)