Skip to content

Commit

Permalink
fix: add sources, destinations and operations to get transactions; 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinezAvellan committed Jan 23, 2025
1 parent 6421ab1 commit f9afbef
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
16 changes: 16 additions & 0 deletions components/transaction/internal/adapters/http/in/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,22 @@ func (handler *TransactionHandler) GetTransaction(c *fiber.Ctx) error {
return http.WithError(c, err)
}

ctxGetTransaction, spanGetTransaction := tracer.Start(ctx, "handler.get_transaction.get_operations")

headerParams, err := http.ValidateParameters(c.Queries())
headerParams.Metadata = &bson.M{}

tran, err = handler.Query.GetOperationsByTransaction(ctxGetTransaction, organizationID, ledgerID, tran, *headerParams)
if err != nil {
mopentelemetry.HandleSpanError(&spanGetTransaction, "Failed to retrieve Operations", err)

logger.Errorf("Failed to retrieve Operations with ID: %s, Error: %s", tran.ID, err.Error())

return http.WithError(c, err)
}

spanGetTransaction.End()

logger.Infof("Successfully retrieved Transaction with ID: %s", transactionID.String())

return http.OK(c, tran)
Expand Down
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
}
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{})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,53 @@ func (uc *UseCase) GetAllTransactions(ctx context.Context, organizationID, ledge
for i := range trans {
if data, ok := metadataMap[trans[i].ID]; ok {
trans[i].Metadata = data
trans[i], err = uc.GetOperationsByTransaction(ctx, organizationID, ledgerID, trans[i], filter)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to get operations to transaction by id", err)

return nil, http.CursorPagination{}, pkg.ValidateBusinessError(constant.ErrNoOperationsFound, reflect.TypeOf(transaction.Transaction{}).Name())
}
}
}
}

return trans, cur, nil
}

func (uc *UseCase) GetOperationsByTransaction(ctx context.Context, organizationID, ledgerID uuid.UUID, tran *transaction.Transaction, filter http.QueryHeader) (*transaction.Transaction, error) {
logger := pkg.NewLoggerFromContext(ctx)
tracer := pkg.NewTracerFromContext(ctx)

ctx, span := tracer.Start(ctx, "query.get_all_transactions_get_operations")
defer span.End()

logger.Infof("Retrieving Operations")

operations, _, err := uc.GetAllOperations(ctx, organizationID, ledgerID, tran.IDtoUUID(), filter)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to retrieve Operations", err)

logger.Errorf("Failed to retrieve Operations with ID: %s, Error: %s", tran.IDtoUUID(), err.Error())

return nil, err
}

source := make([]string, 0)
destination := make([]string, 0)

for _, op := range operations {
if op.Type == constant.DEBIT {
source = append(source, op.AccountAlias)
} else {
destination = append(destination, op.AccountAlias)
}
}

span.End()

tran.Source = source
tran.Destination = destination
tran.Operations = operations

return tran, nil
}

0 comments on commit f9afbef

Please sign in to comment.