Skip to content

Commit

Permalink
Merge pull request #8 from LerianStudio/fix/MZ-431
Browse files Browse the repository at this point in the history
Fix/MZ-431
  • Loading branch information
MartinezAvellan authored May 20, 2024
2 parents e77eebb + be8637e commit e82ac3d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
31 changes: 31 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"slices"
"strconv"
)

Expand Down Expand Up @@ -45,3 +46,33 @@ func CheckMetadataKeyAndValueLength(limit int, metadata map[string]any) error {

return nil
}

// ValidateCountryAddress validate if country in object address contains in countries list using ISO 3166-1 alpha-2
func ValidateCountryAddress(country string) error {
countries := []string{
"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ",
"BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW",
"BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX",
"CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM",
"FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU",
"GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE",
"JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK",
"LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP",
"MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP",
"NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA",
"RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO",
"SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR",
"TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS",
"YE", "YT", "ZA", "ZM", "ZW",
}

if !slices.Contains(countries, country) {
return ValidationError{
Code: "0032",
Title: "Invalid Country Code",
Message: "The provided country code in the 'address.country' field does not conform to the ISO-3166 alpha-2 standard. Please provide a valid alpha-2 country code.",
}
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ func (uc *UseCase) CreateOrganization(ctx context.Context, coi *o.CreateOrganiza
status = coi.Status
}

if coi.ParentOrganizationID != nil && len(*coi.ParentOrganizationID) == 0 {
if common.IsNilOrEmpty(coi.ParentOrganizationID) {
coi.ParentOrganizationID = nil
}

if err := common.ValidateCountryAddress(coi.Address.Country); err != nil {
return nil, err
}

organization := &o.Organization{
ParentOrganizationID: coi.ParentOrganizationID,
LegalName: coi.LegalName,
Expand Down
17 changes: 13 additions & 4 deletions components/ledger/internal/app/command/update-organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ func (uc *UseCase) UpdateOrganizationByID(ctx context.Context, id string, uoi *o
}
}

if common.IsNilOrEmpty(uoi.ParentOrganizationID) {
uoi.ParentOrganizationID = nil
}

if err := common.ValidateCountryAddress(uoi.Address.Country); err != nil {
return nil, err
}

organization := &o.Organization{
LegalName: uoi.LegalName,
DoingBusinessAs: uoi.DoingBusinessAs,
Address: uoi.Address,
Status: uoi.Status,
ParentOrganizationID: uoi.ParentOrganizationID,
LegalName: uoi.LegalName,
DoingBusinessAs: uoi.DoingBusinessAs,
Address: uoi.Address,
Status: uoi.Status,
}

organizationUpdated, err := uc.OrganizationRepo.Update(ctx, uuid.MustParse(id), organization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ type CreateOrganizationInput struct {

// UpdateOrganizationInput is a struct design to encapsulate request update payload data.
type UpdateOrganizationInput struct {
LegalName string `json:"legalName"`
DoingBusinessAs *string `json:"doingBusinessAs,omitempty"`
Address Address `json:"address"`
Status Status `json:"status"`
Metadata map[string]any `json:"metadata"`
LegalName string `json:"legalName"`
ParentOrganizationID *string `json:"parentOrganizationId"`
DoingBusinessAs *string `json:"doingBusinessAs,omitempty"`
Address Address `json:"address"`
Status Status `json:"status"`
Metadata map[string]any `json:"metadata"`
}

// Organization is a struct designed to encapsulate response payload data.
Expand Down Expand Up @@ -71,19 +72,17 @@ func (s Status) IsEmpty() bool {

// Address structure for marshaling/unmarshalling JSON.
type Address struct {
Line1 string `json:"line1"`
Line2 *string `json:"line2,omitempty"`
Neighborhood string `json:"neighborhood"`
ZipCode string `json:"zipCode"`
City string `json:"city"`
State string `json:"state"`
Country string `json:"country"` // According to ISO 3166-1 alpha-2
Line1 string `json:"line1"`
Line2 *string `json:"line2,omitempty"`
ZipCode string `json:"zipCode"`
City string `json:"city"`
State string `json:"state"`
Country string `json:"country"` // According to ISO 3166-1 alpha-2
}

// IsEmpty method that set empty or nil in fields
func (a Address) IsEmpty() bool {
return a.Line1 == "" && a.Line2 == nil && a.Neighborhood == "" &&
a.ZipCode == "" && a.City == "" && a.State == "" && a.Country == ""
return a.Line1 == "" && a.Line2 == nil && a.ZipCode == "" && a.City == "" && a.State == "" && a.Country == ""
}

// ToEntity converts an OrganizationPostgreSQLModel to entity.Organization
Expand Down

0 comments on commit e82ac3d

Please sign in to comment.