-
Notifications
You must be signed in to change notification settings - Fork 16
/
points_tx.go
120 lines (106 loc) · 3.62 KB
/
points_tx.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
package types
import (
"errors"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/gbl08ma/sqalx"
"github.com/jmoiron/sqlx/types"
"github.com/palantir/stacktrace"
)
// PointsTx is a points transaction
type PointsTx struct {
ID int64 `dbKey:"true"`
RewardsAddress string
CreatedAt time.Time
UpdatedAt time.Time
Value int
Type PointsTxType
Extra types.JSONText
}
// GetPointsTxForAddress returns all the points transactions for the given address
func GetPointsTxForAddress(node sqalx.Node, address string, pagParams *PaginationParams) ([]*PointsTx, uint64, error) {
s := sdb.Select().
Where(sq.Eq{"points_tx.rewards_address": address}).
OrderBy("points_tx.created_at DESC")
s = applyPaginationParameters(s, pagParams)
return GetWithSelectAndCount[*PointsTx](node, s)
}
// ErrPointsTxNotFound is returned when we can not find the specified points transaction
var ErrPointsTxNotFound = errors.New("points transaction not found")
// GetLatestPointsTxForAddress returns the most recent points transaction for the given address
func GetLatestPointsTxForAddress(node sqalx.Node, address string) (*PointsTx, error) {
s := sdb.Select().
Where(subQueryEq(
"points_tx.id",
sq.Select("MAX(e.id)").
From("points_tx e").
Where(sq.Eq{"e.rewards_address": address}),
))
txs, err := GetWithSelect[*PointsTx](node, s)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
if len(txs) == 0 {
return nil, stacktrace.Propagate(ErrPointsTxNotFound, "")
}
return txs[0], nil
}
// GetLatestPointsTxForAddress returns the most recent points transaction of the given type for the given address
func GetLatestPointsTxOfTypeForAddress(node sqalx.Node, txType PointsTxType, address string) (*PointsTx, error) {
s := sdb.Select().
Where(subQueryEq(
"points_tx.id",
sq.Select("MAX(e.id)").
From("points_tx e").
Where(sq.Eq{"e.rewards_address": address}).
Where(sq.Eq{"e.type": txType}),
))
txs, err := GetWithSelect[*PointsTx](node, s)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
if len(txs) == 0 {
return nil, stacktrace.Propagate(ErrPointsTxNotFound, "")
}
return txs[0], nil
}
// Insert inserts the PointsTx
func (obj *PointsTx) Insert(node sqalx.Node) error {
return Insert(node, obj)
}
// AdjustValue adjusts the value of the PointsTx by the specified amount.
func (obj *PointsTx) AdjustValue(node sqalx.Node, value int) error {
tx, err := node.Beginx()
if err != nil {
return stacktrace.Propagate(err, "")
}
defer tx.Rollback()
now := time.Now()
_, err = sdb.Update("points_tx").
Set("value", sq.Expr("value + ?", value)).
Set("updated_at", now).
Where(sq.Eq{"id": obj.ID}).
RunWith(tx).Exec()
if err != nil {
return stacktrace.Propagate(err, "")
}
obj.Value += value
obj.UpdatedAt = time.Now()
return stacktrace.Propagate(tx.Commit(), "")
}
type PointsTxType int
const (
PointsTxTypeActivityChallengeReward PointsTxType = 1
PointsTxTypeChatActivityReward PointsTxType = 2
PointsTxTypeMediaEnqueuedReward PointsTxType = 3
PointsTxTypeChatGifAttachment PointsTxType = 4
PointsTxTypeManualAdjustment PointsTxType = 5
PointsTxTypeMediaEnqueuedRewardReversal PointsTxType = 6
PointsTxTypeConversionFromBanano PointsTxType = 7
PointsTxTypeQueueEntryReordering PointsTxType = 8
PointsTxTypeMonthlySubscription PointsTxType = 9
PointsTxTypeSkipThresholdReduction PointsTxType = 10
PointsTxTypeSkipThresholdIncrease PointsTxType = 11
PointsTxTypeConcealedEntryEnqueuing PointsTxType = 12
PointsTxTypeApplicationDefined PointsTxType = 13
)