-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #480 from LerianStudio/fix/MIDAZ-476
Fix/476
- Loading branch information
Showing
5 changed files
with
202 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
components/transaction/internal/services/query/get-all-operations.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package query | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"reflect" | ||
|
||
"github.com/LerianStudio/midaz/components/transaction/internal/adapters/postgres/operation" | ||
"github.com/LerianStudio/midaz/components/transaction/internal/services" | ||
"github.com/LerianStudio/midaz/pkg" | ||
"github.com/LerianStudio/midaz/pkg/constant" | ||
"github.com/LerianStudio/midaz/pkg/mopentelemetry" | ||
"github.com/LerianStudio/midaz/pkg/net/http" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
func (uc *UseCase) GetAllOperations(ctx context.Context, organizationID, ledgerID, transactionID uuid.UUID, filter http.QueryHeader) ([]*operation.Operation, http.CursorPagination, error) { | ||
logger := pkg.NewLoggerFromContext(ctx) | ||
tracer := pkg.NewTracerFromContext(ctx) | ||
|
||
ctx, span := tracer.Start(ctx, "query.get_all_operations") | ||
defer span.End() | ||
|
||
logger.Infof("Retrieving operations by account") | ||
|
||
op, cur, err := uc.OperationRepo.FindAll(ctx, organizationID, ledgerID, transactionID, filter.ToCursorPagination()) | ||
if err != nil { | ||
mopentelemetry.HandleSpanError(&span, "Failed to get all operations on repo", err) | ||
|
||
logger.Errorf("Error getting all operations on repo: %v", err) | ||
|
||
if errors.Is(err, services.ErrDatabaseItemNotFound) { | ||
return nil, http.CursorPagination{}, pkg.ValidateBusinessError(constant.ErrNoOperationsFound, reflect.TypeOf(operation.Operation{}).Name()) | ||
} | ||
|
||
return nil, http.CursorPagination{}, err | ||
} | ||
|
||
if op != nil { | ||
metadata, err := uc.MetadataRepo.FindList(ctx, reflect.TypeOf(operation.Operation{}).Name(), filter) | ||
if err != nil { | ||
mopentelemetry.HandleSpanError(&span, "Failed to get metadata on mongodb operation", err) | ||
|
||
return nil, http.CursorPagination{}, pkg.ValidateBusinessError(constant.ErrNoOperationsFound, reflect.TypeOf(operation.Operation{}).Name()) | ||
} | ||
|
||
metadataMap := make(map[string]map[string]any, len(metadata)) | ||
|
||
for _, meta := range metadata { | ||
metadataMap[meta.EntityID] = meta.Data | ||
} | ||
|
||
for i := range op { | ||
if data, ok := metadataMap[op[i].ID]; ok { | ||
op[i].Metadata = data | ||
} | ||
} | ||
} | ||
|
||
return op, cur, nil | ||
} |
71 changes: 71 additions & 0 deletions
71
components/transaction/internal/services/query/get-all-operations_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package query | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/LerianStudio/midaz/pkg/net/http" | ||
"go.uber.org/mock/gomock" | ||
"testing" | ||
"time" | ||
|
||
"github.com/LerianStudio/midaz/components/transaction/internal/adapters/postgres/operation" | ||
"github.com/LerianStudio/midaz/pkg" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestGetAllOperations is responsible to test GetAllOperations with success and error | ||
func TestGetAllOperations(t *testing.T) { | ||
organizationID := pkg.GenerateUUIDv7() | ||
ledgerID := pkg.GenerateUUIDv7() | ||
transactionID := pkg.GenerateUUIDv7() | ||
filter := http.QueryHeader{ | ||
Limit: 10, | ||
Page: 1, | ||
SortOrder: "asc", | ||
StartDate: time.Now().AddDate(0, -1, 0), | ||
EndDate: time.Now(), | ||
ToAssetCodes: []string{"BRL"}, | ||
} | ||
mockCur := http.CursorPagination{ | ||
Next: "next", | ||
Prev: "prev", | ||
} | ||
|
||
t.Parallel() | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
mockOperationRepo := operation.NewMockRepository(ctrl) | ||
|
||
uc := UseCase{ | ||
OperationRepo: mockOperationRepo, | ||
} | ||
|
||
t.Run("Success", func(t *testing.T) { | ||
trans := []*operation.Operation{{}} | ||
mockOperationRepo. | ||
EXPECT(). | ||
FindAll(gomock.Any(), organizationID, ledgerID, transactionID, filter.ToCursorPagination()). | ||
Return(trans, mockCur, nil). | ||
Times(1) | ||
res, cur, err := uc.OperationRepo.FindAll(context.TODO(), organizationID, ledgerID, transactionID, filter.ToCursorPagination()) | ||
|
||
assert.NoError(t, err) | ||
assert.Len(t, res, 1) | ||
assert.NotNil(t, cur) | ||
}) | ||
|
||
t.Run("Error", func(t *testing.T) { | ||
errMsg := "errDatabaseItemNotFound" | ||
mockOperationRepo. | ||
EXPECT(). | ||
FindAll(gomock.Any(), organizationID, ledgerID, transactionID, filter.ToCursorPagination()). | ||
Return(nil, http.CursorPagination{}, errors.New(errMsg)). | ||
Times(1) | ||
res, cur, err := uc.OperationRepo.FindAll(context.TODO(), organizationID, ledgerID, transactionID, filter.ToCursorPagination()) | ||
|
||
assert.EqualError(t, err, errMsg) | ||
assert.Nil(t, res) | ||
assert.Equal(t, cur, http.CursorPagination{}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters