Skip to content

Commit

Permalink
prevent requests to seatmaps from the past
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Nov 12, 2024
1 parent 0e66b03 commit a2fa325
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion go/api/data/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"time"
)

var ErrSeatMapFreshFetchRequired = errors.New("fresh fetch required but not allowed")

var metroAreaMapping = map[string][2]string{
// region Asia
"PEK": {"BJS", "Beijing, China"},
Expand Down Expand Up @@ -433,7 +435,7 @@ func (h *Handler) Airlines(ctx context.Context, prefix string) ([]common.Airline
}), nil
}

func (h *Handler) SeatMap(ctx context.Context, fn common.FlightNumber, departureAirport, arrivalAirport string, departureDate xtime.LocalDate, cabinClass lufthansa.RequestCabinClass, aircraftType, aircraftConfigurationVersion string) (*lufthansa.SeatAvailability, error) {
func (h *Handler) SeatMap(ctx context.Context, fn common.FlightNumber, departureAirport, arrivalAirport string, departureDate xtime.LocalDate, cabinClass lufthansa.RequestCabinClass, aircraftType, aircraftConfigurationVersion string, allowFetchFresh bool) (*lufthansa.SeatAvailability, error) {
s3Key := h.seatMapS3Key(fn.Airline, aircraftType, aircraftConfigurationVersion, cabinClass)
sm, found, err := h.loadSeatMapFromS3(ctx, s3Key)
if err != nil {
Expand All @@ -457,6 +459,10 @@ func (h *Handler) SeatMap(ctx context.Context, fn common.FlightNumber, departure
return sm, nil
}

if !allowFetchFresh {
return nil, ErrSeatMapFreshFetchRequired
}

sm, err = h.loadSeatMapFromLH(ctx, fn, departureAirport, arrivalAirport, departureDate, cabinClass)
if err != nil {
return nil, err
Expand Down
8 changes: 7 additions & 1 deletion go/api/web/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func NewSeatMapEndpoint(dh *data.Handler) echo.HandlerFunc {
return echo.NewHTTPError(http.StatusNotFound)
}

allowFetchFresh := fsd.DepartureTime(departureDate).After(time.Now().Add(-time.Hour * 3))
cabinClasses := []lufthansa.RequestCabinClass{
lufthansa.RequestCabinClassEco,
lufthansa.RequestCabinClassPremiumEco,
Expand All @@ -103,10 +104,15 @@ func NewSeatMapEndpoint(dh *data.Handler) echo.HandlerFunc {
cabinClass,
aircraftType,
aircraftConfigurationVersion,
allowFetchFresh,
)

if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError)
if errors.Is(err, data.ErrSeatMapFreshFetchRequired) {
return echo.NewHTTPError(http.StatusBadRequest, "Seatmaps can only be requested until 3 hours prior to departure")
} else {
return echo.NewHTTPError(http.StatusInternalServerError)
}
}

if sm != nil {
Expand Down

0 comments on commit a2fa325

Please sign in to comment.