Skip to content

Commit 2bf96d4

Browse files
author
Jeff Yanta
committed
Re-intorduce GetAllByOwner in intent store
1 parent 5cf36eb commit 2bf96d4

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

pkg/code/data/intent/memory/store.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package memory
33
import (
44
"context"
55
"errors"
6+
"sort"
67
"sync"
78
"time"
89

910
"github.com/code-payments/code-server/pkg/code/data/intent"
11+
"github.com/code-payments/code-server/pkg/database/query"
1012
)
1113

1214
type store struct {
@@ -73,6 +75,28 @@ func (s *store) findByState(state intent.State) []*intent.Record {
7375
return res
7476
}
7577

78+
func (s *store) findByOwner(owner string) []*intent.Record {
79+
res := make([]*intent.Record, 0)
80+
for _, item := range s.records {
81+
if item.InitiatorOwnerAccount == owner {
82+
res = append(res, item)
83+
continue
84+
}
85+
86+
if item.SendPublicPaymentMetadata != nil && item.SendPublicPaymentMetadata.DestinationOwnerAccount == owner {
87+
res = append(res, item)
88+
continue
89+
}
90+
91+
if item.ExternalDepositMetadata != nil && item.ExternalDepositMetadata.DestinationOwnerAccount == owner {
92+
res = append(res, item)
93+
continue
94+
}
95+
}
96+
97+
return res
98+
}
99+
76100
func (s *store) findByDestination(destination string) []*intent.Record {
77101
res := make([]*intent.Record, 0)
78102
for _, item := range s.records {
@@ -119,6 +143,38 @@ func (s *store) findByInitiatorAndType(intentType intent.Type, owner string) []*
119143
return res
120144
}
121145

146+
func (s *store) filter(items []*intent.Record, cursor query.Cursor, limit uint64, direction query.Ordering) []*intent.Record {
147+
var start uint64
148+
149+
start = 0
150+
if direction == query.Descending {
151+
start = s.last + 1
152+
}
153+
if len(cursor) > 0 {
154+
start = cursor.ToUint64()
155+
}
156+
157+
var res []*intent.Record
158+
for _, item := range items {
159+
if item.Id > start && direction == query.Ascending {
160+
res = append(res, item)
161+
}
162+
if item.Id < start && direction == query.Descending {
163+
res = append(res, item)
164+
}
165+
}
166+
167+
if direction == query.Descending {
168+
sort.Sort(sort.Reverse(ById(res)))
169+
}
170+
171+
if len(res) >= int(limit) {
172+
return res[:limit]
173+
}
174+
175+
return res
176+
}
177+
122178
func (s *store) filterByState(items []*intent.Record, include bool, states ...intent.State) []*intent.Record {
123179
var res []*intent.Record
124180

@@ -197,6 +253,23 @@ func (s *store) Get(ctx context.Context, intentID string) (*intent.Record, error
197253
return nil, intent.ErrIntentNotFound
198254
}
199255

256+
func (s *store) GetAllByOwner(ctx context.Context, owner string, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*intent.Record, error) {
257+
s.mu.Lock()
258+
defer s.mu.Unlock()
259+
260+
if items := s.findByOwner(owner); len(items) > 0 {
261+
res := s.filter(items, cursor, limit, direction)
262+
263+
if len(res) == 0 {
264+
return nil, intent.ErrIntentNotFound
265+
}
266+
267+
return res, nil
268+
}
269+
270+
return nil, intent.ErrIntentNotFound
271+
}
272+
200273
func (s *store) GetLatestByInitiatorAndType(ctx context.Context, intentType intent.Type, owner string) (*intent.Record, error) {
201274
s.mu.Lock()
202275
defer s.mu.Unlock()

pkg/code/data/intent/postgres/model.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/code-payments/code-server/pkg/currency"
1414

1515
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
16+
q "github.com/code-payments/code-server/pkg/database/query"
1617
)
1718

1819
const (
@@ -211,6 +212,29 @@ func dbGetIntent(ctx context.Context, db *sqlx.DB, intentID string) (*intentMode
211212
return res, nil
212213
}
213214

215+
func dbGetAllByOwner(ctx context.Context, db *sqlx.DB, owner string, cursor q.Cursor, limit uint64, direction q.Ordering) ([]*intentModel, error) {
216+
res := []*intentModel{}
217+
218+
query := `SELECT id, intent_id, intent_type, owner, source, destination_owner, destination, quantity, exchange_currency, exchange_rate, native_amount, usd_market_value, is_withdraw, is_deposit, is_remote_send, is_returned, is_issuer_voiding_gift_card, is_micro_payment, extended_metadata, state, created_at
219+
FROM ` + intentTableName + `
220+
WHERE owner = $1 OR destination_owner = $1
221+
`
222+
223+
opts := []any{owner}
224+
query, opts = q.PaginateQuery(query, opts, cursor, limit, direction)
225+
226+
err := db.SelectContext(ctx, &res, query, opts...)
227+
if err != nil {
228+
return nil, pgutil.CheckNoRows(err, intent.ErrIntentNotFound)
229+
}
230+
231+
if len(res) == 0 {
232+
return nil, intent.ErrIntentNotFound
233+
}
234+
235+
return res, nil
236+
}
237+
214238
func dbGetLatestByInitiatorAndType(ctx context.Context, db *sqlx.DB, intentType intent.Type, owner string) (*intentModel, error) {
215239
res := &intentModel{}
216240

pkg/code/data/intent/postgres/store.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77

88
"github.com/code-payments/code-server/pkg/code/data/intent"
9+
"github.com/code-payments/code-server/pkg/database/query"
910
"github.com/jmoiron/sqlx"
1011
)
1112

@@ -49,6 +50,23 @@ func (s *store) Get(ctx context.Context, intentID string) (*intent.Record, error
4950
return fromIntentModel(obj), nil
5051
}
5152

53+
// GetAllByOwner returns all records for a given owner (as both a source and destination).
54+
//
55+
// Returns ErrNotFound if no records are found.
56+
func (s *store) GetAllByOwner(ctx context.Context, owner string, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*intent.Record, error) {
57+
models, err := dbGetAllByOwner(ctx, s.db, owner, cursor, limit, direction)
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
intents := make([]*intent.Record, len(models))
63+
for i, model := range models {
64+
intents[i] = fromIntentModel(model)
65+
}
66+
67+
return intents, nil
68+
}
69+
5270
// GetLatestByInitiatorAndType gets the latest record by intent type and initiating owner
5371
//
5472
// Returns ErrNotFound if no records are found.

pkg/code/data/intent/store.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package intent
22

33
import (
44
"context"
5+
6+
"github.com/code-payments/code-server/pkg/database/query"
57
)
68

79
type Store interface {
@@ -13,6 +15,11 @@ type Store interface {
1315
// Returns ErrNotFound if no record is found.
1416
Get(ctx context.Context, intentID string) (*Record, error)
1517

18+
// GetAllByOwner returns all records for a given owner (as both a source and destination).
19+
//
20+
// Returns ErrNotFound if no records are found.
21+
GetAllByOwner(ctx context.Context, owner string, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*Record, error)
22+
1623
// GetLatestByInitiatorAndType gets the latest record by initiating owner and intent type
1724
//
1825
// Returns ErrNotFound if no records are found.

pkg/code/data/internal.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ type DatabaseData interface {
171171
SaveIntent(ctx context.Context, record *intent.Record) error
172172
GetIntent(ctx context.Context, intentID string) (*intent.Record, error)
173173
GetIntentBySignature(ctx context.Context, signature string) (*intent.Record, error)
174+
GetAllIntentsByOwner(ctx context.Context, owner string, opts ...query.Option) ([]*intent.Record, error)
174175
GetLatestIntentByInitiatorAndType(ctx context.Context, intentType intent.Type, owner string) (*intent.Record, error)
175176
GetOriginalGiftCardIssuedIntent(ctx context.Context, giftCardVault string) (*intent.Record, error)
176177
GetGiftCardClaimedIntent(ctx context.Context, giftCardVault string) (*intent.Record, error)
@@ -635,6 +636,14 @@ func (dp *DatabaseProvider) GetIntentBySignature(ctx context.Context, signature
635636

636637
return dp.intents.Get(ctx, fulfillmentRecord.Intent)
637638
}
639+
func (dp *DatabaseProvider) GetAllIntentsByOwner(ctx context.Context, owner string, opts ...query.Option) ([]*intent.Record, error) {
640+
req, err := query.DefaultPaginationHandler(opts...)
641+
if err != nil {
642+
return nil, err
643+
}
644+
645+
return dp.intents.GetAllByOwner(ctx, owner, req.Cursor, req.Limit, req.SortBy)
646+
}
638647
func (dp *DatabaseProvider) GetLatestIntentByInitiatorAndType(ctx context.Context, intentType intent.Type, owner string) (*intent.Record, error) {
639648
return dp.intents.GetLatestByInitiatorAndType(ctx, intentType, owner)
640649
}

0 commit comments

Comments
 (0)