/
fuel_eia_diesel_price.go
86 lines (74 loc) · 3.68 KB
/
fuel_eia_diesel_price.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package models
import (
"encoding/json"
"time"
"github.com/facebookgo/clock"
"github.com/gobuffalo/pop"
"github.com/gobuffalo/validate"
"github.com/gobuffalo/validate/validators"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"github.com/transcom/mymove/pkg/unit"
)
// FuelEIADieselPrice used to hold data from the SDDC Fuel Surcharge information
// found at https://etops.sddc.army.mil/pls/ppcig_camp/fsc.output to calculate a
// shipment's fuel surcharge
type FuelEIADieselPrice struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
PubDate time.Time `json:"pub_date" db:"pub_date"`
RateStartDate time.Time `json:"rate_start_date" db:"rate_start_date"`
RateEndDate time.Time `json:"rate_end_date" db:"rate_end_date"`
EIAPricePerGallonMillicents unit.Millicents `json:"eia_price_per_gallon_millicents" db:"eia_price_per_gallon_millicents"`
BaselineRate int64 `json:"baseline_rate" db:"baseline_rate"`
}
// String is not required by pop and may be deleted
func (f FuelEIADieselPrice) String() string {
jf, _ := json.Marshal(f)
return string(jf)
}
// FuelEIADieselPrices is not required by pop and may be deleted
type FuelEIADieselPrices []FuelEIADieselPrice
// String is not required by pop and may be deleted
func (f FuelEIADieselPrices) String() string {
jf, _ := json.Marshal(f)
return string(jf)
}
// FetchMostRecentFuelPrices queries and fetches all fuel_eia_diesel_prices for past specified number of months
func FetchMostRecentFuelPrices(dbConnection *pop.Connection, clock clock.Clock, numMonths int) ([]FuelEIADieselPrice, error) {
today := clock.Now()
query := dbConnection.Where("pub_date >= $1 AND pub_date <= $2", today.AddDate(0, -numMonths, 0), today)
// TODO: what if today's date is before the first Monday/publication date, but in the new month?
var fuelPrices []FuelEIADieselPrice
err := query.Eager().All(&fuelPrices)
if err != nil {
return fuelPrices, errors.Wrap(err, "Fetch line items query failed")
}
return fuelPrices, nil
}
// Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method.
// This method is not required and may be deleted.
func (f *FuelEIADieselPrice) Validate(tx *pop.Connection) (*validate.Errors, error) {
return validate.Validate(
&validators.TimeIsPresent{Field: f.PubDate, Name: "PubDate"},
&validators.TimeIsPresent{Field: f.RateStartDate, Name: "RateStartDate"},
&validators.TimeIsPresent{Field: f.RateEndDate, Name: "RateEndDate"},
&validators.IntIsGreaterThan{Field: f.EIAPricePerGallonMillicents.Int(), Name: "EIAPricePerGallonMillicents", Compared: 0},
&validators.IntIsGreaterThan{Field: int(f.BaselineRate), Name: "BaselineRate", Compared: -1},
&validators.IntIsLessThan{Field: int(f.BaselineRate), Name: "BaselineRate", Compared: 101},
&validators.TimeAfterTime{
FirstTime: f.RateEndDate, FirstName: "RateEndDate",
SecondTime: f.RateStartDate, SecondName: "RateStartDate"},
), nil
}
// ValidateCreate gets run every time you call "pop.ValidateAndCreate" method.
// This method is not required and may be deleted.
func (f *FuelEIADieselPrice) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) {
return validate.NewErrors(), nil
}
// ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method.
// This method is not required and may be deleted.
func (f *FuelEIADieselPrice) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
return validate.NewErrors(), nil
}