-
Notifications
You must be signed in to change notification settings - Fork 387
/
coupons.go
455 lines (371 loc) · 12.4 KB
/
coupons.go
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"database/sql"
"errors"
"fmt"
"sort"
"time"
"github.com/zeebo/errs"
"storj.io/common/memory"
"storj.io/common/uuid"
"storj.io/storj/private/dbutil/pgutil"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/payments"
"storj.io/storj/satellite/payments/coinpayments"
"storj.io/storj/satellite/payments/stripecoinpayments"
"storj.io/storj/satellite/satellitedb/dbx"
)
// ensures that coupons implements payments.CouponsDB.
var _ stripecoinpayments.CouponsDB = (*coupons)(nil)
// coupons is an implementation of payments.CouponsDB.
//
// architecture: Database
type coupons struct {
db *satelliteDB
}
// Insert inserts a coupon into the database.
func (coupons *coupons) Insert(ctx context.Context, coupon payments.Coupon) (_ payments.Coupon, err error) {
defer mon.Task()(&ctx, coupon)(&err)
id, err := uuid.New()
if err != nil {
return payments.Coupon{}, err
}
cpx, err := coupons.db.Create_Coupon(
ctx,
dbx.Coupon_Id(id[:]),
dbx.Coupon_UserId(coupon.UserID[:]),
dbx.Coupon_Amount(coupon.Amount),
dbx.Coupon_Description(coupon.Description),
dbx.Coupon_Type(int(coupon.Type)),
dbx.Coupon_Status(int(coupon.Status)),
dbx.Coupon_Duration(int64(coupon.Duration)),
)
if err != nil {
return payments.Coupon{}, err
}
return fromDBXCoupon(cpx)
}
// Update updates coupon in database.
func (coupons *coupons) Update(ctx context.Context, couponID uuid.UUID, status payments.CouponStatus) (_ payments.Coupon, err error) {
defer mon.Task()(&ctx, couponID)(&err)
cpx, err := coupons.db.Update_Coupon_By_Id(
ctx,
dbx.Coupon_Id(couponID[:]),
dbx.Coupon_Update_Fields{
Status: dbx.Coupon_Status(int(status)),
},
)
if err != nil {
return payments.Coupon{}, err
}
return fromDBXCoupon(cpx)
}
// Get returns coupon by ID.
func (coupons *coupons) Get(ctx context.Context, couponID uuid.UUID) (_ payments.Coupon, err error) {
defer mon.Task()(&ctx, couponID)(&err)
dbxCoupon, err := coupons.db.Get_Coupon_By_Id(ctx, dbx.Coupon_Id(couponID[:]))
if err != nil {
return payments.Coupon{}, err
}
return fromDBXCoupon(dbxCoupon)
}
// Delete removes a coupon from the database by its ID.
func (coupons *coupons) Delete(ctx context.Context, couponID uuid.UUID) (err error) {
defer mon.Task()(&ctx, couponID)(&err)
_, err = coupons.db.Delete_Coupon_By_Id(ctx, dbx.Coupon_Id(couponID[:]))
return err
}
// List returns all coupons of specified user.
func (coupons *coupons) ListByUserID(ctx context.Context, userID uuid.UUID) (_ []payments.Coupon, err error) {
defer mon.Task()(&ctx, userID)(&err)
dbxCoupons, err := coupons.db.All_Coupon_By_UserId_OrderBy_Desc_CreatedAt(
ctx,
dbx.Coupon_UserId(userID[:]),
)
if err != nil {
return nil, err
}
return couponsFromDbxSlice(dbxCoupons)
}
// ListByUserIDAndStatus returns all coupons of specified user and status. Results are ordered (asc) by expiration date.
func (coupons *coupons) ListByUserIDAndStatus(ctx context.Context, userID uuid.UUID, status payments.CouponStatus) (_ []payments.Coupon, err error) {
defer mon.Task()(&ctx, userID)(&err)
dbxCoupons, err := coupons.db.All_Coupon_By_UserId_And_Status_OrderBy_Desc_CreatedAt(
ctx,
dbx.Coupon_UserId(userID[:]),
dbx.Coupon_Status(int(status)),
)
if err != nil {
return nil, err
}
result, err := couponsFromDbxSlice(dbxCoupons)
if err != nil {
return nil, err
}
sort.Slice(result, func(i, k int) bool {
iDate := result[i].ExpirationDate()
kDate := result[k].ExpirationDate()
return iDate.Before(kDate)
})
return result, nil
}
// List returns all coupons with specified status.
func (coupons *coupons) List(ctx context.Context, status payments.CouponStatus) (_ []payments.Coupon, err error) {
defer mon.Task()(&ctx, status)(&err)
dbxCoupons, err := coupons.db.All_Coupon_By_Status_OrderBy_Desc_CreatedAt(
ctx,
dbx.Coupon_Status(int(status)),
)
if err != nil {
return nil, err
}
return couponsFromDbxSlice(dbxCoupons)
}
// ListPending returns paginated list of pending transactions.
func (coupons *coupons) ListPaged(ctx context.Context, offset int64, limit int, before time.Time, status payments.CouponStatus) (_ payments.CouponsPage, err error) {
defer mon.Task()(&ctx)(&err)
var page payments.CouponsPage
dbxCoupons, err := coupons.db.Limited_Coupon_By_CreatedAt_LessOrEqual_And_Status_OrderBy_Desc_CreatedAt(
ctx,
dbx.Coupon_CreatedAt(before.UTC()),
dbx.Coupon_Status(coinpayments.StatusPending.Int()),
limit+1,
offset,
)
if err != nil {
return payments.CouponsPage{}, err
}
if len(dbxCoupons) == limit+1 {
page.Next = true
page.NextOffset = offset + int64(limit) + 1
dbxCoupons = dbxCoupons[:len(dbxCoupons)-1]
}
page.Coupons, err = couponsFromDbxSlice(dbxCoupons)
if err != nil {
return payments.CouponsPage{}, nil
}
return page, nil
}
// fromDBXCoupon converts *dbx.Coupon to *payments.Coupon.
func fromDBXCoupon(dbxCoupon *dbx.Coupon) (coupon payments.Coupon, err error) {
coupon.UserID, err = uuid.FromBytes(dbxCoupon.UserId)
if err != nil {
return payments.Coupon{}, err
}
coupon.ID, err = uuid.FromBytes(dbxCoupon.Id)
if err != nil {
return payments.Coupon{}, err
}
coupon.Duration = int(dbxCoupon.Duration)
coupon.Description = dbxCoupon.Description
coupon.Amount = dbxCoupon.Amount
coupon.Created = dbxCoupon.CreatedAt
coupon.Status = payments.CouponStatus(dbxCoupon.Status)
return coupon, nil
}
// AddUsage creates new coupon usage record in database.
func (coupons *coupons) AddUsage(ctx context.Context, usage stripecoinpayments.CouponUsage) (err error) {
defer mon.Task()(&ctx, usage)(&err)
_, err = coupons.db.Create_CouponUsage(
ctx,
dbx.CouponUsage_CouponId(usage.CouponID[:]),
dbx.CouponUsage_Amount(usage.Amount),
dbx.CouponUsage_Status(int(usage.Status)),
dbx.CouponUsage_Period(usage.Period),
)
return err
}
// TotalUsage gets sum of all usage records for specified coupon.
func (coupons *coupons) TotalUsage(ctx context.Context, couponID uuid.UUID) (_ int64, err error) {
defer mon.Task()(&ctx, couponID)(&err)
query := coupons.db.Rebind(
`SELECT COALESCE(SUM(amount), 0)
FROM coupon_usages
WHERE coupon_id = ?;`,
)
amountRow := coupons.db.QueryRowContext(ctx, query, couponID[:])
var amount int64
err = amountRow.Scan(&amount)
return amount, err
}
// TotalUsage gets sum of all usage records for specified coupon.
func (coupons *coupons) TotalUsageForPeriod(ctx context.Context, couponID uuid.UUID, period time.Time) (_ int64, err error) {
defer mon.Task()(&ctx, couponID)(&err)
query := coupons.db.Rebind(
`SELECT COALESCE(SUM(amount), 0)
FROM coupon_usages
WHERE coupon_id = ?;`,
)
amountRow := coupons.db.QueryRowContext(ctx, query, couponID[:])
var amount int64
err = amountRow.Scan(&amount)
return amount, err
}
// GetLatest return period_end of latest coupon charge.
func (coupons *coupons) GetLatest(ctx context.Context, couponID uuid.UUID) (_ time.Time, err error) {
defer mon.Task()(&ctx, couponID)(&err)
query := coupons.db.Rebind(
`SELECT period
FROM coupon_usages
WHERE coupon_id = ?
ORDER BY period DESC
LIMIT 1;`,
)
amountRow := coupons.db.QueryRowContext(ctx, query, couponID[:])
var created time.Time
err = amountRow.Scan(&created)
if errors.Is(err, sql.ErrNoRows) {
return created, stripecoinpayments.ErrNoCouponUsages.Wrap(err)
}
return created, err
}
// ListUnapplied returns coupon usage page with unapplied coupon usages.
func (coupons *coupons) ListUnapplied(ctx context.Context, offset int64, limit int, period time.Time) (_ stripecoinpayments.CouponUsagePage, err error) {
defer mon.Task()(&ctx, offset, limit, period)(&err)
var page stripecoinpayments.CouponUsagePage
dbxRecords, err := coupons.db.Limited_CouponUsage_By_Period_And_Status_Equal_Number(
ctx,
dbx.CouponUsage_Period(period),
limit+1,
offset,
)
if err != nil {
return stripecoinpayments.CouponUsagePage{}, err
}
if len(dbxRecords) == limit+1 {
page.Next = true
page.NextOffset = offset + int64(limit) + 1
dbxRecords = dbxRecords[:len(dbxRecords)-1]
}
for _, dbxRecord := range dbxRecords {
record, err := couponUsageFromDbxSlice(dbxRecord)
if err != nil {
return stripecoinpayments.CouponUsagePage{}, err
}
page.Usages = append(page.Usages, record)
}
return page, nil
}
// ApplyUsage applies coupon usage and updates its status.
func (coupons *coupons) ApplyUsage(ctx context.Context, couponID uuid.UUID, period time.Time) (err error) {
defer mon.Task()(&ctx, couponID, period)(&err)
_, err = coupons.db.Update_CouponUsage_By_CouponId_And_Period(
ctx,
dbx.CouponUsage_CouponId(couponID[:]),
dbx.CouponUsage_Period(period),
dbx.CouponUsage_Update_Fields{
Status: dbx.CouponUsage_Status(int(stripecoinpayments.CouponUsageStatusApplied)),
},
)
return err
}
// couponsFromDbxSlice is used for creating []payments.Coupon entities from autogenerated []dbx.Coupon struct.
func couponsFromDbxSlice(couponsDbx []*dbx.Coupon) (_ []payments.Coupon, err error) {
var coupons = make([]payments.Coupon, 0)
var errors []error
// Generating []dbo from []dbx and collecting all errors
for _, couponDbx := range couponsDbx {
coupon, err := fromDBXCoupon(couponDbx)
if err != nil {
errors = append(errors, err)
continue
}
coupons = append(coupons, coupon)
}
return coupons, errs.Combine(errors...)
}
// couponUsageFromDbxSlice is used for creating stripecoinpayments.CouponUsage entity from autogenerated dbx.CouponUsage struct.
func couponUsageFromDbxSlice(couponUsageDbx *dbx.CouponUsage) (usage stripecoinpayments.CouponUsage, err error) {
usage.Status = stripecoinpayments.CouponUsageStatus(couponUsageDbx.Status)
usage.Period = couponUsageDbx.Period
usage.Amount = couponUsageDbx.Amount
usage.CouponID, err = uuid.FromBytes(couponUsageDbx.CouponId)
if err != nil {
return stripecoinpayments.CouponUsage{}, err
}
return usage, err
}
// PopulatePromotionalCoupons is used to populate promotional coupons through all active users who already have a project
// and do not have a promotional coupon yet. And updates project limits to selected size.
func (coupons *coupons) PopulatePromotionalCoupons(ctx context.Context, users []uuid.UUID, duration int, amount int64, projectLimit memory.Size) (err error) {
defer mon.Task()(&ctx, users, duration, amount, projectLimit)(&err)
ids, err := coupons.activeUserWithProjectAndWithoutCoupon(ctx, users)
if err != nil {
return err
}
return coupons.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
for _, id := range ids {
_, err = coupons.Insert(ctx, payments.Coupon{
UserID: id.UserID,
Amount: amount,
Duration: duration,
Description: fmt.Sprintf("Promotional credits (limited time - %d billing periods)", duration),
Type: payments.CouponTypePromotional,
Status: payments.CouponActive,
})
if err != nil {
return err
}
// if projectLimit specified, set it, else omit change the existing value
if projectLimit.Int64() > 0 {
_, err = coupons.db.Update_Project_By_Id(ctx,
dbx.Project_Id(id.ProjectID[:]),
dbx.Project_Update_Fields{
UsageLimit: dbx.Project_UsageLimit(projectLimit.Int64()),
},
)
}
if err != nil {
return err
}
}
return nil
})
}
type userAndProject struct {
UserID uuid.UUID
ProjectID uuid.UUID
}
func (coupons *coupons) activeUserWithProjectAndWithoutCoupon(ctx context.Context, users []uuid.UUID) (ids []userAndProject, err error) {
var userIDs [][]byte
for i := range users {
userIDs = append(userIDs, users[i][:])
}
rows, err := coupons.db.QueryContext(ctx, coupons.db.Rebind(`
SELECT users_with_projects.id, users_with_projects.project_id
FROM (
SELECT selected_users.id, first_proj.id AS project_id
FROM (
SELECT id, status
FROM users
WHERE id = any(?)
) AS selected_users
INNER JOIN (
SELECT DISTINCT ON (owner_id) owner_id, id
FROM projects
ORDER BY owner_id, created_at ASC
) AS first_proj
ON selected_users.id = first_proj.owner_id
WHERE selected_users.status = ?
) AS users_with_projects
WHERE users_with_projects.id NOT IN (
SELECT user_id FROM coupons WHERE type = ?
)
`), pgutil.ByteaArray(userIDs), console.Active, payments.CouponTypePromotional)
if err != nil {
return nil, err
}
defer func() { err = errs.Combine(err, rows.Close()) }()
for rows.Next() {
var id userAndProject
err = rows.Scan(&id.UserID, &id.ProjectID)
if err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, rows.Err()
}