-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwhitelabelusers.go
81 lines (67 loc) · 1.77 KB
/
whitelabelusers.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
package database
import (
"context"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"time"
)
type WhitelabelUsers struct {
*pgxpool.Pool
}
func newWhitelabelUsers(db *pgxpool.Pool) *WhitelabelUsers {
return &WhitelabelUsers{
db,
}
}
func (p WhitelabelUsers) Schema() string {
return `
CREATE TABLE IF NOT EXISTS whitelabel_users(
"user_id" int8 NOT NULL UNIQUE,
"expiry" timestamp NOT NULL,
PRIMARY KEY("user_id")
);`
}
func (p *WhitelabelUsers) IsPremium(ctx context.Context, userId uint64) (bool, error) {
expiry, err := p.GetExpiry(ctx, userId)
if err != nil {
return false, err
}
return expiry.After(time.Now()), nil
}
func (p *WhitelabelUsers) AnyPremium(ctx context.Context, userIds []uint64) (bool, error) {
query := `
SELECT EXISTS(
SELECT 1
FROM whitelabel_users
WHERE "user_id" = ANY($1) AND "expiry" > NOW()
);
`
userIdArray := &pgtype.Int8Array{}
if err := userIdArray.Set(userIds); err != nil {
return false, err
}
var res bool
if err := p.QueryRow(ctx, query, userIdArray).Scan(&res); err != nil {
return false, err
}
return res, nil
}
func (p *WhitelabelUsers) GetExpiry(ctx context.Context, userId uint64) (expiry time.Time, e error) {
if err := p.QueryRow(ctx, `SELECT "expiry" from whitelabel_users WHERE "user_id" = $1;`, userId).Scan(&expiry); err != nil && err != pgx.ErrNoRows {
e = err
}
return
}
func (p *WhitelabelUsers) Add(ctx context.Context, userId uint64, interval time.Duration) (err error) {
query := `
INSERT INTO whitelabel_users("user_id", "expiry")
VALUES($1, NOW() + $2)
ON CONFLICT("user_id")
DO UPDATE SET "expiry" = CASE WHEN whitelabel_users.expiry < NOW()
THEN NOW() + $2
ELSE whitelabel_users.expiry + $2
END;`
_, err = p.Exec(ctx, query, userId, interval)
return
}