Skip to content

Commit

Permalink
Added back in nil checks and added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
brooklyn-welsh committed Feb 6, 2025
1 parent 171ef45 commit 8d739e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 16 additions & 1 deletion pkg/services/move/move_weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ func calculateSumOfWeights(move models.Move, updatedShipment *models.MTOShipment

// GetAutoReweighShipments returns all shipments that need to be reweighed
func (w moveWeights) GetAutoReweighShipments(appCtx appcontext.AppContext, move *models.Move, updatedShipment *models.MTOShipment) (models.MTOShipments, error) {
if move == nil {
return nil, apperror.NewBadDataError("received a nil move, a move must be supplied for checking reweighs")
}
if updatedShipment == nil {
return nil, apperror.NewBadDataError("received a nil MTO shipment, an MTO shipment must be supplied for checking reweighs")
}
results := models.MTOShipments{}

totalWeightAllowance, err := w.WeightAllotmentFetcher.GetWeightAllotment(appCtx, string(*move.Orders.Grade), move.Orders.OrdersType)
Expand Down Expand Up @@ -312,10 +318,19 @@ func (w moveWeights) GetAutoReweighShipments(appCtx appcontext.AppContext, move
}

func (w moveWeights) CheckAutoReweigh(appCtx appcontext.AppContext, moveID uuid.UUID, updatedShipment *models.MTOShipment) (models.MTOShipments, error) {
if updatedShipment == nil {
return nil, apperror.NewBadDataError("received a nil MTO shipment, an MTO shipment must be supplied for checking reweighs")
}

var move models.Move
err := appCtx.DB().Eager("MTOShipments", "Orders", "Orders.Entitlement", "MTOShipments.ShipmentType", "MTOShipments.Status", "MTOShipments.DeletedAt", "MTOShipments.PrimeActualWeight", "MTOShipments.PrimeEstimatedWeight").Find(&move, moveID)
if err != nil {
return nil, err
switch err {
case sql.ErrNoRows:
return nil, apperror.NewNotFoundError(moveID, "looking for Move")
default:
return nil, apperror.NewQueryError("Move", err, "")
}
}

if move.Orders.Grade == nil {
Expand Down
15 changes: 11 additions & 4 deletions pkg/services/move/move_weights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/gofrs/uuid"

"github.com/transcom/mymove/pkg/apperror"
"github.com/transcom/mymove/pkg/auth"
"github.com/transcom/mymove/pkg/factory"
"github.com/transcom/mymove/pkg/models"
Expand Down Expand Up @@ -845,14 +846,20 @@ func (suite *MoveServiceSuite) TestAutoReweigh() {
suite.EqualError(err, "could not determine excess weight entitlement without dependents authorization value")
})

suite.Run("returns error if DBAuthorizedWeight returns nil when checking for auto-reweigh", func() {
suite.Run("returns error if can't find move when checking for auto-reweigh", func() {
randomID, err := uuid.NewV4()
suite.NoError(err)
_, err = moveWeights.CheckAutoReweigh(suite.AppContextForTest(), randomID, &models.MTOShipment{})
suite.EqualError(err, apperror.NewNotFoundError(randomID, "looking for Move").Error())
})

suite.Run("returns error if shipment returns nil when checking for auto-reweigh", func() {
approvedMove := factory.BuildAvailableToPrimeMove(suite.DB(), nil, nil)
approvedMove.Orders.Entitlement.DBAuthorizedWeight = nil

err := suite.DB().Save(approvedMove.Orders.Entitlement)
suite.NoError(err)

_, err = moveWeights.CheckAutoReweigh(suite.AppContextForTest(), approvedMove.ID, &models.MTOShipment{})
suite.EqualError(err, "No Authorized Weight could be found when checking for auto-reweigh on "+approvedMove.ID.String())
_, err = moveWeights.CheckAutoReweigh(suite.AppContextForTest(), approvedMove.ID, nil)
suite.EqualError(err, apperror.NewBadDataError("received a nil MTO shipment, an MTO shipment must be supplied for checking reweighs").Error())
})
}

0 comments on commit 8d739e4

Please sign in to comment.