diff --git a/common/utils.go b/common/utils.go index fe185347..e3400174 100644 --- a/common/utils.go +++ b/common/utils.go @@ -1,6 +1,7 @@ package common import ( + "slices" "strconv" ) @@ -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 +} diff --git a/components/ledger/internal/app/command/create-organization.go b/components/ledger/internal/app/command/create-organization.go index 1c4cd532..ea09627a 100644 --- a/components/ledger/internal/app/command/create-organization.go +++ b/components/ledger/internal/app/command/create-organization.go @@ -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, diff --git a/components/ledger/internal/app/command/update-organization.go b/components/ledger/internal/app/command/update-organization.go index 6454b74f..c3ee46f0 100644 --- a/components/ledger/internal/app/command/update-organization.go +++ b/components/ledger/internal/app/command/update-organization.go @@ -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) diff --git a/components/ledger/internal/domain/onboarding/organization/organization.go b/components/ledger/internal/domain/onboarding/organization/organization.go index f2cab36d..07ab05ba 100644 --- a/components/ledger/internal/domain/onboarding/organization/organization.go +++ b/components/ledger/internal/domain/onboarding/organization/organization.go @@ -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. @@ -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