Skip to content

Commit

Permalink
revert previous idea
Browse files Browse the repository at this point in the history
  • Loading branch information
pambecker committed Jan 17, 2025
1 parent e9a1e96 commit ec1649c
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ SELECT move_id, gbloc FROM (


DROP FUNCTION IF EXISTS get_address_gbloc;

CREATE OR REPLACE FUNCTION public.get_address_gbloc(
address_id UUID,
postal_code TEXT,
affiliation TEXT,
OUT gbloc TEXT
)
Expand All @@ -56,13 +56,8 @@ DECLARE
v_count INT;
v_bos_count INT;
v_dept_ind TEXT;
v_postal_code TEXT;
begin
IF address_id IS NOT NULL THEN
is_oconus := get_is_oconus(address_id);
ELSE
is_oconus := false;
END IF;
BEGIN
is_oconus := get_is_oconus(address_id);

IF affiliation in ('AIR_FORCE','SPACE_FORCE') THEN
v_dept_ind := 'AIR_AND_SPACE_FORCE';
Expand Down Expand Up @@ -146,28 +141,20 @@ begin

ELSE --is conus

IF postal_code IS NULL THEN

SELECT o.uspr_zip_id
INTO v_postal_code
FROM addresses a, v_locations o
WHERE a.us_post_region_cities_id = o.uprc_id
AND a.id = address_id;

ELSE
v_postal_code := postal_code;
END IF;

SELECT j.gbloc
INTO gbloc
FROM postal_code_to_gblocs j
WHERE j.postal_code = v_postal_code;
FROM addresses a,
v_locations o,
postal_code_to_gblocs j
WHERE a.us_post_region_cities_id = o.uprc_id
and o.uspr_zip_id = j.postal_code
and a.id = address_id;

END IF;

-- Raise an exception if no rate area is found
IF gbloc IS NULL THEN
RAISE EXCEPTION 'GBLOC not found for address ID % for affiliation % postal_code % ', address_id, affiiation, postal_code;
RAISE EXCEPTION 'GBLOC not found for address ID % for affiliation %', address_id, affiiation;
END IF;
END;
$$ LANGUAGE plpgsql;
46 changes: 33 additions & 13 deletions pkg/handlers/ghcapi/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,41 @@ func (h CreateOrderHandler) Handle(params orderop.CreateOrderParams) middleware.
return orderop.NewCreateOrderUnprocessableEntity(), err
}

destinationGBLOC, err := models.FetchAddressPostalCodeGbloc(appCtx.DB(), newDutyLocation.Address, newDutyLocation.Address.PostalCode, serviceMember)
if err != nil {
err = apperror.NewBadDataError("New duty location GBLOC cannot be verified")
appCtx.Logger().Error(err.Error())
return orderop.NewCreateOrderUnprocessableEntity(), err
var newDutyLocationGBLOC *string
if *newDutyLocation.Address.IsOconus {
newDutyLocationGBLOCOconus, err := models.FetchAddressGbloc(appCtx.DB(), newDutyLocation.Address, serviceMember)
if err != nil {
return nil, apperror.NewNotFoundError(newDutyLocation.ID, "while looking for Duty Location Oconus GBLOC")
}
newDutyLocationGBLOC = newDutyLocationGBLOCOconus
} else {
newDutyLocationGBLOCConus, err := models.FetchGBLOCForPostalCode(appCtx.DB(), newDutyLocation.Address.PostalCode)
if err != nil {
err = apperror.NewBadDataError("New duty location GBLOC cannot be verified")
appCtx.Logger().Error(err.Error())
return orderop.NewCreateOrderUnprocessableEntity(), err
}
newDutyLocationGBLOC = &newDutyLocationGBLOCConus.GBLOC
}

originDutyLocationGBLOC, err := models.FetchAddressPostalCodeGbloc(appCtx.DB(), originDutyLocation.Address, originDutyLocation.Address.PostalCode, serviceMember)
if err != nil {
switch err {
case sql.ErrNoRows:
return nil, apperror.NewNotFoundError(originDutyLocation.ID, "while looking for Duty Location FetchAddressPostalCodeGbloc")
default:
return nil, apperror.NewQueryError("FetchAddressPostalCodeGbloc", err, "")
var originDutyLocationGBLOC *string
if *originDutyLocation.Address.IsOconus {
originDutyLocationGBLOCOconus, err := models.FetchAddressGbloc(appCtx.DB(), originDutyLocation.Address, serviceMember)
if err != nil {
return nil, apperror.NewNotFoundError(originDutyLocation.ID, "while looking for Duty Location Oconus GBLOC")
}
originDutyLocationGBLOC = originDutyLocationGBLOCOconus
} else {
originDutyLocationGBLOCConus, err := models.FetchGBLOCForPostalCode(appCtx.DB(), originDutyLocation.Address.PostalCode)
if err != nil {
switch err {
case sql.ErrNoRows:
return nil, apperror.NewNotFoundError(originDutyLocation.ID, "while looking for Duty Location PostalCodeToGBLOC")
default:
return nil, apperror.NewQueryError("PostalCodeToGBLOC", err, "")
}
}
originDutyLocationGBLOC = &originDutyLocationGBLOCConus.GBLOC
}

grade := (internalmessages.OrderPayGrade)(*payload.Grade)
Expand Down Expand Up @@ -313,7 +333,7 @@ func (h CreateOrderHandler) Handle(params orderop.CreateOrderParams) middleware.
&entitlement,
originDutyLocationGBLOC,
packingAndShippingInstructions,
destinationGBLOC,
newDutyLocationGBLOC,
)
if err != nil || verrs.HasAny() {
return handlers.ResponseForVErrors(appCtx.Logger(), verrs, err), err
Expand Down
78 changes: 59 additions & 19 deletions pkg/handlers/internalapi/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,19 @@ func (h CreateOrdersHandler) Handle(params ordersop.CreateOrdersParams) middlewa
return handlers.ResponseForError(appCtx.Logger(), err), err
}

newDutyLocationGBLOC, err := models.FetchAddressPostalCodeGbloc(appCtx.DB(), newDutyLocation.Address, newDutyLocation.Address.PostalCode, serviceMember)
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
var newDutyLocationGBLOC *string
if *newDutyLocation.Address.IsOconus {
newDutyLocationGBLOCOconus, err := models.FetchAddressGbloc(appCtx.DB(), newDutyLocation.Address, serviceMember)
if err != nil {
return nil, apperror.NewNotFoundError(newDutyLocation.ID, "while looking for Duty Location Oconus GBLOC")
}
newDutyLocationGBLOC = newDutyLocationGBLOCOconus
} else {
newDutyLocationGBLOCConus, err := models.FetchGBLOCForPostalCode(appCtx.DB(), newDutyLocation.Address.PostalCode)
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
}
newDutyLocationGBLOC = &newDutyLocationGBLOCConus.GBLOC
}

var dependentsTwelveAndOver *int
Expand All @@ -183,14 +193,24 @@ func (h CreateOrdersHandler) Handle(params ordersop.CreateOrdersParams) middlewa
dependentsUnderTwelve = models.IntPointer(int(*payload.DependentsUnderTwelve))
}

originDutyLocationGBLOC, err := models.FetchAddressPostalCodeGbloc(appCtx.DB(), originDutyLocation.Address, originDutyLocation.Address.PostalCode, serviceMember)
if err != nil {
switch err {
case sql.ErrNoRows:
return nil, apperror.NewNotFoundError(originDutyLocation.ID, "while looking for Duty Location FetchAddressPostalCodeGbloc")
default:
return nil, apperror.NewQueryError("FetchAddressPostalCodeGbloc", err, "")
var originDutyLocationGBLOC *string
if *originDutyLocation.Address.IsOconus {
originDutyLocationGBLOCOconus, err := models.FetchAddressGbloc(appCtx.DB(), originDutyLocation.Address, serviceMember)
if err != nil {
return nil, apperror.NewNotFoundError(originDutyLocation.ID, "while looking for Duty Location Oconus GBLOC")
}
originDutyLocationGBLOC = originDutyLocationGBLOCOconus
} else {
originDutyLocationGBLOCConus, err := models.FetchGBLOCForPostalCode(appCtx.DB(), originDutyLocation.Address.PostalCode)
if err != nil {
switch err {
case sql.ErrNoRows:
return nil, apperror.NewNotFoundError(originDutyLocation.ID, "while looking for Duty Location PostalCodeToGBLOC")
default:
return nil, apperror.NewQueryError("PostalCodeToGBLOC", err, "")
}
}
originDutyLocationGBLOC = &originDutyLocationGBLOCConus.GBLOC
}

grade := payload.Grade
Expand Down Expand Up @@ -363,11 +383,21 @@ func (h UpdateOrdersHandler) Handle(params ordersop.UpdateOrdersParams) middlewa
return handlers.ResponseForError(appCtx.Logger(), err), err
}

newDutyLocationGBLOC, err := models.FetchAddressPostalCodeGbloc(appCtx.DB(), dutyLocation.Address, dutyLocation.Address.PostalCode, order.ServiceMember)
if err != nil {
err = apperror.NewBadDataError("New duty location GBLOC cannot be verified")
appCtx.Logger().Error(err.Error())
return handlers.ResponseForError(appCtx.Logger(), err), err
var newDutyLocationGBLOC *string
if *dutyLocation.Address.IsOconus {
newDutyLocationGBLOCOconus, err := models.FetchAddressGbloc(appCtx.DB(), dutyLocation.Address, order.ServiceMember)
if err != nil {
return nil, apperror.NewNotFoundError(dutyLocation.ID, "while looking for Duty Location Oconus GBLOC")
}
newDutyLocationGBLOC = newDutyLocationGBLOCOconus
} else {
newDutyLocationGBLOCConus, err := models.FetchGBLOCForPostalCode(appCtx.DB(), dutyLocation.Address.PostalCode)
if err != nil {
err = apperror.NewBadDataError("New duty location GBLOC cannot be verified")
appCtx.Logger().Error(err.Error())
return handlers.ResponseForError(appCtx.Logger(), err), err
}
newDutyLocationGBLOC = &newDutyLocationGBLOCConus.GBLOC
}

if payload.OriginDutyLocationID != "" {
Expand All @@ -382,11 +412,21 @@ func (h UpdateOrdersHandler) Handle(params ordersop.UpdateOrdersParams) middlewa
order.OriginDutyLocation = &originDutyLocation
order.OriginDutyLocationID = &originDutyLocationID

originGBLOC, originGBLOCerr := models.FetchAddressPostalCodeGbloc(appCtx.DB(), originDutyLocation.Address, originDutyLocation.Address.PostalCode, order.ServiceMember)
if originGBLOCerr != nil {
return handlers.ResponseForError(appCtx.Logger(), originGBLOCerr), originGBLOCerr
var originDutyLocationGBLOC *string
if *originDutyLocation.Address.IsOconus {
originDutyLocationGBLOCOconus, err := models.FetchAddressGbloc(appCtx.DB(), originDutyLocation.Address, order.ServiceMember)
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
}
originDutyLocationGBLOC = originDutyLocationGBLOCOconus
} else {
originDutyLocationGBLOCConus, err := models.FetchGBLOCForPostalCode(appCtx.DB(), originDutyLocation.Address.PostalCode)
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
}
originDutyLocationGBLOC = &originDutyLocationGBLOCConus.GBLOC
}
order.OriginDutyLocationGBLOC = originGBLOC
order.OriginDutyLocationGBLOC = originDutyLocationGBLOC

if payload.MoveID != "" {

Expand Down
7 changes: 3 additions & 4 deletions pkg/models/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,11 @@ func EvaluateIsOconus(address Address) bool {
}
}

// Fetches the GBLOC for a specific Address or Postal Code
// OCONUS will always use Address, while CONUS will use Postal Code
func FetchAddressPostalCodeGbloc(db *pop.Connection, address Address, postalCode string, serviceMember ServiceMember) (*string, error) {
// Fetches the GBLOC for a specific Address (for now this will be used for OCONUS)
func FetchAddressGbloc(db *pop.Connection, address Address, serviceMember ServiceMember) (*string, error) {
var gbloc *string

err := db.RawQuery("SELECT * FROM get_address_gbloc($1, $2, $3)", address.ID, postalCode, serviceMember.Affiliation.String()).
err := db.RawQuery("SELECT * FROM get_address_gbloc($1, $2)", address.ID, serviceMember.Affiliation.String()).
First(&gbloc)

if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions pkg/models/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ func (suite *ModelSuite) Test_FetchDutyLocationGblocForAK() {
}
suite.MustSave(&gblocAors)

gbloc, err := m.FetchAddressPostalCodeGbloc(suite.DB(), originDutyLocation.Address, originDutyLocation.Address.PostalCode, serviceMember)
gbloc, err := m.FetchAddressGbloc(suite.DB(), originDutyLocation.Address, serviceMember)
suite.NoError(err)
suite.NotNil(gbloc)
suite.Equal(gbloc, "MBFL")
suite.Equal(string(*gbloc), "MBFL")
})

suite.Run("fetches duty location GBLOC for AK address, Zone II Army", func() {
Expand Down Expand Up @@ -310,10 +310,10 @@ func (suite *ModelSuite) Test_FetchDutyLocationGblocForAK() {
}
suite.MustSave(&gblocAors)

gbloc, err := m.FetchAddressPostalCodeGbloc(suite.DB(), originDutyLocation.Address, originDutyLocation.Address.PostalCode, serviceMember)
gbloc, err := m.FetchAddressGbloc(suite.DB(), originDutyLocation.Address, serviceMember)
suite.NoError(err)
suite.NotNil(gbloc)
suite.Equal(gbloc, "JEAT")
suite.Equal(string(*gbloc), "JEAT")
})

suite.Run("fetches duty location GBLOC for AK Cordova address, Zone IV", func() {
Expand Down Expand Up @@ -342,10 +342,10 @@ func (suite *ModelSuite) Test_FetchDutyLocationGblocForAK() {
}
suite.MustSave(&gblocAors)

gbloc, err := m.FetchAddressPostalCodeGbloc(suite.DB(), originDutyLocation.Address, originDutyLocation.Address.PostalCode, serviceMember)
gbloc, err := m.FetchAddressGbloc(suite.DB(), originDutyLocation.Address, serviceMember)
suite.NoError(err)
suite.NotNil(gbloc)
suite.Equal(gbloc, "MAPS")
suite.Equal(string(*gbloc), "MAPS")
})

suite.Run("fetches duty location GBLOC for AK NOT Cordova address, Zone IV", func() {
Expand Down Expand Up @@ -374,9 +374,9 @@ func (suite *ModelSuite) Test_FetchDutyLocationGblocForAK() {
}
suite.MustSave(&gblocAors)

gbloc, err := m.FetchAddressPostalCodeGbloc(suite.DB(), originDutyLocation.Address, originDutyLocation.Address.PostalCode, serviceMember)
gbloc, err := m.FetchAddressGbloc(suite.DB(), originDutyLocation.Address, serviceMember)
suite.NoError(err)
suite.NotNil(gbloc)
suite.Equal(gbloc, "MAPK")
suite.Equal(string(*gbloc), "MAPK")
})
}
30 changes: 20 additions & 10 deletions pkg/models/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,29 @@ func (m Move) GetDestinationGBLOC(db *pop.Connection) (string, error) {
return "", err
}

err = db.Load(&m.Orders, "ServiceMember")
if err != nil {
if err.Error() == RecordNotFoundErrorString {
return "", errors.WithMessage(err, "No Service Member found in the DB associated with moveID "+m.ID.String())
var newGBLOC string
if *destinationAddress.IsOconus {
err := db.Load(&m.Orders, "ServiceMember")
if err != nil {
if err.Error() == RecordNotFoundErrorString {
return "", errors.WithMessage(err, "No Service Member found in the DB associated with moveID "+m.ID.String())
}
return "", err
}
return "", err
}
newGBLOC, err := FetchAddressPostalCodeGbloc(db, *destinationAddress, destinationAddress.PostalCode, m.Orders.ServiceMember)
if err != nil {
return "", err
newGBLOCOconus, err := FetchAddressGbloc(db, *destinationAddress, m.Orders.ServiceMember)
if err != nil {
return "", err
}
newGBLOC = *newGBLOCOconus
} else {
newGBLOCConus, err := FetchGBLOCForPostalCode(db, destinationAddress.PostalCode)
if err != nil {
return "", err
}
newGBLOC = newGBLOCConus.GBLOC
}

return *newGBLOC, err
return newGBLOC, err
}

// GetDestinationAddress returns the address for the move. This ensures that business logic is centralized.
Expand Down
Loading

0 comments on commit ec1649c

Please sign in to comment.