Skip to content

Commit

Permalink
Merge pull request #14534 from transcom/INT-B-22169
Browse files Browse the repository at this point in the history
INT B-22169
  • Loading branch information
danieljordan-caci authored Jan 13, 2025
2 parents bd28e7f + ac981ee commit 1777a3b
Show file tree
Hide file tree
Showing 28 changed files with 661 additions and 62 deletions.
1 change: 1 addition & 0 deletions migrations/app/migrations_manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1070,3 +1070,4 @@
20241230190647_add_missing_AK_zips_to_zip3_distances.up.sql
20250103130619_revert_data_change_for_gbloc_for_ak.up.sql
20250103180420_update_pricing_proc_to_use_local_price_variable.up.sql
20250110153428_add_shipment_address_updates_to_move_history.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- adding shipment_address_updates table to move history so we can track the activity
SELECT add_audit_history_table(
target_table := 'shipment_address_updates',
audit_rows := BOOLEAN 't',
audit_query_text := BOOLEAN 't',
ignored_cols := ARRAY[
'created_at'
]
);
24 changes: 24 additions & 0 deletions pkg/assets/sql_scripts/move_history_fetcher.sql
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,25 @@ WITH move AS (
JOIN gsr_appeals ON gsr_appeals.id = audit_history.object_id
WHERE audit_history.table_name = 'gsr_appeals'
),
shipment_address_updates AS (
SELECT shipment_address_updates.*,
jsonb_agg(jsonb_build_object(
'status', shipment_address_updates.status
)
)::TEXT AS context
FROM shipment_address_updates
JOIN move_shipments ON shipment_address_updates.shipment_id = move_shipments.id
GROUP BY shipment_address_updates.id
),
shipment_address_updates_logs as (
SELECT audit_history.*,
shipment_address_updates.context AS context,
NULL AS context_id
FROM
audit_history
JOIN shipment_address_updates ON shipment_address_updates.id = audit_history.object_id
WHERE audit_history.table_name = 'shipment_address_updates'
),
combined_logs AS (
SELECT
*
Expand Down Expand Up @@ -736,6 +755,11 @@ WITH move AS (
*
FROM
gsr_appeals_logs
UNION
SELECT
*
FROM
shipment_address_updates_logs


)
Expand Down
6 changes: 4 additions & 2 deletions pkg/gen/ghcapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions pkg/models/re_contract.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package models

import (
"database/sql"
"fmt"
"time"

"github.com/gobuffalo/pop/v6"
"github.com/gobuffalo/validate/v3"
"github.com/gobuffalo/validate/v3/validators"
"github.com/gofrs/uuid"

"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
)

// ReContract represents a contract with pricing information
Expand All @@ -32,3 +37,30 @@ func (r *ReContract) Validate(_ *pop.Connection) (*validate.Errors, error) {
&validators.StringIsPresent{Field: r.Name, Name: "Name"},
), nil
}

func FetchContractForMove(appCtx appcontext.AppContext, moveID uuid.UUID) (ReContract, error) {
var move Move
err := appCtx.DB().Find(&move, moveID)
if err != nil {
if err == sql.ErrNoRows {
return ReContract{}, apperror.NewNotFoundError(moveID, "looking for Move")
}
return ReContract{}, err
}

if move.AvailableToPrimeAt == nil {
return ReContract{}, apperror.NewConflictError(moveID, "unable to pick contract because move is not available to prime")
}

var contractYear ReContractYear
err = appCtx.DB().EagerPreload("Contract").Where("? between start_date and end_date", move.AvailableToPrimeAt).
First(&contractYear)
if err != nil {
if err == sql.ErrNoRows {
return ReContract{}, apperror.NewNotFoundError(uuid.Nil, fmt.Sprintf("no contract year found for %s", move.AvailableToPrimeAt.String()))
}
return ReContract{}, err
}

return contractYear.Contract, nil
}
31 changes: 31 additions & 0 deletions pkg/models/re_contract_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package models_test

import (
"time"

"github.com/transcom/mymove/pkg/factory"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/testdatagen"
)

func (suite *ModelSuite) TestReContractValidations() {
Expand All @@ -23,3 +27,30 @@ func (suite *ModelSuite) TestReContractValidations() {
suite.verifyValidationErrors(&emptyReContract, expErrors)
})
}

func (suite *ModelSuite) TestFetchContractForMove() {
suite.Run("finds valid contract", func() {
reContract := testdatagen.FetchOrMakeReContract(suite.DB(), testdatagen.Assertions{})
testdatagen.FetchOrMakeReContractYear(suite.DB(), testdatagen.Assertions{
ReContractYear: models.ReContractYear{
Contract: reContract,
ContractID: reContract.ID,
StartDate: time.Now(),
EndDate: time.Now().Add(time.Hour * 12),
Escalation: 1.0,
EscalationCompounded: 1.0,
},
})
move := factory.BuildAvailableToPrimeMove(suite.DB(), nil, nil)
contract, err := models.FetchContractForMove(suite.AppContextForTest(), move.ID)
suite.NoError(err)
suite.Equal(contract.ID, reContract.ID)
})

suite.Run("returns error if no contract found", func() {
move := factory.BuildAvailableToPrimeMove(suite.DB(), nil, nil)
contract, err := models.FetchContractForMove(suite.AppContextForTest(), move.ID)
suite.Error(err)
suite.Equal(contract, models.ReContract{})
})
}
14 changes: 14 additions & 0 deletions pkg/models/re_oconus_rate_areas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"time"

"github.com/gobuffalo/pop/v6"
"github.com/gofrs/uuid"
)

Expand All @@ -19,3 +20,16 @@ type OconusRateArea struct {
func (o OconusRateArea) TableName() string {
return "re_oconus_rate_areas"
}

func FetchOconusRateArea(db *pop.Connection, zip string) (*OconusRateArea, error) {
var reOconusRateArea OconusRateArea
err := db.Q().
InnerJoin("re_rate_areas ra", "re_oconus_rate_areas.rate_area_id = ra.id").
InnerJoin("us_post_region_cities upc", "upc.id = re_oconus_rate_areas.us_post_region_cities_id").
Where("upc.uspr_zip_id = ?", zip).
First(&reOconusRateArea)
if err != nil {
return nil, err
}
return &reOconusRateArea, nil
}
18 changes: 16 additions & 2 deletions pkg/models/re_rate_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func FetchReRateAreaItem(tx *pop.Connection, contractID uuid.UUID, code string)
}

// a db stored proc that takes in an address id & a service code to get the rate area id for an address
func FetchRateAreaID(db *pop.Connection, addressID uuid.UUID, serviceID uuid.UUID, contractID uuid.UUID) (uuid.UUID, error) {
if addressID != uuid.Nil && serviceID != uuid.Nil && contractID != uuid.Nil {
func FetchRateAreaID(db *pop.Connection, addressID uuid.UUID, serviceID *uuid.UUID, contractID uuid.UUID) (uuid.UUID, error) {
if addressID != uuid.Nil && contractID != uuid.Nil {
var rateAreaID uuid.UUID
err := db.RawQuery("SELECT get_rate_area_id($1, $2, $3)", addressID, serviceID, contractID).First(&rateAreaID)
if err != nil {
Expand All @@ -67,3 +67,17 @@ func FetchRateAreaID(db *pop.Connection, addressID uuid.UUID, serviceID uuid.UUI
}
return uuid.Nil, fmt.Errorf("error fetching rate area ID - required parameters not provided")
}

func FetchConusRateAreaByPostalCode(db *pop.Connection, zip string, contractID uuid.UUID) (*ReRateArea, error) {
var reRateArea ReRateArea
postalCode := zip[0:3]
err := db.Q().
InnerJoin("re_zip3s rz", "rz.rate_area_id = re_rate_areas.id").
Where("zip3 = ?", postalCode).
Where("re_rate_areas.contract_id = ?", contractID).
First(&reRateArea)
if err != nil {
return nil, err
}
return &reRateArea, nil
}
6 changes: 2 additions & 4 deletions pkg/models/re_rate_area_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ func (suite *ModelSuite) TestFetchRateAreaID() {
service := factory.FetchReServiceByCode(suite.DB(), models.ReServiceCodeIHPK)
contract := testdatagen.FetchOrMakeReContract(suite.DB(), testdatagen.Assertions{})
address := factory.BuildAddress(suite.DB(), nil, nil)
rateAreaId, err := models.FetchRateAreaID(suite.DB(), address.ID, service.ID, contract.ID)
rateAreaId, err := models.FetchRateAreaID(suite.DB(), address.ID, &service.ID, contract.ID)
suite.NotNil(rateAreaId)
suite.NoError(err)
})

suite.Run("fail - receive error when not all values are provided", func() {
var nilUuid uuid.UUID
contract := testdatagen.FetchOrMakeReContract(suite.DB(), testdatagen.Assertions{})
address := factory.BuildAddress(suite.DB(), nil, nil)
rateAreaId, err := models.FetchRateAreaID(suite.DB(), address.ID, nilUuid, contract.ID)
rateAreaId, err := models.FetchRateAreaID(suite.DB(), address.ID, nil, uuid.Nil)
suite.Equal(uuid.Nil, rateAreaId)
suite.Error(err)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (p PerUnitCentsLookup) lookup(appCtx appcontext.AppContext, s *ServiceItemP
switch p.ServiceItem.ReService.Code {
case models.ReServiceCodeIHPK:
// IHPK: Need rate area ID for the pickup address
rateAreaID, err := models.FetchRateAreaID(appCtx.DB(), *p.MTOShipment.PickupAddressID, serviceID, contractID)
rateAreaID, err := models.FetchRateAreaID(appCtx.DB(), *p.MTOShipment.PickupAddressID, &serviceID, contractID)
if err != nil {
return "", fmt.Errorf("error fetching rate area id for shipment ID: %s and service ID %s: %s", p.MTOShipment.ID, serviceID, err)
}
Expand All @@ -43,7 +43,7 @@ func (p PerUnitCentsLookup) lookup(appCtx appcontext.AppContext, s *ServiceItemP

case models.ReServiceCodeIHUPK:
// IHUPK: Need rate area ID for the destination address
rateAreaID, err := models.FetchRateAreaID(appCtx.DB(), *p.MTOShipment.PickupAddressID, serviceID, contractID)
rateAreaID, err := models.FetchRateAreaID(appCtx.DB(), *p.MTOShipment.PickupAddressID, &serviceID, contractID)
if err != nil {
return "", fmt.Errorf("error fetching rate area id for shipment ID: %s and service ID %s: %s", p.MTOShipment.ID, serviceID, err)
}
Expand All @@ -62,11 +62,11 @@ func (p PerUnitCentsLookup) lookup(appCtx appcontext.AppContext, s *ServiceItemP

case models.ReServiceCodeISLH:
// ISLH: Need rate area IDs for origin and destination
originRateAreaID, err := models.FetchRateAreaID(appCtx.DB(), *p.MTOShipment.PickupAddressID, serviceID, contractID)
originRateAreaID, err := models.FetchRateAreaID(appCtx.DB(), *p.MTOShipment.PickupAddressID, &serviceID, contractID)
if err != nil {
return "", fmt.Errorf("error fetching rate area id for origin address for shipment ID: %s and service ID %s: %s", p.MTOShipment.ID, serviceID, err)
}
destRateAreaID, err := models.FetchRateAreaID(appCtx.DB(), *p.MTOShipment.DestinationAddressID, serviceID, contractID)
destRateAreaID, err := models.FetchRateAreaID(appCtx.DB(), *p.MTOShipment.DestinationAddressID, &serviceID, contractID)
if err != nil {
return "", fmt.Errorf("error fetching rate area id for destination address for shipment ID: %s and service ID %s: %s", p.MTOShipment.ID, serviceID, err)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/services/move_history/move_history_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,10 @@ func (suite *MoveHistoryServiceSuite) TestMoveHistoryFetcherFunctionality() {
auditHistoryContains := func(auditHistories models.AuditHistories, keyword string) func() (success bool) {
return func() (success bool) {
for _, record := range auditHistories {
if strings.Contains(*record.ChangedData, keyword) {
return true
if record.ChangedData != nil {
if strings.Contains(*record.ChangedData, keyword) {
return true
}
}
}
return false
Expand Down
Loading

0 comments on commit 1777a3b

Please sign in to comment.