forked from stepan-mitkin/drakonhub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlic.lua
385 lines (353 loc) · 8.31 KB
/
lic.lua
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
-- Autogenerated with DRAKON Editor 1.32
require('strict').on()
local cap_secret = "6LcMXBATAAAAAG6Cji7AQZqFzVigSXdy_dWdOCNq"
local table = table
local string = string
local pairs = pairs
local ipairs = ipairs
local io = io
local pcall = pcall
local xpcall = xpcall
local debug = debug
local tostring = tostring
local clock = require("clock")
local log = require("log")
local digest = require("digest")
local fiber = require("fiber")
local json = require("json")
local fio = require("fio")
local log = require("log")
local os = os
local error = error
local print = print
local utf8 = require("lua-utf8")
local utils = require("utils")
local trans = require("trans")
local mail = require("mail")
local ej = require("ej")
local price_cfg = price_cfg
local global_cfg = global_cfg
local WARNING_BEFORE = utils.days_to_secs(40)
local db = require(global_cfg.db)
setfenv(1, {})
function create_coupon(code, product_id, max_users, tag, user_id)
-- item 422
local data = {
when_created = os.time(),
code = code,
product_id = product_id,
max_users = max_users,
tag = tag,
user_id = user_id
}
-- item 423
db.coupon_insert(
code,
data
)
-- item 424
ej.info(
"generate coupon",
{code=code, user_id=user_id}
)
end
function create_coupons(count, product_id, max_users, tag, user_id)
-- item 400
local i
local coupons = {}
-- item 411
if (max_users < 1) or (max_users > 5000) then
-- item 415
error("wrong number of users: "
.. tostring(max_users))
else
-- item 404
if (count < 1) or (count > 100) then
-- item 407
error("wrong number of coupons: "
.. tostring(count))
else
-- item 4020001
i = 1
while true do
-- item 4020002
if i <= count then
else
break
end
-- item 410
local code = utils.random_string()
-- item 416
code = code:sub(1, 16):upper()
-- item 408
create_coupon(
code,
product_id,
max_users,
tag,
user_id
)
-- item 409
table.insert(
coupons,
code
)
-- item 4020003
i = i + 1
end
end
end
-- item 401
return coupons
end
function create_license(user_id, product_id, expiry, limits, trans_id, agreement_id, tag)
-- item 314
local now = os.time()
-- item 312
local data = {
user_id = user_id,
product_id = product_id,
expiry = expiry,
limits = limits,
trans_id = trans_id,
agreement_id = agreement_id,
tag = tag,
when_created = now,
when_updated = now,
active = true
}
-- item 313
return db.license_insert(data)
end
function create_transaction(user_id, type, data)
-- item 461
local tdata = utils.copy(data)
-- item 459
tdata.when_created = os.time()
tdata.when_updated = tdata.when_created
tdata.user_id = user_id
tdata.type = type
-- item 463
local id
while true do
-- item 464
id = utils.random_string()
id = id:sub(1, 10)
-- item 465
local old = db.transaction_get(id)
-- item 466
if old then
else
break
end
end
-- item 458
db.transaction_insert(
id,
user_id,
tdata
)
-- item 460
return id
end
function delete_license(id)
-- item 392
if id then
-- item 544
local old = db.license_get(id)
-- item 391
db.license_delete(id)
-- item 540
return old
else
-- item 541
return nil
end
end
function delete_user_transactions(user_id)
-- item 535
local all = db.transaction_get_by_user(user_id)
for _, tran in ipairs(all) do
-- item 538
local id = tran[1]
db.transaction_delete(id)
end
end
function get_coupon(code)
-- item 438
if code then
-- item 430
local data = db.coupon_get(code)
-- item 431
if (data) and (not (data.redeemed_by)) then
-- item 435
return data
else
-- item 434
return nil
end
else
-- item 434
return nil
end
end
function get_license(license_id)
-- item 320
local data = get_raw_license(license_id)
-- item 328
if (data) and (data.active) then
-- item 324
local now = os.time()
-- item 325
if now > data.expiry then
-- item 327
return nil
else
-- item 326
return data
end
else
-- item 327
return nil
end
end
function get_raw_license(id)
-- item 546
local expiry = os.time() +
3600 * 24 * 365 * 20
-- item 548
local product = price_cfg.products.basic
-- item 547
local limits = {
max_diagrams = product.max_diagrams,
max_spaces = product.max_spaces,
max_users = product.max_users
}
-- item 545
local data = {
user_id = nil,
product_id = "basic",
expiry = expiry,
limits = limits,
trans_id = nil,
agreement_id = nil,
tag = nil,
when_created = nil,
when_updated = nil,
active = true
}
-- item 382
return data
end
function get_transaction(id)
-- item 473
return db.transaction_get(id)
end
function get_user_transactions(user_id)
-- item 542
local all = db.transaction_get_by_user(user_id)
-- item 508
local result = {}
for _, tran in ipairs(all) do
-- item 512
local id = tran[1]
local transaction = get_transaction(id)
-- item 514
if (transaction.completed) and (transaction.status == "success") then
-- item 513
table.insert(result, transaction)
end
end
-- item 524
local by_when_created = function(left, right)
return left.when_created < right.when_created
end
-- item 525
table.sort(
result,
by_when_created
)
-- item 509
return result
end
function should_show_banner(expiry)
-- item 485
local now = os.time()
-- item 484
local left = expiry - now
local show_banner = (left < WARNING_BEFORE)
-- item 486
return show_banner
end
function update_coupon(id, fields)
-- item 444
local coupon = get_coupon(id)
-- item 449
if coupon then
for key, value in pairs(fields) do
-- item 447
coupon[key] = value
end
-- item 452
coupon.when_updated = os.time()
-- item 448
db.coupon_update(
id,
coupon
)
end
end
function update_license(id, fields)
-- item 334
local license = get_raw_license(id)
-- item 338
if license then
for key, value in pairs(fields) do
-- item 337
license[key] = value
end
-- item 342
license.when_updated = os.time()
-- item 341
db.license_update(
id,
license
)
end
end
function update_transaction(id, fields)
-- item 492
local transaction = get_transaction(id)
-- item 496
if transaction then
for key, value in pairs(fields) do
-- item 495
transaction[key] = value
end
-- item 499
transaction.when_updated = os.time()
-- item 543
db.transaction_update(
transaction.id,
transaction.user_id,
transaction
)
end
end
return {
create_license = create_license,
get_license = get_license,
create_transaction = create_transaction,
get_transaction = get_transaction,
create_coupon = create_coupon,
create_coupons = create_coupons,
get_coupon = get_coupon,
update_license = update_license,
update_coupon = update_coupon,
get_raw_license = get_raw_license,
should_show_banner = should_show_banner,
update_transaction = update_transaction,
get_user_transactions = get_user_transactions,
delete_user_transactions = delete_user_transactions,
delete_license = delete_license
}