Skip to content

Commit

Permalink
address is_oconus implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TevinAdams committed Oct 16, 2024
1 parent 15a943a commit aaaf0ff
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
DO $$
BEGIN
UPDATE addresses
SET is_oconus = CASE
WHEN country IN ('US', 'United States') THEN false
ELSE true
END;
END $$;
-- Set temp timeout due to potentially large modification
-- Time is 5 minutes in milliseconds
SET statement_timeout = 300000;
SET lock_timeout = 300000;
SET idle_in_transaction_session_timeout = 300000;

-- Update is_oconus value on the addresses table based on the addresses country and the state
UPDATE addresses
SET is_oconus = CASE
1 change: 1 addition & 0 deletions pkg/factory/address_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func BuildMinimalAddress(db *pop.Connection, customs []Customization, traits []T
State: "GA",
PostalCode: "30813",
Country: models.StringPointer("US"),
IsOconus: models.BoolPointer(false),
}

// Overwrite values with those from customizations
Expand Down
4 changes: 4 additions & 0 deletions pkg/factory/address_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (suite *FactorySuite) TestBuildAddress() {
City: customCity,
State: customState,
PostalCode: customPostalCode,
IsOconus: models.BoolPointer(false),
},
},
}, nil)
Expand Down Expand Up @@ -96,6 +97,7 @@ func (suite *FactorySuite) TestBuildAddress() {
Model: models.Address{
StreetAddress1: customAddress1,
StreetAddress2: customAddress2,
IsOconus: models.BoolPointer(false),
},
},
}, []Trait{
Expand Down Expand Up @@ -125,6 +127,7 @@ func (suite *FactorySuite) TestBuildAddress() {
{
Model: models.Address{
StreetAddress1: customAddress1,
IsOconus: models.BoolPointer(false),
},
},
}, []Trait{
Expand Down Expand Up @@ -168,6 +171,7 @@ func (suite *FactorySuite) TestBuildAddress() {
PostalCode: customPostalCode,
Country: models.StringPointer("Canada"),
County: "County",
IsOconus: models.BoolPointer(false),
},
LinkOnly: true,
},
Expand Down
88 changes: 16 additions & 72 deletions pkg/handlers/authentication/devlocal.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,22 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
return nil, userType
}

address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
IsOconus: models.BoolPointer(false),
}

// Evaluate address and populate addresses isOconus value
isOconus, err := models.IsAddressOconus(appCtx.DB(), address)
if err != nil {
return nil, userType
}
address.IsOconus = &isOconus

switch userType {
case MilMoveUserType:
newServiceMember := models.ServiceMember{
Expand All @@ -501,14 +517,6 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
}
case TOOOfficeUserType:
// Now create the Truss JPPSO
address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
}

verrs, err := appCtx.DB().ValidateAndSave(&address)
if err != nil {
appCtx.Logger().Error("could not create address", zap.Error(err))
Expand Down Expand Up @@ -574,14 +582,6 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
}
case TIOOfficeUserType:
// Now create the Truss JPPSO
address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
}

verrs, err := appCtx.DB().ValidateAndSave(&address)
if err != nil {
appCtx.Logger().Error("could not create address", zap.Error(err))
Expand Down Expand Up @@ -646,14 +646,6 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
}
case ServicesCounselorOfficeUserType:
// Now create the Truss JPPSO
address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
}

verrs, err := appCtx.DB().ValidateAndSave(&address)
if err != nil {
appCtx.Logger().Error("could not create address", zap.Error(err))
Expand Down Expand Up @@ -718,14 +710,6 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
}
case PrimeSimulatorOfficeUserType:
// Now create the Truss JPPSO
address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
}

verrs, err := appCtx.DB().ValidateAndSave(&address)
if err != nil {
appCtx.Logger().Error("could not create address", zap.Error(err))
Expand Down Expand Up @@ -790,14 +774,6 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
}
case QaeOfficeUserType:
// Now create the Truss JPPSO
address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
}

verrs, err := appCtx.DB().ValidateAndSave(&address)
if err != nil {
appCtx.Logger().Error("could not create address", zap.Error(err))
Expand Down Expand Up @@ -862,14 +838,6 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
appCtx.Logger().Error("validation errors creating office user", zap.Stringer("errors", verrs))
}
case CustomerServiceRepresentativeOfficeUserType:
address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
}

verrs, err := appCtx.DB().ValidateAndSave(&address)
if err != nil {
appCtx.Logger().Error("could not create address", zap.Error(err))
Expand Down Expand Up @@ -934,14 +902,6 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
appCtx.Logger().Error("validation errors creating office user", zap.Stringer("errors", verrs))
}
case HQOfficeUserType:
address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
}

verrs, err := appCtx.DB().ValidateAndSave(&address)
if err != nil {
appCtx.Logger().Error("could not create address", zap.Error(err))
Expand Down Expand Up @@ -1006,14 +966,6 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
appCtx.Logger().Error("validation errors creating office user", zap.Stringer("errors", verrs))
}
case GSROfficeUserType:
address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
}

verrs, err := appCtx.DB().ValidateAndSave(&address)
if err != nil {
appCtx.Logger().Error("could not create address", zap.Error(err))
Expand Down Expand Up @@ -1079,14 +1031,6 @@ func createUser(h devlocalAuthHandler, w http.ResponseWriter, r *http.Request) (
}
case MultiRoleOfficeUserType:
// Now create the Truss JPPSO
address := models.Address{
StreetAddress1: "1333 Minna St",
City: "San Francisco",
State: "CA",
PostalCode: "94115",
County: "SAINT CLAIR",
}

verrs, err := appCtx.DB().ValidateAndSave(&address)
if err != nil {
appCtx.Logger().Error("could not create address", zap.Error(err))
Expand Down
1 change: 1 addition & 0 deletions pkg/handlers/ghcapi/internal/payloads/model_to_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ func Address(address *models.Address) *ghcmessages.Address {
Country: address.Country,
County: &address.County,
ETag: etag.GenerateEtag(address.UpdatedAt),
IsOconus: address.IsOconus,
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/handlers/internalapi/addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func fakeAddressPayload() *internalmessages.Address {
State: models.StringPointer("AL"),
PostalCode: models.StringPointer("40356"),
County: models.StringPointer("JESSAMINE"),
IsOconus: models.BoolPointer(false),
}
}

Expand All @@ -33,6 +34,7 @@ func (suite *HandlerSuite) TestShowAddressHandler() {
State: "state",
PostalCode: "12345",
County: "JESSAMINE",
IsOconus: models.BoolPointer(false),
}
suite.MustSave(&address)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func Address(address *models.Address) *internalmessages.Address {
PostalCode: &address.PostalCode,
Country: address.Country,
County: &address.County,
IsOconus: address.IsOconus,
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/migrate/fixtures/copyFromStdinKeywords.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
COPY public.addresses (id, street_address_1, street_address_2, city, state, postal_code, created_at, updated_at, street_address_3, country) FROM stdin;
82b2ee8a-ee1c-48ef-a066-14ba1129330d 123 Any St \N Ellsworth DO AFB SD 57706 2018-05-28 14:27:38.959754 2018-05-28 14:27:38.959755 \N United States
\.
COPY public.addresses (id, street_address_1, street_address_2, city, state, postal_code, created_at, updated_at, street_address_3, is_oconus, country_id) FROM stdin;
82b2ee8a-ee1c-48ef-a066-14ba1129330d 123 Any St \N Ellsworth DO AFB SD 57706 2018-05-28 14:27:38.959754 2018-05-28 14:27:38.959755 \N f 791899e6-cd77-46f2-981b-176ecb8d7098
\.
31 changes: 31 additions & 0 deletions pkg/models/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,34 @@ func (a *Address) Copy() *Address {
}
return nil
}

// Check if an address is CONUS or OCONUS
func IsAddressOconus(db *pop.Connection, address Address) (bool, error) {
// use the data we have first, if it's not nil
if address.Country != nil {
isOconus := EvaluateIsOconus(address)
return isOconus, nil
} else if address.CountryId != nil {
country, err := FetchCountryByID(db, *address.CountryId)
if err != nil {
return false, err
}
address.Country = &country
isOconus := EvaluateIsOconus(address)
return isOconus, nil
} else {
if address.State == "HI" || address.State == "AK" {
return true, nil
}
return false, nil
}
}

// Conditional logic for a CONUS and OCONUS address
func EvaluateIsOconus(address Address) bool {
if address.Country.Country != "US" || address.Country.Country == "US" && address.State == "AK" || address.Country.Country == "US" && address.State == "HI" {
return true
} else {
return false
}
}
35 changes: 35 additions & 0 deletions pkg/models/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,38 @@ func (suite *ModelSuite) TestAddressCountryCode() {
suite.Equal("NotImplementedCountryCode: Country 'Ireland'", err.Error())

}

func (suite *ModelSuite) TestIsAddressOconusNoCountry() {
address := m.Address{
StreetAddress1: "street 1",
StreetAddress2: m.StringPointer("street 2"),
StreetAddress3: m.StringPointer("street 3"),
City: "city",
State: "SC",
PostalCode: "29229",
County: "county",
}

result, err := m.IsAddressOconus(suite.DB(), address)
suite.NoError(err)

suite.Equal(false, result)
}

// Test IsOconus logic for an address with no country and a state of AK
func (suite *ModelSuite) TestIsAddressOconusForAKState() {
address := m.Address{
StreetAddress1: "street 1",
StreetAddress2: m.StringPointer("street 2"),
StreetAddress3: m.StringPointer("street 3"),
City: "Anchorage",
State: "AK",
PostalCode: "99502",
County: "county",
}

result, err := m.IsAddressOconus(suite.DB(), address)
suite.NoError(err)

suite.Equal(true, result)
}
17 changes: 17 additions & 0 deletions pkg/models/service_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ func SaveServiceMember(appCtx appcontext.AppContext, serviceMember *ServiceMembe
responseError = err
return err
}

// Evaluate address and populate addresses isOconus value
isOconus, err := IsAddressOconus(appCtx.DB(), *serviceMember.ResidentialAddress)
if err != nil {
responseError = err
return err
}
serviceMember.ResidentialAddress.IsOconus = &isOconus

serviceMember.ResidentialAddress.County = county
if verrs, err := txnAppCtx.DB().ValidateAndSave(serviceMember.ResidentialAddress); verrs.HasAny() || err != nil {
responseVErrors.Append(verrs)
Expand All @@ -236,6 +245,14 @@ func SaveServiceMember(appCtx appcontext.AppContext, serviceMember *ServiceMembe
serviceMember.BackupMailingAddressID = &serviceMember.BackupMailingAddress.ID
}

// Evaluate address and populate addresses isOconus value
isOconus, err := IsAddressOconus(appCtx.DB(), *serviceMember.BackupMailingAddress)
if err != nil {
responseError = err
return err
}
serviceMember.BackupMailingAddress.IsOconus = &isOconus

if verrs, err := txnAppCtx.DB().ValidateAndSave(serviceMember); verrs.HasAny() || err != nil {
responseVErrors.Append(verrs)
responseError = err
Expand Down
7 changes: 7 additions & 0 deletions pkg/services/address/address_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func (f *addressCreator) CreateAddress(appCtx appcontext.AppContext, address *mo
transformedAddress.County = county
}

// Evaluate address and populate addresses isOconus value
isOconus, err := models.IsAddressOconus(appCtx.DB(), transformedAddress)
if err != nil {
return nil, err
}
transformedAddress.IsOconus = &isOconus

txnErr := appCtx.NewTransaction(func(txnCtx appcontext.AppContext) error {
verrs, err := txnCtx.DB().Eager().ValidateAndCreate(&transformedAddress)
if verrs != nil && verrs.HasAny() {
Expand Down
18 changes: 18 additions & 0 deletions pkg/services/address/address_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,22 @@ func (suite *AddressSuite) TestAddressCreator() {
suite.NotNil(err)
suite.Equal("No county found for provided zip code 11111", err.Error())
})

suite.Run("Successfully creates a CONUS address", func() {
country := &models.Country{}
country.Country = "US"
addressCreator := NewAddressCreator()
address, err := addressCreator.CreateAddress(suite.AppContextForTest(), &models.Address{
StreetAddress1: "7645 Ballinshire N",
City: "Indianapolis",
State: "IN",
PostalCode: "46254",
Country: country,
})

suite.False(*address.IsOconus)
suite.NotNil(address.ID)
suite.Nil(err)
suite.NotNil(address.Country)
})
}
Loading

0 comments on commit aaaf0ff

Please sign in to comment.