From d0038fa6780c6f4b117b1e45f1bf0558a8543315 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 15 Apr 2024 18:13:58 +0000 Subject: [PATCH] adding changes to the go backend fo qualify excess risk --- .../order/excess_weight_risk_manager.go | 70 +++++++++++++++++-- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/pkg/services/order/excess_weight_risk_manager.go b/pkg/services/order/excess_weight_risk_manager.go index d3313c7437c..177073d7375 100644 --- a/pkg/services/order/excess_weight_risk_manager.go +++ b/pkg/services/order/excess_weight_risk_manager.go @@ -2,6 +2,7 @@ package order import ( "database/sql" + "errors" "time" "github.com/gobuffalo/validate/v3" @@ -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 {