159 lines
4.5 KiB
Go
159 lines
4.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"git.coopgo.io/coopgo-platform/routing-service"
|
|
"git.coopgo.io/coopgo-platform/solidarity-transport/types"
|
|
"github.com/google/uuid"
|
|
"github.com/paulmach/orb"
|
|
"github.com/paulmach/orb/geojson"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (h *Handler) GetDriverJourneys(departure *geojson.Feature, arrival *geojson.Feature, departureDate time.Time, noreturn bool) ([]*types.DriverJourney, error) {
|
|
minDistance := h.Config.GetInt64("parameters.limits.distance.min")
|
|
maxDistance := h.Config.GetInt64("parameters.limits.distance.max")
|
|
day := int(departureDate.Weekday())
|
|
timeInDay := departureDate.Format("15:04")
|
|
driverJourneys := []*types.DriverJourney{}
|
|
|
|
// Get Availabilities
|
|
availabilities, err := h.Storage.GetRegularAvailabilities(day, timeInDay)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error in GetRegularAvailabilities")
|
|
return nil, err
|
|
}
|
|
|
|
for _, a := range availabilities {
|
|
log.Debug().Any("availability", a).Msg("Availability found")
|
|
if a.Address != nil {
|
|
var route *routing.Route
|
|
if noreturn {
|
|
route, err = h.Routing.Route(
|
|
[]orb.Point{
|
|
a.Address.Point(),
|
|
departure.Point(),
|
|
arrival.Point(),
|
|
a.Address.Point(),
|
|
},
|
|
)
|
|
} else {
|
|
route, err = h.Routing.Route(
|
|
[]orb.Point{
|
|
a.Address.Point(),
|
|
departure.Point(),
|
|
arrival.Point(),
|
|
departure.Point(),
|
|
a.Address.Point(),
|
|
},
|
|
)
|
|
}
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("failedcomputing route request")
|
|
continue
|
|
}
|
|
|
|
log.Debug().Any("route", route).Msg("debug route")
|
|
|
|
passengerDistance := int64(route.Legs[1].Distance)
|
|
if !noreturn {
|
|
passengerDistance = passengerDistance + int64(route.Legs[2].Distance)
|
|
}
|
|
|
|
driverJourney := &types.DriverJourney{
|
|
Id: uuid.NewString(),
|
|
DriverId: a.DriverId,
|
|
PassengerPickup: departure,
|
|
PassengerDrop: arrival,
|
|
PassengerDistance: passengerDistance,
|
|
DriverDeparture: a.Address,
|
|
DriverArrival: a.Address,
|
|
DriverDistance: int64(route.Distance),
|
|
Duration: route.Legs[1].Duration,
|
|
JourneyPolyline: route.Polyline,
|
|
PassengerPickupDate: departureDate,
|
|
DriverDepartureDate: departureDate.Add(-1 * route.Legs[0].Duration),
|
|
Price: types.SolidarityTransportPrice{
|
|
Currency: "EUR",
|
|
Amount: 0,
|
|
},
|
|
Noreturn: noreturn,
|
|
}
|
|
if driverJourney.PassengerDistance >= minDistance && driverJourney.PassengerDistance <= maxDistance {
|
|
driverJourneys = append(driverJourneys, driverJourney)
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := h.Storage.PushDriverJourneys(driverJourneys); err != nil {
|
|
log.Error().Err(err).Msg("error storing driver journeys in database")
|
|
}
|
|
|
|
return driverJourneys, nil
|
|
}
|
|
|
|
func (h Handler) GetDriverJourney(driverid string, journeyid string) (*types.DriverJourney, error) {
|
|
journey, err := h.Storage.GetDriverJourney(journeyid)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error retrieving journey")
|
|
return nil, err
|
|
}
|
|
|
|
if driverid != journey.DriverId {
|
|
return nil, errors.New("not allowed, driver id mismatch")
|
|
}
|
|
|
|
return journey, nil
|
|
}
|
|
|
|
func (h Handler) ToggleDriverJourneyNoreturn(journeyid string) error {
|
|
journey, err := h.Storage.GetDriverJourney(journeyid)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error retrieving journey")
|
|
return err
|
|
}
|
|
|
|
if journey.Noreturn {
|
|
journey.Noreturn = false
|
|
route, err := h.Routing.Route(
|
|
[]orb.Point{
|
|
journey.DriverDeparture.Point(),
|
|
journey.PassengerPickup.Point(),
|
|
journey.PassengerDrop.Point(),
|
|
journey.PassengerPickup.Point(),
|
|
journey.DriverDeparture.Point(),
|
|
},
|
|
)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error in route calculation")
|
|
return err
|
|
}
|
|
journey.PassengerDistance = int64(route.Legs[1].Distance) + int64(route.Legs[2].Distance)
|
|
journey.DriverDistance = int64(route.Distance)
|
|
journey.Duration = route.Legs[1].Duration + route.Legs[2].Duration
|
|
} else {
|
|
journey.Noreturn = true
|
|
route, err := h.Routing.Route(
|
|
[]orb.Point{
|
|
journey.DriverDeparture.Point(),
|
|
journey.PassengerPickup.Point(),
|
|
journey.PassengerDrop.Point(),
|
|
journey.DriverDeparture.Point(),
|
|
},
|
|
)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error in route calculation")
|
|
return err
|
|
}
|
|
journey.PassengerDistance = int64(route.Legs[1].Distance)
|
|
journey.DriverDistance = int64(route.Distance)
|
|
journey.Duration = route.Legs[1].Duration
|
|
}
|
|
log.Debug().Any("journey", journey).Msg("journey update")
|
|
|
|
err = h.Storage.UpdateDriverJourney(*journey)
|
|
return err
|
|
}
|