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-19178 Yellow flag on MTO page persists even after review #12427

Closed
Changes from all commits
Commits
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
70 changes: 64 additions & 6 deletions pkg/services/order/excess_weight_risk_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package order

import (
"database/sql"
"errors"
"time"

"github.com/gobuffalo/validate/v3"
Expand Down Expand Up @@ -199,18 +200,75 @@ func (f *excessWeightRiskManager) updateAuthorizedWeight(appCtx appcontext.AppCo
}

func (f *excessWeightRiskManager) acknowledgeExcessWeight(appCtx appcontext.AppContext, move models.Move) (*models.Move, error) {
if !excessWeightRiskShouldBeAcknowledged(move) {
return &move, nil
db := appCtx.DB()
var theMove models.Move
err := db.EagerPreload("MTOShipments", "MTOShipments.PPMShipment", "Orders", "Orders.Grade", "Orders.Entitlement.DependentsAuthorized").Find(&theMove, move.ID)

if err != nil {
switch err {
case sql.ErrNoRows:
return nil, apperror.NewNotFoundError(move.ID, "looking for Move")
default:
return nil, apperror.NewQueryError("Move", err, "")
}
}

if theMove.Orders.Grade == nil {
return nil, errors.New("could not determine excess weight entitlement without grade")
}

if theMove.Orders.Entitlement.DependentsAuthorized == nil {
return nil, errors.New("could not determine excess weight entitlement without dependents authorization value")
}

totalWeightAllowance := models.GetWeightAllotment(*theMove.Orders.Grade)

weight := totalWeightAllowance.TotalWeightSelf
if *theMove.Orders.Entitlement.DependentsAuthorized {
weight = totalWeightAllowance.TotalWeightSelfPlusDependents
}

estimatedWeightTotal := 0

for _, shipment := range theMove.MTOShipments {

// turn this into a function later...
// use the appropriate riskOfExcess threshold modifier when it comes to NTSR shipments once that info is released.

// We should avoid counting shipments that haven't been approved yet and will need to account for diversions
// and cancellations factoring into the estimated weight total.
if shipment.Status == models.MTOShipmentStatusApproved {
if shipment.PrimeEstimatedWeight != nil {
estimatedWeightTotal += shipment.PrimeEstimatedWeight.Int()
}
if shipment.PPMShipment != nil {
estimatedWeightTotal += shipment.PPMShipment.EstimatedWeight.Int()
}
}
}

const RiskOfExcessThreshold = .9
// may need to take into account floating point precision here but should be dealing with whole numbers
if int(float32(weight)*RiskOfExcessThreshold) <= estimatedWeightTotal {
excessWeightQualifiedAt := time.Now()
theMove.ExcessWeightQualifiedAt = &excessWeightQualifiedAt
} else if theMove.ExcessWeightQualifiedAt != nil {
// the move had previously qualified for excess weight but does not any longer so reset the value
theMove.ExcessWeightQualifiedAt = nil
}

if !excessWeightRiskShouldBeAcknowledged(theMove) {
return &theMove, nil
}

now := time.Now()
move.ExcessWeightAcknowledgedAt = &now
verrs, err := appCtx.DB().ValidateAndUpdate(&move)
theMove.ExcessWeightAcknowledgedAt = &now
verrs, err := appCtx.DB().ValidateAndUpdate(&theMove)
if e := f.handleError(move.ID, verrs, err); e != nil {
return &move, e
return &theMove, e
}

return &move, nil
return &theMove, nil
}

func (f *excessWeightRiskManager) handleError(modelID uuid.UUID, verrs *validate.Errors, err error) error {
Expand Down