Skip to content

Commit

Permalink
Merge pull request #22 from LerianStudio/fix/MZ-485
Browse files Browse the repository at this point in the history
Fix/mz 485
  • Loading branch information
MartinezAvellan authored Jun 4, 2024
2 parents d21a34b + 6b7599d commit 94e3eb4
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 64 deletions.
19 changes: 2 additions & 17 deletions common/stringUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,9 @@ func RemoveSpaces(word string) string {
}

// IsNilOrEmpty returns a boolean indicating if a *string is nil or empty.
// It's use TrimSpace so, a string " " and "" will be considered empty
// It's use TrimSpace so, a string " " and "" and "null" and "nil" will be considered empty
func IsNilOrEmpty(s *string) bool {
return s == nil || strings.TrimSpace(*s) == ""
}

// IsUpper check if string is lower
func IsUpper(s string) error {
for _, r := range s {
if unicode.IsLetter(r) && !unicode.IsUpper(r) {
return ValidationError{
Code: "0004",
Title: "Invalid Data provided.",
Message: "Invalid Data provided.",
}
}
}

return nil
return s == nil || strings.TrimSpace(*s) == "" || strings.TrimSpace(*s) == "null" || strings.TrimSpace(*s) == "nil"
}

// CamelToSnakeCase converts a given camelCase string to snake_case format.
Expand Down
17 changes: 14 additions & 3 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"slices"
"strconv"
"strings"
"unicode"

"go.mongodb.org/mongo-driver/bson"
)
Expand Down Expand Up @@ -109,11 +110,21 @@ func ValidateCurrency(code string) error {
"VED", "VEF", "VND", "VUV", "WST", "XAF", "XCD", "XDR", "XOF", "XPF", "XSU", "XUA", "YER", "ZAR", "ZMW", "ZWL",
}

for _, r := range code {
if unicode.IsLetter(r) && !unicode.IsUpper(r) {
return ValidationError{
Code: "0004",
Title: "Code Uppercase Requirement",
Message: "The code must be in uppercase. Please send the code in uppercase format.",
}
}
}

if !slices.Contains(currencies, code) {
return ValidationError{
Code: "0033",
Title: "Invalid Code Format",
Message: "The 'code' field must be alphanumeric, in upper case, and must contain at least one letter. Please provide a valid code.",
Code: "0005",
Title: "Currency Code Standard Compliance",
Message: "Currency-type instruments must adhere to the ISO-4217 standard. Please use a currency code that follows ISO-4217 guidelines.",
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (r *AccountPostgreSQLRepository) Update(ctx context.Context, organizationID
args = append(args, record.Alias)
}

if account.ProductID != "" {
if !common.IsNilOrEmpty(account.ProductID) {
updates = append(updates, "product_id = $"+strconv.Itoa(len(args)+1))
args = append(args, record.ProductID)
}
Expand Down
38 changes: 19 additions & 19 deletions components/ledger/internal/app/command/create-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,13 @@ func (uc *UseCase) CreateAccount(ctx context.Context, organizationID, ledgerID,
cai.EntityID = &portfolio.EntityID
}

account := &a.Account{
ID: uuid.New().String(),
InstrumentCode: cai.InstrumentCode,
Alias: cai.Alias,
Name: cai.Name,
Type: cai.Type,
ParentAccountID: cai.ParentAccountID,
ProductID: cai.ProductID,
OrganizationID: organizationID,
PortfolioID: portfolioID,
LedgerID: ledgerID,
EntityID: *cai.EntityID,
Balance: balance,
Status: status,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

if !common.IsNilOrEmpty(cai.ParentAccountID) {
acc, err := uc.AccountRepo.Find(ctx, uuid.MustParse(organizationID), uuid.MustParse(ledgerID), uuid.MustParse(portfolioID), uuid.MustParse(*cai.ParentAccountID))
if err != nil {
return nil, err
}

if acc.InstrumentCode != account.InstrumentCode {
if acc.InstrumentCode != cai.InstrumentCode {
return nil, common.ValidationError{
EntityType: reflect.TypeOf(a.Account{}).Name(),
Title: "Mismatched Instrument Code",
Expand All @@ -95,6 +77,24 @@ func (uc *UseCase) CreateAccount(ctx context.Context, organizationID, ledgerID,
}
}

account := &a.Account{
ID: uuid.New().String(),
InstrumentCode: cai.InstrumentCode,
Alias: cai.Alias,
Name: cai.Name,
Type: cai.Type,
ParentAccountID: cai.ParentAccountID,
ProductID: cai.ProductID,
OrganizationID: organizationID,
PortfolioID: portfolioID,
LedgerID: ledgerID,
EntityID: *cai.EntityID,
Balance: balance,
Status: status,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

port, err := uc.AccountRepo.Create(ctx, account)
if err != nil {
logger.Errorf("Error creating account: %v", err)
Expand Down
4 changes: 0 additions & 4 deletions components/ledger/internal/app/command/create-instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ func (uc *UseCase) CreateInstrument(ctx context.Context, organizationID, ledgerI
return nil, err
}

if err := common.IsUpper(cii.Code); err != nil {
return nil, err
}

if cii.Type == "currency" {
if err := common.ValidateCurrency(cii.Code); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion components/ledger/internal/domain/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// MetadataMongoDBModel represents the metadata into mongodb context
type MetadataMongoDBModel struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
ID primitive.ObjectID `bson:"_id"`
EntityID string `bson:"entity_id"`
EntityName string `bson:"entity_name"`
Data JSON `bson:"metadata"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type UpdateOrganizationInput struct {

// Organization is a struct designed to encapsulate response payload data.
type Organization struct {
ID string `json:"id,omitempty"`
ID string `json:"id"`
ParentOrganizationID *string `json:"parentOrganizationId"`
LegalName string `json:"legalName"`
DoingBusinessAs *string `json:"doingBusinessAs"`
Expand Down
10 changes: 5 additions & 5 deletions components/ledger/internal/domain/portfolio/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type AccountPostgreSQLModel struct {
OrganizationID string
LedgerID string
PortfolioID string
ProductID string
ProductID *string
AvailableBalance *float64
OnHoldBalance *float64
BalanceScale *float64
Expand All @@ -40,7 +40,7 @@ type CreateAccountInput struct {
Alias *string `json:"alias"`
Type string `json:"type"`
ParentAccountID *string `json:"parentAccountId"`
ProductID string `json:"productId"`
ProductID *string `json:"productId"`
EntityID *string `json:"entityId"`
Status Status `json:"status"`
Metadata map[string]any `json:"metadata"`
Expand All @@ -51,21 +51,21 @@ type UpdateAccountInput struct {
Name string `json:"name"`
Status Status `json:"status"`
Alias *string `json:"alias"`
ProductID string `json:"productId"`
ProductID *string `json:"productId"`
Metadata map[string]any `json:"metadata"`
}

// Account is a struct designed to encapsulate response payload data.
type Account struct {
ID string `json:"id"`
Name string `json:"name"`
ParentAccountID *string `json:"parentAccountId,omitempty"`
ParentAccountID *string `json:"parentAccountId"`
EntityID string `json:"entityId"`
InstrumentCode string `json:"instrumentCode"`
OrganizationID string `json:"organizationId"`
LedgerID string `json:"ledgerId"`
PortfolioID string `json:"portfolioId"`
ProductID string `json:"productId"`
ProductID *string `json:"productId"`
Balance Balance `json:"balance"`
Status Status `json:"status"`
Alias *string `json:"alias"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ CREATE TABLE IF NOT EXISTS account
id UUID PRIMARY KEY NOT NULL DEFAULT (uuid_generate_v4()),
name TEXT,
parent_account_id UUID,
entity_id UUID,
entity_id TEXT,
instrument_code TEXT NOT NULL,
organization_id UUID NOT NULL,
ledger_id UUID NOT NULL,
portfolio_id UUID NOT NULL,
product_id UUID NOT NULL,
product_id UUID,
available_balance NUMERIC NOT NULL,
on_hold_balance NUMERIC NOT NULL,
balance_scale NUMERIC NOT NULL,
Expand Down
Loading

0 comments on commit 94e3eb4

Please sign in to comment.