~singpolyma/jmp-pay

ref: 3c26c525b5c678ccb0e144fefe2bdf2b6b727f94 jmp-pay/bin/billing_monthly_cronjob -rwxr-xr-x 1.6 KiB
3c26c525Stephen Paul Weber Merge branch 'bill-via-sgx-jmp' 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/ruby
# frozen_string_literal: true

require "dhall"
require "pg"

require_relative "../lib/blather_notify"
require_relative "../lib/to_form"

CONFIG = Dhall.load(<<-DHALL).sync
	(#{ARGV[0]}) : {
		sgx_jmp: Text,
		notify_using: {
			jid: Text,
			password: Text,
			target: Text -> Text,
			body: Text -> Text -> Text
		}
	}
DHALL

using ToForm

db = PG.connect(dbname: "jmp")
db.type_map_for_results = PG::BasicTypeMapForResults.new(db)
db.type_map_for_queries = PG::BasicTypeMapForQueries.new(db)

BlatherNotify.start(
	CONFIG[:notify_using][:jid],
	CONFIG[:notify_using][:password]
)

promises = []

db.exec(
	<<-SQL
	SELECT customer_id
	FROM customer_plans
	WHERE expires_at <= LOCALTIMESTAMP + '4 days'
	SQL
).each do |row|
	EM.next_tick do
		promises << BlatherNotify.execute(
			"customer info",
			{ q: row["customer_id"] }.to_form(:submit)
		).then { |iq|
			BlatherNotify.write_with_promise(BlatherNotify.command(
				"customer info",
				iq.sessionid
			))
		}.then do |iq|
			unless iq.form.field("action")
				next "#{row["customer_id"]} not found"
			end

			BlatherNotify.write_with_promise(BlatherNotify.command(
				"customer info",
				iq.sessionid,
				action: :complete,
				form: { action: "bill_plan" }.to_form(:submit)
			))
		end
	end
end

one = Queue.new

def format(item)
	if item.respond_to?(:note) && item.note
		item.note.text
	elsif item.respond_to?(:to_xml)
		item.to_xml
	else
		item.inspect
	end
end

EM.add_timer(0) do
	EMPromise.all(promises).then(
		->(all) { one << all },
		->(err) { one << RuntimeError.new(format(err)) }
	)
end

result = one.pop

raise result if result.is_a?(Exception)

result.each do |item|
	puts format(item)
end