From 36ae9985b908784ea59669087e99cc56e9399f14 Mon Sep 17 00:00:00 2001 From: Brecci Date: Thu, 14 Nov 2024 18:05:00 -0300 Subject: [PATCH 1/8] fix: update transaction error messages to comply with gitbook :bug: --- common/constant/errors.go | 2 + common/errors.go | 56 ++++++++----------- common/gold/transaction/model/validations.go | 31 ++-------- .../database/mongodb/metadata.mongodb.go | 6 +- .../postgres/transaction.postgresql.go | 29 ++-------- .../app/command/update-transaction.go | 16 +----- .../query/get-all-metadata-transactions.go | 17 ++---- .../app/query/get-all-operations-account.go | 8 +-- .../app/query/get-all-transactions.go | 15 +---- .../internal/ports/http/transaction.go | 14 ++--- 10 files changed, 54 insertions(+), 140 deletions(-) diff --git a/common/constant/errors.go b/common/constant/errors.go index 28760029..ee6fab93 100644 --- a/common/constant/errors.go +++ b/common/constant/errors.go @@ -77,4 +77,6 @@ var ( ErrInvalidMetadataNesting = errors.New("0067") ErrOperationIDNotFound = errors.New("0068") ErrNoOperationsFound = errors.New("0069") + ErrTransactionIDNotFound = errors.New("0070") + ErrNoTransactionsFound = errors.New("0071") ) diff --git a/common/errors.go b/common/errors.go index efa01aad..e1809aeb 100644 --- a/common/errors.go +++ b/common/errors.go @@ -18,28 +18,6 @@ type EntityNotFoundError struct { Err error } -// NewEntityNotFoundError creates an instance of EntityNotFoundError. -func NewEntityNotFoundError(entityType string) EntityNotFoundError { - return EntityNotFoundError{ - EntityType: entityType, - Code: "", - Title: "", - Message: "", - Err: nil, - } -} - -// WrapEntityNotFoundError creates an instance of EntityNotFoundError. -func WrapEntityNotFoundError(entityType string, err error) EntityNotFoundError { - return EntityNotFoundError{ - EntityType: entityType, - Code: "", - Title: "", - Message: "", - Err: err, - } -} - // Error implements the error interface. func (e EntityNotFoundError) Error() string { if strings.TrimSpace(e.Message) == "" { @@ -409,7 +387,7 @@ func ValidateBusinessError(err error, entityType string, args ...any) error { Title: "Duplicate Product Name Error", Message: fmt.Sprintf("A product with the name %s already exists for this ledger ID %s. Please try again with a different ledger or name.", args...), }, - cn.ErrBalanceRemainingDeletion: EntityConflictError{ + cn.ErrBalanceRemainingDeletion: UnprocessableOperationError{ EntityType: entityType, Code: cn.ErrBalanceRemainingDeletion.Error(), Title: "Balance Remaining Deletion Error", @@ -421,13 +399,13 @@ func ValidateBusinessError(err error, entityType string, args ...any) error { Title: "Invalid Script Format Error", Message: "The script provided in your request is invalid or in an unsupported format. Please verify the script format and try again.", }, - cn.ErrInsufficientFunds: EntityConflictError{ + cn.ErrInsufficientFunds: UnprocessableOperationError{ EntityType: entityType, Code: cn.ErrInsufficientFunds.Error(), Title: "Insufficient Funds Error", Message: "The transaction could not be completed due to insufficient funds in the account. Please add sufficient funds to your account and try again.", }, - cn.ErrAccountIneligibility: EntityConflictError{ + cn.ErrAccountIneligibility: UnprocessableOperationError{ EntityType: entityType, Code: cn.ErrAccountIneligibility.Error(), Title: "Account Ineligibility Error", @@ -439,37 +417,37 @@ func ValidateBusinessError(err error, entityType string, args ...any) error { Title: "Alias Unavailability Error", Message: fmt.Sprintf("The alias %s is already in use. Please choose a different alias and try again.", args...), }, - cn.ErrParentTransactionIDNotFound: EntityConflictError{ + cn.ErrParentTransactionIDNotFound: EntityNotFoundError{ EntityType: entityType, Code: cn.ErrParentTransactionIDNotFound.Error(), Title: "Parent Transaction ID Not Found", Message: fmt.Sprintf("The parentTransactionId %s does not correspond to any existing transaction. Please review the ID and try again.", args...), }, - cn.ErrImmutableField: EntityConflictError{ + cn.ErrImmutableField: ValidationError{ EntityType: entityType, Code: cn.ErrImmutableField.Error(), Title: "Immutable Field Error", Message: fmt.Sprintf("The %s field cannot be modified. Please remove this field from your request and try again.", args...), }, - cn.ErrTransactionTimingRestriction: EntityConflictError{ + cn.ErrTransactionTimingRestriction: UnprocessableOperationError{ EntityType: entityType, Code: cn.ErrTransactionTimingRestriction.Error(), Title: "Transaction Timing Restriction", Message: fmt.Sprintf("You can only perform another transaction using %s of %f from %s to %s after %s. Please wait until the specified time to try again.", args...), }, - cn.ErrAccountStatusTransactionRestriction: EntityConflictError{ + cn.ErrAccountStatusTransactionRestriction: ValidationError{ EntityType: entityType, Code: cn.ErrAccountStatusTransactionRestriction.Error(), Title: "Account Status Transaction Restriction", Message: "The current statuses of the source and/or destination accounts do not permit transactions. Change the account status(es) and try again.", }, - cn.ErrInsufficientAccountBalance: EntityConflictError{ + cn.ErrInsufficientAccountBalance: ValidationError{ EntityType: entityType, Code: cn.ErrInsufficientAccountBalance.Error(), Title: "Insufficient Account Balance Error", Message: fmt.Sprintf("The account %s does not have sufficient balance. Please try again with an amount that is less than or equal to the available balance.", args...), }, - cn.ErrTransactionMethodRestriction: EntityConflictError{ + cn.ErrTransactionMethodRestriction: ValidationError{ EntityType: entityType, Code: cn.ErrTransactionMethodRestriction.Error(), Title: "Transaction Method Restriction", @@ -703,18 +681,30 @@ func ValidateBusinessError(err error, entityType string, args ...any) error { Title: "Invalid Metadata Nesting", Message: fmt.Sprintf("The metadata object cannot contain nested values. Please ensure that the value %s is not nested and try again.", args...), }, - cn.ErrOperationIDNotFound: ValidationError{ + cn.ErrOperationIDNotFound: EntityNotFoundError{ EntityType: entityType, Code: cn.ErrOperationIDNotFound.Error(), Title: "Operation ID Not Found", Message: "The provided operation ID does not exist in our records. Please verify the operation ID and try again.", }, - cn.ErrNoOperationsFound: ValidationError{ + cn.ErrNoOperationsFound: EntityNotFoundError{ EntityType: entityType, Code: cn.ErrNoOperationsFound.Error(), Title: "No Operations Found", Message: "No operations were found in the search. Please review the search criteria and try again.", }, + cn.ErrTransactionIDNotFound: EntityNotFoundError{ + EntityType: entityType, + Code: cn.ErrTransactionIDNotFound.Error(), + Title: "Transaction ID Not Found", + Message: "The provided transaction ID does not exist in our records. Please verify the transaction ID and try again.", + }, + cn.ErrNoTransactionsFound: EntityNotFoundError{ + EntityType: entityType, + Code: cn.ErrNoTransactionsFound.Error(), + Title: "No Transactions Found", + Message: "No transactions were found in the search. Please review the search criteria and try again.", + }, } if mappedError, found := errorMap[err]; found { diff --git a/common/gold/transaction/model/validations.go b/common/gold/transaction/model/validations.go index 1948da40..80404d6a 100644 --- a/common/gold/transaction/model/validations.go +++ b/common/gold/transaction/model/validations.go @@ -7,47 +7,32 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/constant" + cn "github.com/LerianStudio/midaz/common/constant" a "github.com/LerianStudio/midaz/common/mgrpc/account" ) // ValidateAccounts function with some validates in accounts and DSL operations func ValidateAccounts(validate Responses, accounts []*a.Account) error { if len(accounts) != (len(validate.From) + len(validate.To)) { - return common.ValidationError{ - Code: "0019", - Title: "Transaction Participation Error", - Message: "One or more accounts listed in the transaction statement are ineligible to participate. Please review the account statuses and try again.", - } + return common.ValidateBusinessError(cn.ErrAccountIneligibility, "ValidateAccounts") } for _, acc := range accounts { for key := range validate.From { if acc.Id == key || acc.Alias == key && acc.AllowSending { if acc.Balance.Available <= 0 && !strings.Contains(acc.Alias, "@external") { - return common.ValidationError{ - Code: "0025", - Title: "Insuficient balance", - Message: strings.ReplaceAll("The account {Id} has insufficient balance. Try again sending the amount minor or equal to the available balance.", "{Id}", acc.Alias), - } + return common.ValidateBusinessError(cn.ErrInsufficientAccountBalance, "ValidateAccounts", acc.Alias) } } if acc.Id == key || acc.Alias == key && !acc.AllowSending { - return common.ValidationError{ - Code: "0019", - Title: "Transaction Participation Error", - Message: "One or more accounts listed in the transaction statement are ineligible to participate. Please review the account statuses and try again.", - } + return common.ValidateBusinessError(cn.ErrAccountIneligibility, "ValidateAccounts") } } for key := range validate.To { if acc.Id == key || acc.Alias == key && !acc.AllowReceiving { - return common.ValidationError{ - Code: "0019", - Title: "Transaction Participation Error", - Message: "One or more accounts listed in the transaction statement are ineligible to participate. Please review the account statuses and try again.", - } + return common.ValidateBusinessError(cn.ErrAccountIneligibility, "ValidateAccounts") } } } @@ -351,11 +336,7 @@ func ValidateSendSourceAndDistribute(transaction Transaction) (*Responses, error response.Aliases = append(response.Aliases, response.Destinations...) if math.Abs(float64(sourcesTotal)-float64(destinationsTotal)) > 0.00001 { - return nil, common.ValidationError{ - Code: "0018", - Title: "Insufficient Funds", - Message: "The transaction could not be completed due to insufficient funds in the account. Please add funds to your account and try again.", - } + return nil, common.ValidateBusinessError(cn.ErrInsufficientFunds, "ValidateSendSourceAndDistribute") } return response, nil diff --git a/components/transaction/internal/adapters/database/mongodb/metadata.mongodb.go b/components/transaction/internal/adapters/database/mongodb/metadata.mongodb.go index ba618dc5..c5edd1b9 100644 --- a/components/transaction/internal/adapters/database/mongodb/metadata.mongodb.go +++ b/components/transaction/internal/adapters/database/mongodb/metadata.mongodb.go @@ -3,6 +3,7 @@ package mongodb import ( "context" "errors" + cn "github.com/LerianStudio/midaz/common/constant" "github.com/LerianStudio/midaz/common/mopentelemetry" "strings" "time" @@ -200,10 +201,7 @@ func (mmr *MetadataMongoDBRepository) Update(ctx context.Context, collection, id mopentelemetry.HandleSpanError(&spanUpdate, "Failed to update metadata", err) if errors.Is(err, mongo.ErrNoDocuments) { - return common.EntityNotFoundError{ - EntityType: collection, - Err: err, - } + return common.ValidateBusinessError(cn.ErrEntityNotFound, collection) } return err diff --git a/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go b/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go index 4f38f508..d7dd80bd 100644 --- a/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go +++ b/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "errors" + cn "github.com/LerianStudio/midaz/common/constant" "github.com/LerianStudio/midaz/common/mopentelemetry" "reflect" "strconv" @@ -98,12 +99,7 @@ func (r *TransactionPostgreSQLRepository) Create(ctx context.Context, transactio } if rowsAffected == 0 { - err := common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Title: "Entity not found.", - Code: "0007", - Message: "No entity was found matching the provided ID. Ensure the correct ID is being used for the entity you are attempting to manage.", - } + err := common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(t.Transaction{}).Name()) mopentelemetry.HandleSpanError(&span, "Failed to create transaction. Rows affected is 0", err) @@ -301,12 +297,7 @@ func (r *TransactionPostgreSQLRepository) Find(ctx context.Context, organization mopentelemetry.HandleSpanError(&span, "Failed to scan row", err) if errors.Is(err, sql.ErrNoRows) { - return nil, common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Title: "Entity not found.", - Code: "0007", - Message: "No entity was found matching the provided ID. Ensure the correct ID is being used for the entity you are attempting to manage.", - } + return nil, common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(t.Transaction{}).Name()) } return nil, err @@ -387,12 +378,7 @@ func (r *TransactionPostgreSQLRepository) Update(ctx context.Context, organizati } if rowsAffected == 0 { - err := common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Title: "Entity not found.", - Code: "0007", - Message: "No entity was found matching the provided ID. Ensure the correct ID is being used for the entity you are attempting to manage.", - } + err := common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(t.Transaction{}).Name()) mopentelemetry.HandleSpanError(&span, "Failed to update transaction. Rows affected is 0", err) @@ -436,12 +422,7 @@ func (r *TransactionPostgreSQLRepository) Delete(ctx context.Context, organizati } if rowsAffected == 0 { - err := common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Title: "Entity not found.", - Code: "0007", - Message: "No entity was found matching the provided ID. Ensure the correct ID is being used for the entity you are attempting to manage.", - } + err := common.ValidateBusinessError(cn.ErrEntityNotFound, reflect.TypeOf(t.Transaction{}).Name()) mopentelemetry.HandleSpanError(&span, "Failed to delete transaction. Rows affected is 0", err) diff --git a/components/transaction/internal/app/command/update-transaction.go b/components/transaction/internal/app/command/update-transaction.go index 3662a276..684c8d0b 100644 --- a/components/transaction/internal/app/command/update-transaction.go +++ b/components/transaction/internal/app/command/update-transaction.go @@ -3,7 +3,7 @@ package command import ( "context" "errors" - "fmt" + cn "github.com/LerianStudio/midaz/common/constant" "github.com/LerianStudio/midaz/common/mopentelemetry" "reflect" @@ -34,12 +34,7 @@ func (uc *UseCase) UpdateTransaction(ctx context.Context, organizationID, ledger logger.Errorf("Error updating transaction on repo by id: %v", err) if errors.Is(err, app.ErrDatabaseItemNotFound) { - return nil, common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Message: fmt.Sprintf("Transaction with id %s was not found", transactionID.String()), - Code: "TRANSACTION_NOT_FOUND", - Err: err, - } + return nil, common.ValidateBusinessError(cn.ErrTransactionIDNotFound, reflect.TypeOf(t.Transaction{}).Name()) } return nil, err @@ -91,12 +86,7 @@ func (uc *UseCase) UpdateTransactionStatus(ctx context.Context, organizationID, logger.Errorf("Error updating status transaction on repo by id: %v", err) if errors.Is(err, app.ErrDatabaseItemNotFound) { - return nil, common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Message: fmt.Sprintf("Transaction with id %s was not found", transactionID.String()), - Code: "TRANSACTION_NOT_FOUND", - Err: err, - } + return nil, common.ValidateBusinessError(cn.ErrTransactionIDNotFound, reflect.TypeOf(t.Transaction{}).Name()) } return nil, err diff --git a/components/transaction/internal/app/query/get-all-metadata-transactions.go b/components/transaction/internal/app/query/get-all-metadata-transactions.go index 21548b36..ecfe6064 100644 --- a/components/transaction/internal/app/query/get-all-metadata-transactions.go +++ b/components/transaction/internal/app/query/get-all-metadata-transactions.go @@ -3,6 +3,7 @@ package query import ( "context" "errors" + cn "github.com/LerianStudio/midaz/common/constant" "github.com/LerianStudio/midaz/common/mopentelemetry" "reflect" @@ -13,7 +14,7 @@ import ( "github.com/google/uuid" ) -// GetAllMetadataTransactions fetch all Transacntions from the repository +// GetAllMetadataTransactions fetch all Transactions from the repository func (uc *UseCase) GetAllMetadataTransactions(ctx context.Context, organizationID, ledgerID uuid.UUID, filter commonHTTP.QueryHeader) ([]*t.Transaction, error) { logger := common.NewLoggerFromContext(ctx) tracer := common.NewTracerFromContext(ctx) @@ -27,12 +28,7 @@ func (uc *UseCase) GetAllMetadataTransactions(ctx context.Context, organizationI if err != nil || metadata == nil { mopentelemetry.HandleSpanError(&span, "Failed to get transactions on repo by metadata", err) - return nil, common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Message: "Transactions by metadata was not found", - Code: "TRANSACTION_NOT_FOUND", - Err: err, - } + return nil, common.ValidateBusinessError(cn.ErrNoTransactionsFound, reflect.TypeOf(t.Transaction{}).Name()) } uuids := make([]uuid.UUID, len(metadata)) @@ -50,12 +46,7 @@ func (uc *UseCase) GetAllMetadataTransactions(ctx context.Context, organizationI logger.Errorf("Error getting transactions on repo by query params: %v", err) if errors.Is(err, app.ErrDatabaseItemNotFound) { - return nil, common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Message: "Transactions by metadata was not found", - Code: "TRANSACTION_NOT_FOUND", - Err: err, - } + return nil, common.ValidateBusinessError(cn.ErrNoTransactionsFound, reflect.TypeOf(t.Transaction{}).Name()) } return nil, err diff --git a/components/transaction/internal/app/query/get-all-operations-account.go b/components/transaction/internal/app/query/get-all-operations-account.go index 923ac1d8..c657c1dd 100644 --- a/components/transaction/internal/app/query/get-all-operations-account.go +++ b/components/transaction/internal/app/query/get-all-operations-account.go @@ -3,6 +3,7 @@ package query import ( "context" "errors" + cn "github.com/LerianStudio/midaz/common/constant" "github.com/LerianStudio/midaz/common/mopentelemetry" "reflect" @@ -29,12 +30,7 @@ func (uc *UseCase) GetAllOperationsByAccount(ctx context.Context, organizationID logger.Errorf("Error getting operations on repo: %v", err) if errors.Is(err, app.ErrDatabaseItemNotFound) { - return nil, common.EntityNotFoundError{ - EntityType: reflect.TypeOf(o.Operation{}).Name(), - Message: "Operation was not found", - Code: "OPERATION_NOT_FOUND", - Err: err, - } + return nil, common.ValidateBusinessError(cn.ErrNoOperationsFound, reflect.TypeOf(o.Operation{}).Name()) } return nil, err diff --git a/components/transaction/internal/app/query/get-all-transactions.go b/components/transaction/internal/app/query/get-all-transactions.go index c3cd3d2d..16c58f66 100644 --- a/components/transaction/internal/app/query/get-all-transactions.go +++ b/components/transaction/internal/app/query/get-all-transactions.go @@ -3,6 +3,7 @@ package query import ( "context" "errors" + cn "github.com/LerianStudio/midaz/common/constant" "github.com/LerianStudio/midaz/common/mopentelemetry" "reflect" @@ -30,12 +31,7 @@ func (uc *UseCase) GetAllTransactions(ctx context.Context, organizationID, ledge logger.Errorf("Error getting transactions on repo: %v", err) if errors.Is(err, app.ErrDatabaseItemNotFound) { - return nil, common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Message: "Transaction was not found", - Code: "TRANSACTION_NOT_FOUND", - Err: err, - } + return nil, common.ValidateBusinessError(cn.ErrNoTransactionsFound, reflect.TypeOf(t.Transaction{}).Name()) } return nil, err @@ -46,12 +42,7 @@ func (uc *UseCase) GetAllTransactions(ctx context.Context, organizationID, ledge if err != nil { mopentelemetry.HandleSpanError(&span, "Failed to get metadata on mongodb transaction", err) - return nil, common.EntityNotFoundError{ - EntityType: reflect.TypeOf(t.Transaction{}).Name(), - Message: "Metadata was not found", - Code: "TRANSACTION_NOT_FOUND", - Err: err, - } + return nil, common.ValidateBusinessError(cn.ErrNoTransactionsFound, reflect.TypeOf(t.Transaction{}).Name()) } metadataMap := make(map[string]map[string]any, len(metadata)) diff --git a/components/transaction/internal/ports/http/transaction.go b/components/transaction/internal/ports/http/transaction.go index 67d4d5b4..4d960c14 100644 --- a/components/transaction/internal/ports/http/transaction.go +++ b/components/transaction/internal/ports/http/transaction.go @@ -6,9 +6,11 @@ import ( "github.com/google/uuid" "go.opentelemetry.io/otel/trace" "net/http" + "reflect" "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/constant" + cn "github.com/LerianStudio/midaz/common/constant" "github.com/LerianStudio/midaz/common/gold/transaction" gold "github.com/LerianStudio/midaz/common/gold/transaction/model" "github.com/LerianStudio/midaz/common/mgrpc/account" @@ -75,11 +77,7 @@ func (handler *TransactionHandler) CreateTransactionDSL(c *fiber.Ctx) error { errListener := transaction.Validate(dsl) if errListener != nil && len(errListener.Errors) > 0 { - err := common.ValidationError{ - Code: "0017", - Title: "Invalid Script Error", - Message: "The script provided in the request is invalid or in an unsupported format. Please verify the script and try again.", - } + err := common.ValidateBusinessError(cn.ErrInvalidScriptFormat, reflect.TypeOf(t.Transaction{}).Name()) mopentelemetry.HandleSpanError(&span, "Failed to validate script in DSL", err) @@ -90,11 +88,7 @@ func (handler *TransactionHandler) CreateTransactionDSL(c *fiber.Ctx) error { parserDSL, ok := parsed.(gold.Transaction) if !ok { - err := common.ValidationError{ - Code: "0017", - Title: "Invalid Script Error", - Message: "The script provided in the request is invalid or in an unsupported format. Please verify the script and try again.", - } + err := common.ValidateBusinessError(cn.ErrInvalidScriptFormat, reflect.TypeOf(t.Transaction{}).Name()) mopentelemetry.HandleSpanError(&span, "Failed to parse script in DSL", err) From 1065e33d9b0114a269e8ea2b2e95b24bf207babe Mon Sep 17 00:00:00 2001 From: Brecci Date: Thu, 14 Nov 2024 18:12:03 -0300 Subject: [PATCH 2/8] chore: fix lint issues ST1019 :wrench: --- common/gold/transaction/model/validations.go | 7 +++---- components/transaction/internal/ports/http/transaction.go | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/common/gold/transaction/model/validations.go b/common/gold/transaction/model/validations.go index 80404d6a..e0e29724 100644 --- a/common/gold/transaction/model/validations.go +++ b/common/gold/transaction/model/validations.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/LerianStudio/midaz/common" - "github.com/LerianStudio/midaz/common/constant" cn "github.com/LerianStudio/midaz/common/constant" a "github.com/LerianStudio/midaz/common/mgrpc/account" ) @@ -47,7 +46,7 @@ func ValidateFromToOperation(ft FromTo, validate Responses, acc *a.Account) (Amo balanceAfter := Balance{} if ft.IsFrom { - ba, err := OperateAmounts(validate.From[ft.Account], acc.Balance, constant.DEBIT) + ba, err := OperateAmounts(validate.From[ft.Account], acc.Balance, cn.DEBIT) if err != nil { return amount, balanceAfter, err } @@ -59,7 +58,7 @@ func ValidateFromToOperation(ft FromTo, validate Responses, acc *a.Account) (Amo balanceAfter = ba } else { - ba, err := OperateAmounts(validate.To[ft.Account], acc.Balance, constant.CREDIT) + ba, err := OperateAmounts(validate.To[ft.Account], acc.Balance, cn.CREDIT) if err != nil { return amount, balanceAfter, err } @@ -209,7 +208,7 @@ func OperateAmounts(amount Amount, balance *a.Balance, operation string) (Balanc var total float64 switch operation { - case constant.DEBIT: + case cn.DEBIT: if int(balance.Scale) < amount.Scale { v0 := Scale(int(balance.Available), int(balance.Scale), amount.Scale) total = v0 - float64(amount.Value) diff --git a/components/transaction/internal/ports/http/transaction.go b/components/transaction/internal/ports/http/transaction.go index 4d960c14..8b7d1d14 100644 --- a/components/transaction/internal/ports/http/transaction.go +++ b/components/transaction/internal/ports/http/transaction.go @@ -9,7 +9,6 @@ import ( "reflect" "github.com/LerianStudio/midaz/common" - "github.com/LerianStudio/midaz/common/constant" cn "github.com/LerianStudio/midaz/common/constant" "github.com/LerianStudio/midaz/common/gold/transaction" gold "github.com/LerianStudio/midaz/common/gold/transaction/model" @@ -392,7 +391,7 @@ func (handler *TransactionHandler) createTransaction(c *fiber.Ctx, logger mlog.L ctxUpdateTransactionStatus, spanUpdateTransactionStatus := tracer.Start(ctx, "handler.create_transaction.update_transaction_status") //TODO: use event driven and broken and parts - _, err = handler.Command.UpdateTransactionStatus(ctxUpdateTransactionStatus, organizationID, ledgerID, transactionID, constant.APPROVED) + _, err = handler.Command.UpdateTransactionStatus(ctxUpdateTransactionStatus, organizationID, ledgerID, transactionID, cn.APPROVED) if err != nil { mopentelemetry.HandleSpanError(&spanUpdateTransactionStatus, "Failed to update transaction status", err) @@ -480,7 +479,7 @@ func (handler *TransactionHandler) processAccounts(ctx context.Context, logger m var accountsToUpdate []*account.Account - go gold.UpdateAccounts(constant.DEBIT, validate.From, accounts, result, e) + go gold.UpdateAccounts(cn.DEBIT, validate.From, accounts, result, e) select { case r := <-result: accountsToUpdate = append(accountsToUpdate, r...) @@ -490,7 +489,7 @@ func (handler *TransactionHandler) processAccounts(ctx context.Context, logger m return err } - go gold.UpdateAccounts(constant.CREDIT, validate.To, accounts, result, e) + go gold.UpdateAccounts(cn.CREDIT, validate.To, accounts, result, e) select { case r := <-result: accountsToUpdate = append(accountsToUpdate, r...) From bb646b75161b1698adacc32164862d910fa5e987 Mon Sep 17 00:00:00 2001 From: Brecci Date: Thu, 14 Nov 2024 18:41:03 -0300 Subject: [PATCH 3/8] feat: add version endpoint to ledger and transaction services :sparkles: --- common/net/http/handler.go | 16 ++++++++-------- components/ledger/.env.example | 4 +--- components/ledger/internal/ports/http/routes.go | 3 +++ components/transaction/.env.example | 1 + .../transaction/internal/ports/http/routes.go | 4 +++- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/common/net/http/handler.go b/common/net/http/handler.go index 0cbdf874..0f964616 100644 --- a/common/net/http/handler.go +++ b/common/net/http/handler.go @@ -1,8 +1,8 @@ package http import ( + "github.com/LerianStudio/midaz/common" "log" - "os" "time" "github.com/gofiber/fiber/v2" @@ -18,14 +18,14 @@ func Ping(c *fiber.Ctx) error { } // Version returns HTTP Status 200 with given version. -func Version(version string) fiber.Handler { - return func(c *fiber.Ctx) error { - return c.JSON(fiber.Map{ - "version": version, - "buildNumber": os.Getenv("BUILD_NUMBER"), - "requestDate": time.Now().UTC(), - }) +func Version(c *fiber.Ctx) error { + message := fiber.Map{ + "version": common.GetenvOrDefault("VERSION", "0.0.0"), + "buildNumber": common.GetenvOrDefault("BUILD_NUMBER", "0"), + "requestDate": time.Now().UTC(), } + + return OK(c, message) } // Welcome returns HTTP Status 200 with service info. diff --git a/components/ledger/.env.example b/components/ledger/.env.example index 64fb5bb8..200fd799 100644 --- a/components/ledger/.env.example +++ b/components/ledger/.env.example @@ -1,10 +1,8 @@ # DEFAULT local # ENV_NAME=production -# APP VERSION -VERSION=1.21.0 - # APP +VERSION=1.21.0 SERVER_PORT=3000 SERVER_ADDRESS=:${SERVER_PORT} diff --git a/components/ledger/internal/ports/http/routes.go b/components/ledger/internal/ports/http/routes.go index 84bc353f..cb8261df 100644 --- a/components/ledger/internal/ports/http/routes.go +++ b/components/ledger/internal/ports/http/routes.go @@ -78,6 +78,9 @@ func NewRouter(lg mlog.Logger, tl *mopentelemetry.Telemetry, cc *mcasdoor.Casdoo // Health f.Get("/health", lib.Ping) + // Version + f.Get("/version", lib.Version) + // Doc lib.DocAPI("ledger", "Ledger API", f) diff --git a/components/transaction/.env.example b/components/transaction/.env.example index 4542e010..adeac118 100644 --- a/components/transaction/.env.example +++ b/components/transaction/.env.example @@ -2,6 +2,7 @@ # ENV_NAME=production # APP +VERSION=1.21.0 APP_CONTEXT=/transaction/v1 SERVER_PORT=3002 SERVER_ADDRESS=:${SERVER_PORT} diff --git a/components/transaction/internal/ports/http/routes.go b/components/transaction/internal/ports/http/routes.go index 51909582..b0369ce4 100644 --- a/components/transaction/internal/ports/http/routes.go +++ b/components/transaction/internal/ports/http/routes.go @@ -54,7 +54,9 @@ func NewRouter(lg mlog.Logger, tl *mopentelemetry.Telemetry, cc *mcasdoor.Casdoo // Health f.Get("/health", lib.Ping) - // Doc + // Version + f.Get("/version", lib.Version) + lib.DocAPI("transaction", "Transaction API", f) f.Use(tlMid.EndTracingSpans) From 798406f2ac00eb9e11fa8076c38906c0aa322f47 Mon Sep 17 00:00:00 2001 From: Brecci Date: Thu, 14 Nov 2024 18:43:33 -0300 Subject: [PATCH 4/8] fix: remove build number from version endpoint in ledger and transaction services :bug: --- common/net/http/handler.go | 7 ++----- .../ledger/internal/domain/portfolio/account/account.go | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/common/net/http/handler.go b/common/net/http/handler.go index 0f964616..8a9abe4c 100644 --- a/common/net/http/handler.go +++ b/common/net/http/handler.go @@ -19,13 +19,10 @@ func Ping(c *fiber.Ctx) error { // Version returns HTTP Status 200 with given version. func Version(c *fiber.Ctx) error { - message := fiber.Map{ + return OK(c, fiber.Map{ "version": common.GetenvOrDefault("VERSION", "0.0.0"), - "buildNumber": common.GetenvOrDefault("BUILD_NUMBER", "0"), "requestDate": time.Now().UTC(), - } - - return OK(c, message) + }) } // Welcome returns HTTP Status 200 with service info. diff --git a/components/ledger/internal/domain/portfolio/account/account.go b/components/ledger/internal/domain/portfolio/account/account.go index 7a976530..646cea12 100644 --- a/components/ledger/internal/domain/portfolio/account/account.go +++ b/components/ledger/internal/domain/portfolio/account/account.go @@ -114,4 +114,4 @@ func (t *AccountPostgreSQLModel) FromEntity(account *mmodel.Account) { deletedAtCopy := *account.DeletedAt t.DeletedAt = sql.NullTime{Time: deletedAtCopy, Valid: true} } -} \ No newline at end of file +} From c352dcc01fb8737e81e07206cac253f65b7d5805 Mon Sep 17 00:00:00 2001 From: lerian-studio Date: Mon, 18 Nov 2024 10:39:02 +0000 Subject: [PATCH 5/8] chore(release): 1.29.0-beta.2 ## [1.29.0-beta.2](https://github.com/LerianStudio/midaz/compare/v1.29.0-beta.1...v1.29.0-beta.2) (2024-11-18) ### Features * added command list from products ([fe7503e](https://github.com/LerianStudio/midaz/commit/fe7503ea6c4b971be4ffba55ed21035bfeb15710)) * create rest get product ([bf9a271](https://github.com/LerianStudio/midaz/commit/bf9a271ddd396e7800c2d69a1f3d87fc00916077)) --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e6787fc..eb877f97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## [1.29.0-beta.2](https://github.com/LerianStudio/midaz/compare/v1.29.0-beta.1...v1.29.0-beta.2) (2024-11-18) + + +### Features + +* added command list from products ([fe7503e](https://github.com/LerianStudio/midaz/commit/fe7503ea6c4b971be4ffba55ed21035bfeb15710)) +* create rest get product ([bf9a271](https://github.com/LerianStudio/midaz/commit/bf9a271ddd396e7800c2d69a1f3d87fc00916077)) + ## [1.29.0-beta.1](https://github.com/LerianStudio/midaz/compare/v1.28.0...v1.29.0-beta.1) (2024-11-14) From 21adf36ae0faf04dcd9783e63cf925a9eaf6a208 Mon Sep 17 00:00:00 2001 From: lerian-studio Date: Mon, 18 Nov 2024 10:47:31 +0000 Subject: [PATCH 6/8] chore(release): 1.29.0-beta.3 ## [1.29.0-beta.3](https://github.com/LerianStudio/midaz/compare/v1.29.0-beta.2...v1.29.0-beta.3) (2024-11-18) ### Bug Fixes * update transaction error messages to comply with gitbook :bug: ([36ae998](https://github.com/LerianStudio/midaz/commit/36ae9985b908784ea59669087e99cc56e9399f14)) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb877f97..0a84b40c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [1.29.0-beta.3](https://github.com/LerianStudio/midaz/compare/v1.29.0-beta.2...v1.29.0-beta.3) (2024-11-18) + + +### Bug Fixes + +* update transaction error messages to comply with gitbook :bug: ([36ae998](https://github.com/LerianStudio/midaz/commit/36ae9985b908784ea59669087e99cc56e9399f14)) + ## [1.29.0-beta.2](https://github.com/LerianStudio/midaz/compare/v1.29.0-beta.1...v1.29.0-beta.2) (2024-11-18) From 41f637d32c37f3e090321d21e46ab0fa180e5e73 Mon Sep 17 00:00:00 2001 From: Gabriel Brecci <34200450+qnen@users.noreply.github.com> Date: Mon, 18 Nov 2024 08:54:44 -0300 Subject: [PATCH 7/8] fix: add doc endpoint comment in transaction routes.go --- components/transaction/internal/ports/http/routes.go | 1 + 1 file changed, 1 insertion(+) diff --git a/components/transaction/internal/ports/http/routes.go b/components/transaction/internal/ports/http/routes.go index b0369ce4..a5b49069 100644 --- a/components/transaction/internal/ports/http/routes.go +++ b/components/transaction/internal/ports/http/routes.go @@ -57,6 +57,7 @@ func NewRouter(lg mlog.Logger, tl *mopentelemetry.Telemetry, cc *mcasdoor.Casdoo // Version f.Get("/version", lib.Version) + // Doc lib.DocAPI("transaction", "Transaction API", f) f.Use(tlMid.EndTracingSpans) From d11d9e561abe96b611ac392de702674065eb00e1 Mon Sep 17 00:00:00 2001 From: lerian-studio Date: Mon, 18 Nov 2024 12:17:49 +0000 Subject: [PATCH 8/8] chore(release): 1.29.0-beta.4 ## [1.29.0-beta.4](https://github.com/LerianStudio/midaz/compare/v1.29.0-beta.3...v1.29.0-beta.4) (2024-11-18) ### Features * add version endpoint to ledger and transaction services :sparkles: ([bb646b7](https://github.com/LerianStudio/midaz/commit/bb646b75161b1698adacc32164862d910fa5e987)) ### Bug Fixes * add doc endpoint comment in transaction routes.go ([41f637d](https://github.com/LerianStudio/midaz/commit/41f637d32c37f3e090321d21e46ab0fa180e5e73)) * remove build number from version endpoint in ledger and transaction services :bug: ([798406f](https://github.com/LerianStudio/midaz/commit/798406f2ac00eb9e11fa8076c38906c0aa322f47)) --- CHANGELOG.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a84b40c..7a07ec54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +## [1.29.0-beta.4](https://github.com/LerianStudio/midaz/compare/v1.29.0-beta.3...v1.29.0-beta.4) (2024-11-18) + + +### Features + +* add version endpoint to ledger and transaction services :sparkles: ([bb646b7](https://github.com/LerianStudio/midaz/commit/bb646b75161b1698adacc32164862d910fa5e987)) + + +### Bug Fixes + +* add doc endpoint comment in transaction routes.go ([41f637d](https://github.com/LerianStudio/midaz/commit/41f637d32c37f3e090321d21e46ab0fa180e5e73)) +* remove build number from version endpoint in ledger and transaction services :bug: ([798406f](https://github.com/LerianStudio/midaz/commit/798406f2ac00eb9e11fa8076c38906c0aa322f47)) + ## [1.29.0-beta.3](https://github.com/LerianStudio/midaz/compare/v1.29.0-beta.2...v1.29.0-beta.3) (2024-11-18)