Skip to content

Commit

Permalink
Merge branch 'develop' into feature/MIDAZ-251
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwelbm authored Nov 18, 2024
2 parents 80d3a62 + a1b8aac commit 9926161
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 162 deletions.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
## [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)


### 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)


### 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)


Expand Down
2 changes: 2 additions & 0 deletions common/constant/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
56 changes: 23 additions & 33 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "" {
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 9 additions & 29 deletions common/gold/transaction/model/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,32 @@ 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"
)

// 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")
}
}
}
Expand All @@ -62,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
}
Expand All @@ -74,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
}
Expand Down Expand Up @@ -224,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)
Expand Down Expand Up @@ -351,11 +335,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
Expand Down
15 changes: 6 additions & 9 deletions common/net/http/handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package http

import (
"github.com/LerianStudio/midaz/common"
"log"
"os"
"time"

"github.com/gofiber/fiber/v2"
Expand All @@ -18,14 +18,11 @@ 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 {
return OK(c, fiber.Map{
"version": common.GetenvOrDefault("VERSION", "0.0.0"),
"requestDate": time.Now().UTC(),
})
}

// Welcome returns HTTP Status 200 with service info.
Expand Down
4 changes: 1 addition & 3 deletions components/ledger/.env.example
Original file line number Diff line number Diff line change
@@ -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}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ func (t *AccountPostgreSQLModel) FromEntity(account *mmodel.Account) {
deletedAtCopy := *account.DeletedAt
t.DeletedAt = sql.NullTime{Time: deletedAtCopy, Valid: true}
}
}
}
3 changes: 3 additions & 0 deletions components/ledger/internal/ports/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions components/transaction/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# ENV_NAME=production

# APP
VERSION=1.21.0
APP_CONTEXT=/transaction/v1
SERVER_PORT=3002
SERVER_ADDRESS=:${SERVER_PORT}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mongodb
import (
"context"
"errors"
cn "github.com/LerianStudio/midaz/common/constant"
"github.com/LerianStudio/midaz/common/mopentelemetry"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 9926161

Please sign in to comment.