-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
143 lines (117 loc) · 4.97 KB
/
store.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
package postgres
import (
"context"
"database/sql"
"time"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/intent"
"github.com/jmoiron/sqlx"
)
type store struct {
db *sqlx.DB
}
func New(db *sql.DB) intent.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// Save creates or updates a intnent on the store.
func (s *store) Save(ctx context.Context, record *intent.Record) error {
obj, err := toIntentModel(record)
if err != nil {
return err
}
err = obj.dbSave(ctx, s.db)
if err != nil {
return err
}
res := fromIntentModel(obj)
res.CopyTo(record)
return nil
}
// Get finds the intent record for a given intent ID.
//
// Returns ErrNotFound if no record is found.
func (s *store) Get(ctx context.Context, intentID string) (*intent.Record, error) {
obj, err := dbGetIntent(ctx, s.db, intentID)
if err != nil {
return nil, err
}
return fromIntentModel(obj), nil
}
// GetLatestByInitiatorAndType gets the latest record by intent type and initiating owner
//
// Returns ErrNotFound if no records are found.
func (s *store) GetLatestByInitiatorAndType(ctx context.Context, intentType intent.Type, owner string) (*intent.Record, error) {
model, err := dbGetLatestByInitiatorAndType(ctx, s.db, intentType, owner)
if err != nil {
return nil, err
}
return fromIntentModel(model), nil
}
// GetAllByOwner returns all records for a given owner (as both a source and destination).
//
// Returns ErrNotFound if no records are found.
func (s *store) GetAllByOwner(ctx context.Context, owner string, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*intent.Record, error) {
models, err := dbGetAllByOwner(ctx, s.db, owner, cursor, limit, direction)
if err != nil {
return nil, err
}
intents := make([]*intent.Record, len(models))
for i, model := range models {
intents[i] = fromIntentModel(model)
}
return intents, nil
}
// CountForAntispam gets a count of intents for antispam purposes. It calculates the
// number of intents by type and state for a phone number since a timestamp.
func (s *store) CountForAntispam(ctx context.Context, intentType intent.Type, phoneNumber string, states []intent.State, since time.Time) (uint64, error) {
return dbGetCountForAntispam(ctx, s.db, intentType, phoneNumber, states, since)
}
// CountOwnerInteractionsForAntispam gets a count of intents for antispam purposes. It
// calculates the number of times a source owner is involved in an intent with the
// destination owner since a timestamp.
func (s *store) CountOwnerInteractionsForAntispam(ctx context.Context, sourceOwner, destinationOwner string, states []intent.State, since time.Time) (uint64, error) {
return dbGetCountOwnerInteractionsForAntispam(ctx, s.db, sourceOwner, destinationOwner, states, since)
}
// GetTransactedAmountForAntiMoneyLaundering gets the total transacted Kin in quarks and the
// corresponding USD market value for a phone number since a timestamp.
func (s *store) GetTransactedAmountForAntiMoneyLaundering(ctx context.Context, phoneNumber string, since time.Time) (uint64, float64, error) {
return dbGetTransactedAmountForAntiMoneyLaundering(ctx, s.db, phoneNumber, since)
}
// GetDepositedAmountForAntiMoneyLaundering gets the total deposited Kin in quarks and the
// corresponding USD market value for a phone number since a timestamp.
func (s *store) GetDepositedAmountForAntiMoneyLaundering(ctx context.Context, phoneNumber string, since time.Time) (uint64, float64, error) {
return dbGetDepositedAmountForAntiMoneyLaundering(ctx, s.db, phoneNumber, since)
}
// GetNetBalanceFromPrePrivacy2022Intents gets the net balance of Kin in quarks after appying
// pre-privacy legacy payment intents when intents detailed the entirety of the payment.
func (s *store) GetNetBalanceFromPrePrivacy2022Intents(ctx context.Context, account string) (int64, error) {
return dbGetNetBalanceFromPrePrivacy2022Intents(ctx, s.db, account)
}
// GetLatestSaveRecentRootIntentForTreasury gets the latest SaveRecentRoot intent for a treasury
func (s *store) GetLatestSaveRecentRootIntentForTreasury(ctx context.Context, treasury string) (*intent.Record, error) {
model, err := dbGetLatestSaveRecentRootIntentForTreasury(ctx, s.db, treasury)
if err != nil {
return nil, err
}
return fromIntentModel(model), nil
}
// GetOriginalGiftCardIssuedIntent gets the original intent where a gift card
// was issued by its vault address.
func (s *store) GetOriginalGiftCardIssuedIntent(ctx context.Context, giftCardVault string) (*intent.Record, error) {
model, err := dbGetOriginalGiftCardIssuedIntent(ctx, s.db, giftCardVault)
if err != nil {
return nil, err
}
return fromIntentModel(model), nil
}
// GetGiftCardClaimedIntent gets the intent where a gift card was claimed by its
// vault address
func (s *store) GetGiftCardClaimedIntent(ctx context.Context, giftCardVault string) (*intent.Record, error) {
model, err := dbGetGiftCardClaimedIntent(ctx, s.db, giftCardVault)
if err != nil {
return nil, err
}
return fromIntentModel(model), nil
}