Skip to content

Commit a87cdf2

Browse files
committed
feat: changed on the repository to fetch multiple quotes
1 parent 9a8c20e commit a87cdf2

File tree

6 files changed

+145
-12
lines changed

6 files changed

+145
-12
lines changed

internal/adapters/dataproviders/database/mongo/pegin.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,39 @@ func (repo *peginMongoRepository) GetQuote(ctx context.Context, hash string) (*q
6868
return &result.PeginQuote, nil
6969
}
7070

71+
func (repo *peginMongoRepository) GetQuotes(ctx context.Context, hashes []string) ([]quote.PeginQuote, error) {
72+
var result StoredPeginQuote
73+
dbCtx, cancel := context.WithTimeout(ctx, repo.conn.timeout)
74+
defer cancel()
75+
76+
for _, hash := range hashes {
77+
if err := quote.ValidateQuoteHash(hash); err != nil {
78+
return nil, err
79+
}
80+
}
81+
82+
collection := repo.conn.Collection(PeginQuoteCollection)
83+
filter := bson.M{"hash": bson.M{"$in": hashes}}
84+
85+
quotesReturn := make([]quote.PeginQuote, 0)
86+
87+
cursor, err := collection.Find(dbCtx, filter)
88+
if errors.Is(err, mongo.ErrNoDocuments) {
89+
return nil, nil
90+
} else if err != nil {
91+
return nil, err
92+
}
93+
for cursor.Next(ctx) {
94+
err := cursor.Decode(&result)
95+
if err != nil {
96+
return nil, err
97+
}
98+
quotesReturn = append(quotesReturn, result.PeginQuote)
99+
}
100+
logDbInteraction(Read, quotesReturn)
101+
return quotesReturn, nil
102+
}
103+
71104
func (repo *peginMongoRepository) GetRetainedQuote(ctx context.Context, hash string) (*quote.RetainedPeginQuote, error) {
72105
var result quote.RetainedPeginQuote
73106
dbCtx, cancel := context.WithTimeout(ctx, repo.conn.timeout)

internal/adapters/dataproviders/database/mongo/pegin_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,45 @@ func TestPeginMongoRepository_DeleteQuotes(t *testing.T) {
338338
assert.Zero(t, count)
339339
})
340340
}
341+
342+
func TestPeginMongoRepository_GetQuotes(t *testing.T) {
343+
t.Run("Successfully retrieves quotes", func(t *testing.T) {
344+
client, collection := getClientAndCollectionMocks(mongo.PeginQuoteCollection)
345+
log.SetLevel(log.DebugLevel)
346+
hashes := []string{testRetainedPegoutQuote.QuoteHash}
347+
repo := mongo.NewPeginMongoRepository(mongo.NewConnection(client, time.Duration(1)))
348+
collection.On("Find", mock.Anything,
349+
bson.M{"hash": bson.M{"$in": hashes}},
350+
).Return(mongoDb.NewCursorFromDocuments([]any{testPeginQuote}, nil, nil)).Once()
351+
result, err := repo.GetQuotes(context.Background(), hashes)
352+
collection.AssertExpectations(t)
353+
require.NoError(t, err)
354+
assert.Equal(t, []quote.PeginQuote{testPeginQuote}, result)
355+
})
356+
357+
t.Run("Fails validation for hashes", func(t *testing.T) {
358+
client, _ := getClientAndCollectionMocks(mongo.PeginQuoteCollection)
359+
360+
invalidHashes := []string{"invalidHash"}
361+
conn := mongo.NewConnection(client, time.Duration(1))
362+
repo := mongo.NewPeginMongoRepository(conn)
363+
364+
_, err := repo.GetQuotes(context.Background(), invalidHashes)
365+
require.Error(t, err)
366+
assert.Equal(t, "invalid quote hash length: expected 64 characters, got 11", err.Error())
367+
})
368+
369+
t.Run("No quotes found", func(t *testing.T) {
370+
client, collection := getClientAndCollectionMocks(mongo.PeginQuoteCollection)
371+
372+
expectedHashes := []string{testRetainedPegoutQuote.QuoteHash}
373+
collection.On("Find", mock.Anything, bson.M{"hash": bson.M{"$in": expectedHashes}}).Return(nil, mongoDb.ErrNoDocuments).Once()
374+
375+
conn := mongo.NewConnection(client, time.Duration(1))
376+
repo := mongo.NewPeginMongoRepository(conn)
377+
378+
quotes, err := repo.GetQuotes(context.Background(), expectedHashes)
379+
require.NoError(t, err)
380+
assert.Nil(t, quotes)
381+
})
382+
}

internal/entities/quote/pegin_quote.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
type PeginQuoteRepository interface {
2828
InsertQuote(ctx context.Context, hash string, quote PeginQuote) error
2929
GetQuote(ctx context.Context, hash string) (*PeginQuote, error)
30+
GetQuotes(ctx context.Context, hashes []string) ([]PeginQuote, error)
3031
GetRetainedQuote(ctx context.Context, hash string) (*RetainedPeginQuote, error)
3132
InsertRetainedQuote(ctx context.Context, quote RetainedPeginQuote) error
3233
UpdateRetainedQuote(ctx context.Context, quote RetainedPeginQuote) error

internal/usecases/pegin/get_pegin_report.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,14 @@ func (useCase *GetPeginReportUseCase) Run(ctx context.Context) (GetPeginReportRe
5353
}, nil
5454
}
5555

56-
quotes := make([]quote.PeginQuote, 0)
56+
quoteHashes := make([]string, 0)
5757
for _, q := range retained {
58-
var peginQuote *quote.PeginQuote
59-
peginQuote, err = useCase.peginQuoteRepository.GetQuote(ctx, q.QuoteHash)
60-
if err != nil {
61-
return GetPeginReportResult{}, usecases.WrapUseCaseError(usecases.GetPeginReportId, err)
62-
}
63-
if peginQuote != nil {
64-
quotes = append(quotes, *peginQuote)
65-
}
58+
quoteHashes = append(quoteHashes, q.QuoteHash)
59+
}
60+
61+
quotes, err := useCase.peginQuoteRepository.GetQuotes(ctx, quoteHashes)
62+
if err != nil {
63+
return GetPeginReportResult{}, usecases.WrapUseCaseError(usecases.GetPeginReportId, err)
6664
}
6765

6866
minimumQuoteValue = useCase.calculateMinimumQuoteValue(quotes)

internal/usecases/pegin/get_pegin_report_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ func TestGetPeginReportUseCase_Run(t *testing.T) {
2727
{QuoteHash: "hash10"},
2828
}
2929

30+
quoteHashes := []string{"hash1", "hash2", "hash3", "hash4", "hash5", "hash6", "hash7", "hash8", "hash9", "hash10"}
31+
3032
peginQuotes := []quote.PeginQuote{
3133
{Value: entities.NewWei(1000), CallFee: entities.NewWei(10)},
3234
{Value: entities.NewWei(2000), CallFee: entities.NewWei(20)},
@@ -51,9 +53,7 @@ func TestGetPeginReportUseCase_Run(t *testing.T) {
5153
peginQuoteRepository.On("GetRetainedQuoteByState", ctx, quote.PeginStateRegisterPegInSucceeded).
5254
Return(retainedQuotes, nil).Once()
5355

54-
for i, retainedQuote := range retainedQuotes {
55-
peginQuoteRepository.On("GetQuote", ctx, retainedQuote.QuoteHash).Return(&peginQuotes[i], nil).Once()
56-
}
56+
peginQuoteRepository.On("GetQuotes", ctx, quoteHashes).Return(peginQuotes, nil).Once()
5757

5858
useCase := pegin.NewGetPeginReportUseCase(peginQuoteRepository)
5959

test/mocks/pegin_quote_repository_mock.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)