Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B-21094: Automatic Reweigh Based on Estimated Weight #14364

Merged
merged 47 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e9d0465
re-rolled in all changes and started with a fresh integrationTesting …
WeatherfordAaron Dec 10, 2024
62febea
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Dec 10, 2024
f04de38
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Dec 12, 2024
31655a8
change requested to move the bool to services
WeatherfordAaron Dec 12, 2024
ce54b7a
Revert "change requested to move the bool to services"
WeatherfordAaron Dec 13, 2024
2b7eefa
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Dec 13, 2024
1d1a84c
moved all logic that interacts with the db to the services folder
WeatherfordAaron Dec 17, 2024
bf57220
moved all logic that interacts with the db to the services folder
WeatherfordAaron Dec 17, 2024
b303e13
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Dec 17, 2024
ddf3df1
Merge branch 'INT-B-21094' of https://github.com/transcom/mymove into…
WeatherfordAaron Dec 17, 2024
c1e704e
since only 1 change in this file is a whitespace change now, undoing it
WeatherfordAaron Dec 17, 2024
2d0d953
since test coverage change, need to undo these additional tests
WeatherfordAaron Dec 18, 2024
f437dd3
corrected pointers so that the playwright tests work
WeatherfordAaron Dec 18, 2024
8ddd9eb
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Dec 18, 2024
34bdfb3
removed extraneous DB calls
WeatherfordAaron Dec 18, 2024
c031b13
removed extraneous DB calls
WeatherfordAaron Dec 18, 2024
3d2c131
removed extraneous DB calls
WeatherfordAaron Dec 18, 2024
a435233
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Dec 19, 2024
2a993f3
added nil check
WeatherfordAaron Dec 19, 2024
f712475
expanded on nil check for DBAuthorizedWeight and added a test
WeatherfordAaron Dec 19, 2024
ca0bbc0
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Dec 19, 2024
8e9244b
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Dec 19, 2024
dbaf906
put the IF statement outside of the else
WeatherfordAaron Dec 19, 2024
ddf5b77
corrected where the reweigh check is occuring
WeatherfordAaron Dec 19, 2024
b3f7a10
Revert "corrected where the reweigh check is occuring"
WeatherfordAaron Dec 19, 2024
d4c8ed2
fixed issue where nil ref was found
WeatherfordAaron Dec 26, 2024
cc39642
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Dec 26, 2024
8745a44
corrected faulty sql as well as put the estimated and actuel weight c…
WeatherfordAaron Dec 26, 2024
c7954ac
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Jan 6, 2025
ac7bb89
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Jan 10, 2025
7d31903
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Jan 10, 2025
6fc336f
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Jan 15, 2025
8ff1fe3
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Jan 28, 2025
92340f7
fixed test
WeatherfordAaron Jan 28, 2025
07b3eda
moved check for availableToPrimeAt
WeatherfordAaron Jan 28, 2025
beb93c3
Merge branch 'integrationTesting' into INT-B-21094
WeatherfordAaron Jan 28, 2025
f65f928
fixed some nil checks
WeatherfordAaron Jan 28, 2025
da9f7e3
fixed some nil checks
WeatherfordAaron Jan 28, 2025
880b5f9
fixed tests
WeatherfordAaron Jan 29, 2025
c88085a
fixed tests
WeatherfordAaron Jan 29, 2025
b885a16
fixed tests
WeatherfordAaron Jan 29, 2025
e81e826
fixed tests
WeatherfordAaron Jan 29, 2025
5201b10
fixed tests
WeatherfordAaron Jan 29, 2025
171ef45
changed to using the weight allotment in stead of authorized weight
WeatherfordAaron Feb 3, 2025
8d739e4
Added back in nil checks and added tests.
brooklyn-welsh Feb 6, 2025
1e9ee0f
Merge branch 'integrationTesting' into INT-B-21094
brooklyn-welsh Feb 6, 2025
3196b19
Merge branch 'integrationTesting' into INT-B-21094
taeJungCaci Feb 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pkg/models/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,6 @@ func GetTotalNetWeightForMove(m Move) unit.Pound {
}
}
return totalNetWeight

}

// gets total weight from all ppm and hhg shipments within a move
Expand Down
117 changes: 117 additions & 0 deletions pkg/services/mocks/WeightAllotmentFetcher.go

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

89 changes: 89 additions & 0 deletions pkg/services/mocks/WeightRestrictor.go

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

122 changes: 77 additions & 45 deletions pkg/services/move/move_weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package move
import (
"database/sql"
"errors"
"math"
"time"

"github.com/gobuffalo/validate/v3"
Expand Down Expand Up @@ -62,7 +63,7 @@ func shipmentHasReweighWeight(shipment models.MTOShipment) bool {
}

// return the lower weight of a shipment's actual weight and the reweighed weight
func lowerShipmentWeight(shipment models.MTOShipment) int {
func lowerShipmentActualWeight(shipment models.MTOShipment) int {
actualWeight := 0
if shipment.PrimeActualWeight != nil {
actualWeight = shipment.PrimeActualWeight.Int()
Expand All @@ -78,6 +79,23 @@ func lowerShipmentWeight(shipment models.MTOShipment) int {
return actualWeight
}

// return the lower weight of a shipment's estimated weight and the reweighed weight
func lowerShipmentEstimatedWeight(shipment models.MTOShipment) int {
estimatedWeight := 0
if shipment.PrimeEstimatedWeight != nil {
estimatedWeight = shipment.PrimeEstimatedWeight.Int()
}

if shipmentHasReweighWeight(shipment) {
reweighWeight := shipment.Reweigh.Weight.Int()
if reweighWeight < estimatedWeight {
return reweighWeight
}
}

return estimatedWeight
}

func (w moveWeights) CheckExcessWeight(appCtx appcontext.AppContext, moveID uuid.UUID, updatedShipment models.MTOShipment) (*models.Move, *validate.Errors, error) {
db := appCtx.DB()
var move models.Move
Expand Down Expand Up @@ -244,17 +262,60 @@ func calculateSumOfWeights(move models.Move, updatedShipment *models.MTOShipment
return sumOfWeights
}

// GetAutoReweighShipments returns all shipments that need to be reweighed
func GetAutoReweighShipments(move *models.Move, updatedShipment *models.MTOShipment) (models.MTOShipments, error) {
results := models.MTOShipments{}
weightLimit := 0

if move.Orders.Entitlement.DBAuthorizedWeight != nil {
weightLimit = *move.Orders.Entitlement.DBAuthorizedWeight
danieljordan-caci marked this conversation as resolved.
Show resolved Hide resolved
} else {
return nil, errors.New("No Authorized Weight could be found when checking for auto-reweigh on " + move.ID.String())
}

totalActualWeight := 0
totalEstimatedWeight := 0
for i := range move.MTOShipments {
if move.MTOShipments[i].ShipmentType != models.MTOShipmentTypePPM &&
availableShipmentStatus(move.MTOShipments[i].Status) &&
move.MTOShipments[i].DeletedAt == nil &&
updatedShipment.ID != move.MTOShipments[i].ID {
if move.MTOShipments[i].PrimeActualWeight != nil {
totalActualWeight += lowerShipmentActualWeight(move.MTOShipments[i])
}
if move.MTOShipments[i].PrimeEstimatedWeight != nil {
totalEstimatedWeight += lowerShipmentEstimatedWeight(move.MTOShipments[i])
}
results = append(results, move.MTOShipments[i])
} else if move.MTOShipments[i].ID == updatedShipment.ID {
if updatedShipment.PrimeActualWeight != nil {
totalActualWeight += lowerShipmentActualWeight(*updatedShipment)
}
if updatedShipment.PrimeEstimatedWeight != nil {
totalEstimatedWeight += lowerShipmentEstimatedWeight(*updatedShipment)
}
results = append(results, *updatedShipment)
}
}

// Check actual weight first
if int(totalActualWeight) >= int(math.Round(float64(weightLimit)*0.9)) {
return results, nil
}

// Check estimated weight second
if int(totalEstimatedWeight) >= int(math.Round(float64(weightLimit)*0.9)) {
return results, nil
}

return models.MTOShipments{}, nil
}

func (w moveWeights) CheckAutoReweigh(appCtx appcontext.AppContext, moveID uuid.UUID, updatedShipment *models.MTOShipment) (models.MTOShipments, error) {
db := appCtx.DB()
var move models.Move
err := db.Eager("MTOShipments", "MTOShipments.Reweigh", "Orders.Entitlement").Find(&move, moveID)
err := appCtx.DB().Eager("MTOShipments", "Orders", "Orders.Entitlement", "MTOShipments.ShipmentType", "MTOShipments.Status", "MTOShipments.DeletedAt", "MTOShipments.PrimeActualWeight", "MTOShipments.PrimeEstimatedWeight").Find(&move, moveID)
if err != nil {
switch err {
case sql.ErrNoRows:
return nil, apperror.NewNotFoundError(moveID, "looking for Move")
default:
return nil, apperror.NewQueryError("Move", err, "")
}
return nil, err
cameroncaci marked this conversation as resolved.
Show resolved Hide resolved
}

if move.Orders.Grade == nil {
Expand All @@ -265,48 +326,19 @@ func (w moveWeights) CheckAutoReweigh(appCtx appcontext.AppContext, moveID uuid.
return nil, errors.New("could not determine excess weight entitlement without dependents authorization value")
}

totalWeightAllowance, err := w.WeightAllotmentFetcher.GetWeightAllotment(appCtx, string(*move.Orders.Grade), move.Orders.OrdersType)
autoReweighShipments, err := GetAutoReweighShipments(&move, updatedShipment)
if err != nil {
return nil, err
}

overallWeightAllowance := totalWeightAllowance.TotalWeightSelf
if *move.Orders.Entitlement.DependentsAuthorized {
overallWeightAllowance = totalWeightAllowance.TotalWeightSelfPlusDependents
}

moveWeightTotal := 0
for _, shipment := range move.MTOShipments {
// We should avoid counting shipments that haven't been approved yet and will need to account for diversions
// and cancellations factoring into the weight total.
if availableShipmentStatus(shipment.Status) {
if shipment.ID != updatedShipment.ID {
moveWeightTotal += lowerShipmentWeight(shipment)
} else {
// the shipment being updated might have a reweigh that wasn't loaded
updatedShipment.Reweigh = shipment.Reweigh
moveWeightTotal += lowerShipmentWeight(*updatedShipment)
if len(autoReweighShipments) > 0 {
for _, shipment := range autoReweighShipments {
reweigh, err := w.ReweighRequestor.RequestShipmentReweigh(appCtx, shipment.ID, models.ReweighRequesterSystem)
if err != nil {
return nil, err
}
}
}

autoReweighShipments := models.MTOShipments{}
// may need to take into account floating point precision here but should be dealing with whole numbers
if int(float32(overallWeightAllowance)*AutoReweighRequestThreshold) <= moveWeightTotal {
for _, shipment := range move.MTOShipments {
// We should avoid counting shipments that haven't been approved yet and will need to account for diversions
// and cancellations factoring into the weight total.
if availableShipmentStatus(shipment.Status) && (shipment.Reweigh == nil || shipment.Reweigh.ID == uuid.Nil) {
reweigh, err := w.ReweighRequestor.RequestShipmentReweigh(appCtx, shipment.ID, models.ReweighRequesterSystem)
if err != nil {
return nil, err
}
autoReweighShipments = append(autoReweighShipments, shipment)
// this may not be necessary depending on how the shipment is being updated/refetched elsewhere
if shipment.ID == updatedShipment.ID {
updatedShipment.Reweigh = reweigh
}
}
shipment.Reweigh = reweigh
}
}

Expand Down
Loading