86 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Go
		
	
	
	
| package handler
 | |
| 
 | |
| import (
 | |
| 	"errors"
 | |
| 	"time"
 | |
| 
 | |
| 	"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) ([]*types.DriverJourney, error) {
 | |
| 	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 {
 | |
| 			route, err := h.Routing.Route(
 | |
| 				[]orb.Point{
 | |
| 					a.Address.Point(),
 | |
| 					departure.Point(),
 | |
| 					arrival.Point(),
 | |
| 					a.Address.Point(),
 | |
| 				},
 | |
| 			)
 | |
| 			if err != nil {
 | |
| 				log.Error().Err(err).Msg("failedcomputing route request")
 | |
| 				continue
 | |
| 			}
 | |
| 
 | |
| 			log.Debug().Any("route", route).Msg("debug route")
 | |
| 
 | |
| 			driverJourney := &types.DriverJourney{
 | |
| 				Id:                  uuid.NewString(),
 | |
| 				DriverId:            a.DriverId,
 | |
| 				PassengerPickup:     departure,
 | |
| 				PassengerDrop:       arrival,
 | |
| 				PassengerDistance:   int64(route.Legs[1].Distance),
 | |
| 				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,
 | |
| 				},
 | |
| 			}
 | |
| 			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
 | |
| }
 |