-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpolarentitlements.go
More file actions
131 lines (108 loc) · 3.52 KB
/
Copy pathpolarentitlements.go
File metadata and controls
131 lines (108 loc) · 3.52 KB
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
package database
import (
"context"
_ "embed"
"errors"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type PolarEntitlements struct {
*pgxpool.Pool
}
func newPolarEntitlements(db *pgxpool.Pool) *PolarEntitlements {
return &PolarEntitlements{
db,
}
}
type PolarEntitlement struct {
PolarSubscriptionId string `json:"polar_subscription_id"`
EntitlementId uuid.UUID `json:"entitlement_id"`
UserId uint64 `json:"user_id"`
PolarProductId string `json:"polar_product_id"`
Status string `json:"status"`
}
type PolarEntitlementWithDetails struct {
PolarEntitlement
SkuId uuid.UUID `json:"sku_id"`
SkuLabel string `json:"sku_label"`
Tier string `json:"tier"`
ExpiresAt *time.Time `json:"expires_at"`
GuildId *uint64 `json:"guild_id"`
PermittedServerCount *int `json:"permitted_server_count,omitempty"`
}
var (
//go:embed sql/polar_entitlements/schema.sql
polarEntitlementsSchema string
//go:embed sql/polar_entitlements/insert.sql
polarEntitlementsInsert string
//go:embed sql/polar_entitlements/get_by_subscription_id.sql
polarEntitlementsGetBySubscriptionId string
//go:embed sql/polar_entitlements/list_by_user.sql
polarEntitlementsListByUser string
//go:embed sql/polar_entitlements/delete_by_subscription_id.sql
polarEntitlementsDeleteBySubscriptionId string
//go:embed sql/polar_entitlements/update_status.sql
polarEntitlementsUpdateStatus string
)
func (e PolarEntitlements) Schema() string {
return polarEntitlementsSchema
}
func (e *PolarEntitlements) Insert(ctx context.Context, tx pgx.Tx, polarSubscriptionId string, entitlementId uuid.UUID, userId uint64, polarProductId string, status string) error {
_, err := tx.Exec(ctx, polarEntitlementsInsert, polarSubscriptionId, entitlementId, userId, polarProductId, status)
return err
}
func (e *PolarEntitlements) GetBySubscriptionId(ctx context.Context, tx pgx.Tx, polarSubscriptionId string) (*PolarEntitlement, error) {
var ent PolarEntitlement
err := tx.QueryRow(ctx, polarEntitlementsGetBySubscriptionId, polarSubscriptionId).Scan(
&ent.PolarSubscriptionId,
&ent.EntitlementId,
&ent.UserId,
&ent.PolarProductId,
&ent.Status,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, err
}
return &ent, nil
}
func (e *PolarEntitlements) ListByUser(ctx context.Context, tx pgx.Tx, userId uint64) ([]PolarEntitlementWithDetails, error) {
rows, err := tx.Query(ctx, polarEntitlementsListByUser, userId)
if err != nil {
return nil, err
}
defer rows.Close()
var entitlements []PolarEntitlementWithDetails
for rows.Next() {
var ent PolarEntitlementWithDetails
if err := rows.Scan(
&ent.PolarSubscriptionId,
&ent.EntitlementId,
&ent.UserId,
&ent.PolarProductId,
&ent.Status,
&ent.SkuId,
&ent.SkuLabel,
&ent.Tier,
&ent.ExpiresAt,
&ent.GuildId,
&ent.PermittedServerCount,
); err != nil {
return nil, err
}
entitlements = append(entitlements, ent)
}
return entitlements, nil
}
func (e *PolarEntitlements) DeleteBySubscriptionId(ctx context.Context, tx pgx.Tx, polarSubscriptionId string) error {
_, err := tx.Exec(ctx, polarEntitlementsDeleteBySubscriptionId, polarSubscriptionId)
return err
}
func (e *PolarEntitlements) UpdateStatus(ctx context.Context, tx pgx.Tx, polarSubscriptionId string, status string) error {
_, err := tx.Exec(ctx, polarEntitlementsUpdateStatus, polarSubscriptionId, status)
return err
}