Skip to content

Commit

Permalink
Merge pull request #11 from LerianStudio/fix/MZ-436
Browse files Browse the repository at this point in the history
Fix/MZ-436
  • Loading branch information
MartinezAvellan authored May 21, 2024
2 parents 828da4a + 481e1fe commit 273ad06
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 25 deletions.
61 changes: 60 additions & 1 deletion common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package common
import (
"slices"
"strconv"

"github.com/jackc/pgx/v5/pgconn"
)

// Contains checks if an item is in a slice. This function uses type parameters to work with any slice type.
Expand Down Expand Up @@ -77,7 +79,7 @@ func ValidateCountryAddress(country string) error {
return nil
}

// ValidateType validate type values if c
// ValidateType validate type values of currencies
func ValidateType(t string) error {
types := []string{"crypto", "currency", "commodity", "others"}

Expand Down Expand Up @@ -122,3 +124,60 @@ func ValidateCurrency(code string) error {

return nil
}

// ValidatePGError validate pgError and return business error
func ValidatePGError(pgErr *pgconn.PgError, entityType string) error {
switch pgErr.ConstraintName {
case "account_parent_account_id_fkey":
return ValidationError{
EntityType: entityType,
Title: "Invalid Parent Account ID",
Code: "0029",
Message: "The specified parent account ID does not exist. Please verify the ID is correct and attempt your request again.",
}
case "account_instrument_code_fkey":
return ValidationError{
EntityType: entityType,
Title: "Instrument Code Not Found",
Code: "0034",
Message: "The provided instrument code does not exist in our records. Please verify the instrument code and try again.",
}
case "account_portfolio_id_fkey":
return ValidationError{
EntityType: entityType,
Title: "Portfolio ID Not Found",
Code: "0035",
Message: "The provided product ID does not exist in our records. Please verify the product ID and try again.",
}
case "account_product_id_fkey":
return ValidationError{
EntityType: entityType,
Title: "Product ID Not Found",
Code: "0036",
Message: "The provided product ID does not exist in our records. Please verify the product ID and try again.",
}
case "account_ledger_id_fkey":
return ValidationError{
EntityType: entityType,
Title: "Ledger ID Not Found",
Code: "0037",
Message: "The provided ledger ID does not exist in our records. Please verify the ledger ID and try again.",
}
case "account_organization_id_fkey":
return ValidationError{
EntityType: entityType,
Title: "Organization ID Not Found",
Code: "0038",
Message: "The provided organization ID does not exist in our records. Please verify the organization ID and try again.",
}
case "account_parent_organization_id_fkey":
return ValidationError{
EntityType: entityType,
Title: "Parent Organization ID Not Found",
Code: "0039",
Message: "The provided parent organization ID does not exist in our records. Please verify the parent organization ID and try again.",
}
default:
return pgErr
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/LerianStudio/midaz/common/mpostgres"
a "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/account"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
"github.com/lib/pq"
)

Expand Down Expand Up @@ -73,6 +75,11 @@ func (r *AccountPostgreSQLRepository) Create(ctx context.Context, account *a.Acc
record.DeletedAt,
)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(a.Account{}).Name())
}

return nil, err
}

Expand Down Expand Up @@ -196,6 +203,32 @@ func (r *AccountPostgreSQLRepository) Find(ctx context.Context, organizationID,
return account.ToEntity(), nil
}

// FindByAlias find account from the database using Organization and Ledger id and Alias.
func (r *AccountPostgreSQLRepository) FindByAlias(ctx context.Context, organizationID, ledgerID, portfolioID uuid.UUID, alias string) (bool, error) {
db, err := r.connection.GetDB(ctx)
if err != nil {
return false, err
}

rows, err := db.QueryContext(ctx, "SELECT * FROM account WHERE organization_id = $1 AND ledger_id = $2 AND portfolio_id = $3 AND alias LIKE $4 AND deleted_at IS NULL ORDER BY created_at DESC",
organizationID, ledgerID, portfolioID, alias)
if err != nil {
return false, err
}
defer rows.Close()

if rows.Next() {
return true, common.EntityConflictError{
EntityType: reflect.TypeOf(a.Account{}).Name(),
Title: "Alias has been taken",
Code: "0020",
Message: fmt.Sprintf("The alias %s has been taken already. Please, inform another one.", alias),
}
}

return false, nil
}

// ListByIDs retrieves Accounts entities from the database using the provided IDs.
func (r *AccountPostgreSQLRepository) ListByIDs(ctx context.Context, organizationID, ledgerID, portfolioID uuid.UUID, ids []uuid.UUID) ([]*a.Account, error) {
db, err := r.connection.GetDB(ctx)
Expand Down Expand Up @@ -277,7 +310,7 @@ func (r *AccountPostgreSQLRepository) Update(ctx context.Context, organizationID
args = append(args, record.StatusDescription)
}

if account.Alias != "" {
if !common.IsNilOrEmpty(account.Alias) {
updates = append(updates, "alias = $"+strconv.Itoa(len(args)+1))
args = append(args, record.Alias)
}
Expand Down Expand Up @@ -312,6 +345,11 @@ func (r *AccountPostgreSQLRepository) Update(ctx context.Context, organizationID

result, err := db.ExecContext(ctx, query, args...)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(a.Account{}).Name())
}

return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/LerianStudio/midaz/common/mpostgres"
i "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/instrument"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
"github.com/lib/pq"
)

Expand Down Expand Up @@ -51,6 +52,11 @@ func (r *InstrumentPostgreSQLRepository) Create(ctx context.Context, instrument
record.ID, record.Name, record.Type, record.Code, record.Status, record.StatusDescription,
record.LedgerID, record.OrganizationID, record.CreatedAt, record.UpdatedAt, record.DeletedAt)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(i.Instrument{}).Name())
}

return nil, err
}

Expand Down Expand Up @@ -232,6 +238,11 @@ func (r *InstrumentPostgreSQLRepository) Update(ctx context.Context, organizatio

result, err := db.ExecContext(ctx, query, args...)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(i.Instrument{}).Name())
}

return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/LerianStudio/midaz/common/mpostgres"
l "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/ledger"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
"github.com/lib/pq"
)

Expand Down Expand Up @@ -50,6 +51,11 @@ func (r *LedgerPostgreSQLRepository) Create(ctx context.Context, ledger *l.Ledge
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *`,
record.ID, record.Name, record.OrganizationID, record.Status, record.StatusDescription, record.CreatedAt, record.UpdatedAt, record.DeletedAt)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(l.Ledger{}).Name())
}

return nil, err
}

Expand Down Expand Up @@ -211,6 +217,11 @@ func (r *LedgerPostgreSQLRepository) Update(ctx context.Context, organizationID,

result, err := db.ExecContext(ctx, query, args...)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(l.Ledger{}).Name())
}

return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/LerianStudio/midaz/common/mpostgres"
o "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/organization"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
"github.com/lib/pq"
)

Expand Down Expand Up @@ -48,6 +49,11 @@ func (r *OrganizationPostgreSQLRepository) Create(ctx context.Context, organizat

address, err := json.Marshal(record.Address)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(o.Organization{}).Name())
}

return nil, err
}

Expand Down Expand Up @@ -138,6 +144,11 @@ func (r *OrganizationPostgreSQLRepository) Update(ctx context.Context, id uuid.U

result, err := db.ExecContext(ctx, query, args...)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(o.Organization{}).Name())
}

return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/LerianStudio/midaz/common/mpostgres"
p "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/portfolio"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
"github.com/lib/pq"
)

Expand Down Expand Up @@ -50,6 +51,11 @@ func (r *PortfolioPostgreSQLRepository) Create(ctx context.Context, portfolio *p
record.ID, record.Name, record.EntityID, record.LedgerID, record.OrganizationID,
record.Status, record.StatusDescription, record.CreatedAt, record.UpdatedAt, record.DeletedAt)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(p.Portfolio{}).Name())
}

return nil, err
}

Expand Down Expand Up @@ -238,6 +244,11 @@ func (r *PortfolioPostgreSQLRepository) Update(ctx context.Context, organization

result, err := db.ExecContext(ctx, query, args...)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(p.Portfolio{}).Name())
}

return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/LerianStudio/midaz/common/mpostgres"
r "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/product"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
"github.com/lib/pq"
)

Expand Down Expand Up @@ -57,6 +58,11 @@ func (p *ProductPostgreSQLRepository) Create(ctx context.Context, product *r.Pro
record.DeletedAt,
)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(r.Product{}).Name())
}

return nil, err
}

Expand Down Expand Up @@ -243,6 +249,11 @@ func (p *ProductPostgreSQLRepository) Update(ctx context.Context, organizationID

result, err := db.ExecContext(ctx, query, args...)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return nil, common.ValidatePGError(pgErr, reflect.TypeOf(r.Product{}).Name())
}

return nil, err
}

Expand Down
Loading

0 comments on commit 273ad06

Please sign in to comment.