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-20229 refactor fuel pricer #12974

Merged
merged 20 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions migrations/app/migrations_manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -951,5 +951,6 @@
20240531153321_update_tio_role_name.up.sql
20240531154303_add_more_submitted_columns_to_ppm_document_tables.up.sql
20240603040207_add_submitted_cols_to_moving_expenses.up.sql
20240604203456_add_effective_expiraton_date_ghc_fuel.up.sql
20240603152949_update_too_role_name.up.sql
20240606195706_adding_uncapped_request_total.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER Table ghc_diesel_fuel_prices
ADD column IF NOT EXISTS effective_date date,
ADD column IF NOT EXISTS end_date date;
danieljordan-caci marked this conversation as resolved.
Show resolved Hide resolved

-- update current records with effective date and end date
-- business rule is that the diesel fuel prices are posted on Mondays and are effective Tuesday and end the following Monday
update ghc_diesel_fuel_prices set effective_date = publication_date + interval '1' day;
update ghc_diesel_fuel_prices set end_date = effective_date + interval '6' day;
2 changes: 2 additions & 0 deletions pkg/handlers/primeapi/payment_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@ func (suite *HandlerSuite) setupDomesticLinehaulData() (models.Move, models.MTOS
ghcDieselFuelPrice := models.GHCDieselFuelPrice{
PublicationDate: publicationDate,
FuelPriceInMillicents: unit.Millicents(277600),
EffectiveDate: publicationDate.AddDate(0, 0, 1),
EndDate: publicationDate.AddDate(0, 0, 7),
}
suite.MustSave(&ghcDieselFuelPrice)

Expand Down
2 changes: 2 additions & 0 deletions pkg/models/ghc_diesel_fuel_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type GHCDieselFuelPrice struct {
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
FuelPriceInMillicents unit.Millicents `json:"fuel_price_in_millicents" db:"fuel_price_in_millicents"`
PublicationDate time.Time `json:"publication_date" db:"publication_date"`
EffectiveDate time.Time `json:"effective_date" db:"effective_date"`
EndDate time.Time `json:"end_date" db:"end_date"`
}

// TableName overrides the table name used by Pop.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ func (r EIAFuelPriceLookup) lookup(appCtx appcontext.AppContext, _ *ServiceItemP
return "", fmt.Errorf("could not find actual pickup date for MTOShipment [%s]", r.MTOShipment.ID)
}

// Find the GHCDieselFuelPrice object with the closest prior PublicationDate to the ActualPickupDate of the MTOShipment in question
// Find the GHCDieselFuelPrice object effective before the shipment's ActualPickupDate and ends after the ActualPickupDate
var ghcDieselFuelPrice models.GHCDieselFuelPrice
err := db.Where("publication_date <= ?", actualPickupDate).Order("publication_date DESC").Last(&ghcDieselFuelPrice)
err := db.Where("? BETWEEN effective_date and end_date", actualPickupDate).Order("publication_date DESC").First(&ghcDieselFuelPrice) //only want the first published price per week
if err != nil {
switch err {
case sql.ErrNoRows:
return "", apperror.NewNotFoundError(uuid.Nil, "Looking for GHCDieselFuelPrice")
// If no published price is found, look for the first published price after the actual pickup date
err = db.Where("publication_date <= ?", actualPickupDate).Order("publication_date DESC").Last(&ghcDieselFuelPrice)
if err != nil {
switch err {
case sql.ErrNoRows:
return "", apperror.NewNotFoundError(uuid.Nil, "Looking for GHCDieselFuelPrice")
default:
return "", apperror.NewQueryError("GHCDieselFuelPrice", err, "")
}
}
default:
return "", apperror.NewQueryError("GHCDieselFuelPrice", err, "")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (suite *ServiceParamValueLookupsSuite) TestEIAFuelPriceLookup() {

firstGHCDieselFuelPrice.PublicationDate = time.Date(2020, time.July, 06, 0, 0, 0, 0, time.UTC)
firstGHCDieselFuelPrice.FuelPriceInMillicents = unit.Millicents(243699)
firstGHCDieselFuelPrice.EffectiveDate = firstGHCDieselFuelPrice.PublicationDate.AddDate(0, 0, 1)
firstGHCDieselFuelPrice.EndDate = firstGHCDieselFuelPrice.PublicationDate.AddDate(0, 0, 7)

var existingFuelPrice1 models.GHCDieselFuelPrice
err := suite.DB().Where("ghc_diesel_fuel_prices.publication_date = ?", firstGHCDieselFuelPrice.PublicationDate).First(&existingFuelPrice1)
Expand All @@ -39,6 +41,8 @@ func (suite *ServiceParamValueLookupsSuite) TestEIAFuelPriceLookup() {

secondGHCDieselFuelPrice.PublicationDate = time.Date(2020, time.July, 13, 0, 0, 0, 0, time.UTC)
secondGHCDieselFuelPrice.FuelPriceInMillicents = unit.Millicents(243799)
secondGHCDieselFuelPrice.EffectiveDate = secondGHCDieselFuelPrice.PublicationDate.AddDate(0, 0, 1)
secondGHCDieselFuelPrice.EndDate = secondGHCDieselFuelPrice.PublicationDate.AddDate(0, 0, 7)

var existingFuelPrice2 models.GHCDieselFuelPrice
err = suite.DB().Where("ghc_diesel_fuel_prices.publication_date = ?", secondGHCDieselFuelPrice.PublicationDate).First(&existingFuelPrice2)
Expand All @@ -50,6 +54,9 @@ func (suite *ServiceParamValueLookupsSuite) TestEIAFuelPriceLookup() {

thirdGHCDieselFuelPrice.PublicationDate = time.Date(2020, time.July, 20, 0, 0, 0, 0, time.UTC)
thirdGHCDieselFuelPrice.FuelPriceInMillicents = unit.Millicents(243299)
thirdGHCDieselFuelPrice.EffectiveDate = thirdGHCDieselFuelPrice.PublicationDate.AddDate(0, 0, 1)
thirdGHCDieselFuelPrice.EndDate = thirdGHCDieselFuelPrice.PublicationDate.AddDate(0, 0, 7)

var existingFuelPrice3 models.GHCDieselFuelPrice
err = suite.DB().Where("ghc_diesel_fuel_prices.publication_date = ?", thirdGHCDieselFuelPrice.PublicationDate).First(&existingFuelPrice3)
if err == nil {
Expand Down Expand Up @@ -168,3 +175,85 @@ func (suite *ServiceParamValueLookupsSuite) TestEIAFuelPriceLookup() {
suite.Equal("Not found looking for pickup address", err.Error())
})
}
func (suite *ServiceParamValueLookupsSuite) TestEIAFuelPriceLookupWithInvalidActualPickupDate() {
key := models.ServiceItemParamNameEIAFuelPrice
var mtoServiceItem models.MTOServiceItem
var paymentRequest models.PaymentRequest

setupTestData := func() {
testdatagen.MakeReContractYear(suite.DB(), testdatagen.Assertions{
ReContractYear: models.ReContractYear{
StartDate: time.Now().Add(-24 * time.Hour),
EndDate: time.Now().Add(24 * time.Hour),
},
})
mtoServiceItem = factory.BuildMTOServiceItem(suite.DB(), []factory.Customization{
{
Model: models.MTOShipment{
ActualPickupDate: nil,
},
},
}, []factory.Trait{
factory.GetTraitAvailableToPrimeMove,
})

paymentRequest = factory.BuildPaymentRequest(suite.DB(), []factory.Customization{
{
Model: mtoServiceItem.MoveTaskOrder,
LinkOnly: true,
},
}, nil)
}

suite.Run("lookup GHC diesel fuel price with nil actual pickup date", func() {
setupTestData()

paramLookup, err := ServiceParamLookupInitialize(suite.AppContextForTest(), suite.planner, mtoServiceItem, paymentRequest.ID, paymentRequest.MoveTaskOrderID, nil)
suite.FatalNoError(err)
_, err = paramLookup.ServiceParamValue(suite.AppContextForTest(), key)
suite.Error(err)
suite.Contains(err.Error(), "EIAFuelPriceLookup with error Not found Looking for GHCDieselFuelPrice")
})
}

func (suite *ServiceParamValueLookupsSuite) TestEIAFuelPriceLookupWithNoGHCDieselFuelPriceData() {
key := models.ServiceItemParamNameEIAFuelPrice
var mtoServiceItem models.MTOServiceItem
var paymentRequest models.PaymentRequest
actualPickupDate := time.Date(2020, time.July, 15, 0, 0, 0, 0, time.UTC)

setupTestData := func() {
testdatagen.MakeReContractYear(suite.DB(), testdatagen.Assertions{
ReContractYear: models.ReContractYear{
StartDate: time.Now().Add(-24 * time.Hour),
EndDate: time.Now().Add(24 * time.Hour),
},
})
mtoServiceItem = factory.BuildMTOServiceItem(suite.DB(), []factory.Customization{
{
Model: models.MTOShipment{
ActualPickupDate: &actualPickupDate,
},
},
}, []factory.Trait{
factory.GetTraitAvailableToPrimeMove,
})

paymentRequest = factory.BuildPaymentRequest(suite.DB(), []factory.Customization{
{
Model: mtoServiceItem.MoveTaskOrder,
LinkOnly: true,
},
}, nil)
}

suite.Run("lookup GHC diesel fuel price with no data", func() {
setupTestData()

paramLookup, err := ServiceParamLookupInitialize(suite.AppContextForTest(), suite.planner, mtoServiceItem, paymentRequest.ID, paymentRequest.MoveTaskOrderID, nil)
suite.FatalNoError(err)
_, err = paramLookup.ServiceParamValue(suite.AppContextForTest(), key)
suite.Error(err)
suite.Contains(err.Error(), "Looking for GHCDieselFuelPrice")
})
}
32 changes: 26 additions & 6 deletions pkg/services/ghcdieselfuelprice/ghc_diesel_fuel_price_storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ func (d *DieselFuelPriceInfo) RunStorer(appCtx appcontext.AppContext) error {
err = appCtx.DB().Where("publication_date = ?", publicationDate).First(&lastGHCDieselFuelPrice)
if err != nil {
appCtx.Logger().Info("no existing GHCDieselFuelPrice record found with", zap.String("publication_date", publicationDate.String()))
newGHCDieselFuelPrice.EffectiveDate = publicationDate.AddDate(0, 0, 1)

dayOfWeek := publicationDate.Weekday().String()
appCtx.Logger().Info("day_of_week", zap.String("day_of_week", dayOfWeek))
var daysAdded int
//fuel prices are generally published on mondays and then by business rule should expire on monday no matter what- but in case its published on a different day, we will still always expire on the following monday
switch dayOfWeek {
case "Monday":
daysAdded = 7
case "Tuesday":
daysAdded = 6
//very unlikely to get past here- monday is the normal publish day- tuesday if monday is holiday.. but adding other weekdays just in case
case "Wednesday":
daysAdded = 6
case "Thursday":
daysAdded = 4
case "Friday":
daysAdded = 3
}

newGHCDieselFuelPrice.EndDate = publicationDate.AddDate(0, 0, daysAdded)
appCtx.Logger().Info("effective_date", zap.String("effective_date", newGHCDieselFuelPrice.EffectiveDate.String()))
appCtx.Logger().Info("end_date", zap.String("EndDate", newGHCDieselFuelPrice.EndDate.String()))

verrs, err := appCtx.DB().ValidateAndCreate(&newGHCDieselFuelPrice)
if err != nil {
Expand All @@ -52,16 +75,13 @@ func (d *DieselFuelPriceInfo) RunStorer(appCtx appcontext.AppContext) error {
return fmt.Errorf("failed to validate ghcDieselFuelPrice: %w", verrs)
}
} else if priceInMillicents != lastGHCDieselFuelPrice.FuelPriceInMillicents {
appCtx.Logger().Info("Updating existing GHCDieselFuelPrice record found with", zap.String("publication_date", publicationDate.String()))
lastGHCDieselFuelPrice.FuelPriceInMillicents = priceInMillicents
appCtx.Logger().Info("existing GHCDieselFuelPrice record found with", zap.String("publication_date", publicationDate.String()))

verrs, err := appCtx.DB().ValidateAndUpdate(&lastGHCDieselFuelPrice)
//no longer updating prices throughout the week- only accept the first published price per week
if err != nil {
return fmt.Errorf("failed to update ghcDieselFuelPrice: %w", err)
}
if verrs.HasAny() {
return fmt.Errorf("failed to validate ghcDieselFuelPrice: %w", verrs)
}

} else {
appCtx.Logger().Info(
"Existing GHCDieselFuelPrice record found with matching fuel prices",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,11 @@ func (suite *GHCDieselFuelPriceServiceSuite) Test_ghcDieselFuelPriceStorer() {
suite.NoError(err)

var ghcDieselFuelPrice models.GHCDieselFuelPrice

err = suite.DB().Last(&ghcDieselFuelPrice)
suite.NoError(err)

suite.Equal("2020-06-22T00:00:00Z", ghcDieselFuelPrice.PublicationDate.Format(time.RFC3339))
suite.Equal(unit.Millicents(265900), ghcDieselFuelPrice.FuelPriceInMillicents)
})

suite.Run("run storer for existing publication date", func() {
// Under test: RunStorer function (creates or updates fuel price data for a specific publication date)
// Mocked: None
// Set up: Create a fuel price object for 20200622 then try to update it
// Expected outcome: fuel price is updated
dieselFuelPriceInfo := defaultDieselFuelPriceInfo
err := dieselFuelPriceInfo.RunStorer(suite.AppContextForTest())
suite.NoError(err)

updatedDieselFuelPriceInfo := defaultDieselFuelPriceInfo
updatedDieselFuelPriceInfo.dieselFuelPriceData.price = 2.420

err = updatedDieselFuelPriceInfo.RunStorer(suite.AppContextForTest())
suite.NoError(err)

var ghcDieselFuelPrice models.GHCDieselFuelPrice

err = suite.DB().Last(&ghcDieselFuelPrice)
suite.NoError(err)

suite.Equal("2020-06-22T00:00:00Z", ghcDieselFuelPrice.PublicationDate.Format(time.RFC3339))
suite.Equal(unit.Millicents(242000), ghcDieselFuelPrice.FuelPriceInMillicents)

count, err := suite.DB().Count(models.GHCDieselFuelPrice{})
suite.NoError(err)

suite.Equal(1, count)
})

suite.Run("test publication date in time", func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,13 @@ func (suite *PaymentRequestServiceSuite) setupRecalculateData1() (models.Move, m

// FSC price data (needs actual pickup date from move created above)
publicationDate := moveTaskOrder.MTOShipments[0].ActualPickupDate.AddDate(0, 0, -3) // 3 days earlier
effectiveDate := publicationDate.AddDate(0, 0, 1)
endDate := publicationDate.AddDate(0, 0, 7)
ghcDieselFuelPrice := models.GHCDieselFuelPrice{
PublicationDate: publicationDate,
FuelPriceInMillicents: recalculateTestFSCPrice,
EffectiveDate: effectiveDate,
EndDate: endDate,
}
suite.MustSave(&ghcDieselFuelPrice)

Expand Down
2 changes: 2 additions & 0 deletions pkg/services/ppm_closeout/ppm_closeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func (suite *PPMCloseoutSuite) TestPPMShipmentCreator() {
GHCDieselFuelPrice: models.GHCDieselFuelPrice{
FuelPriceInMillicents: unit.Millicents(281400),
PublicationDate: time.Date(2020, time.March, 9, 0, 0, 0, 0, time.UTC),
EffectiveDate: time.Date(2020, time.March, 10, 0, 0, 0, 0, time.UTC),
EndDate: time.Date(2020, time.March, 17, 0, 0, 0, 0, time.UTC),
},
})

Expand Down
2 changes: 2 additions & 0 deletions pkg/services/ppmshipment/ppm_estimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ func (suite *PPMShipmentSuite) TestPPMEstimator() {
GHCDieselFuelPrice: models.GHCDieselFuelPrice{
FuelPriceInMillicents: unit.Millicents(281400),
PublicationDate: time.Date(2020, time.March, 9, 0, 0, 0, 0, time.UTC),
EffectiveDate: time.Date(2020, time.March, 10, 0, 0, 0, 0, time.UTC),
EndDate: time.Date(2020, time.March, 16, 0, 0, 0, 0, time.UTC),
},
})

Expand Down
3 changes: 2 additions & 1 deletion pkg/testdatagen/make_ghc_diesel_fuel_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func MakeGHCDieselFuelPrice(db *pop.Connection, assertions Assertions) models.GH
ghcDieselFuelPrice := models.GHCDieselFuelPrice{
FuelPriceInMillicents: unit.Millicents(243300),
PublicationDate: time.Date(GHCTestYear, time.July, 20, 0, 0, 0, 0, time.UTC),
EffectiveDate: time.Date(GHCTestYear, time.July, 21, 0, 0, 0, 0, time.UTC),
EndDate: time.Date(GHCTestYear, time.July, 27, 0, 0, 0, 0, time.UTC),
}

mergeModels(&ghcDieselFuelPrice, assertions.GHCDieselFuelPrice)

mustCreate(db, &ghcDieselFuelPrice, assertions.Stub)
Expand Down
2 changes: 2 additions & 0 deletions scripts/run-server-test
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ else
gotest_args+=("-parallel" "8")
gotest_args+=("-failfast")
fi
## mac users uncomment the following line to run tests with the classic linker, which clears a lot of warnings that fill the console, do not commit to repo uncommented
#gotest_args+=("-ldflags=-extldflags=-Wl,-ld_classic")

# Try to compile tests, but don't run them.
if [[ "${DRY_RUN:-}" == "1" ]]; then
Expand Down