package grpcserver import ( "context" "time" "git.coopgo.io/coopgo-platform/carpool-service/servers/grpc/proto" "github.com/paulmach/orb" "github.com/rs/zerolog/log" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/timestamppb" ) func (s *CarpoolServiceServerImpl) DriverJourneys(ctx context.Context, req *proto.DriverJourneysRequest) (*proto.DriverJourneysResponse, error) { log.Debug(). Str("departure date", req.DepartureDate.String()). Msg("grpc server - DriverJourneys") departure := orb.Point{req.DepartureLng, req.DepartureLat} arrival := orb.Point{req.ArrivalLng, req.ArrivalLat} td := 900 * time.Second if req.TimeDelta != nil { td = time.Duration(*req.TimeDelta) * time.Second } departureDate := req.DepartureDate.AsTime() minDate := departureDate.Add(-td) maxDate := departureDate.Add(td) log.Debug(). Str("departure_date", departureDate.Format(time.RFC3339)). Str("mindate", minDate.Format(time.RFC3339)). Str("maxdate", maxDate.Format(time.RFC3339)). Int64("td", int64(td)). Msg("DriverJourneys show dates") journeys, err := s.Handler.GetDriverJourneys(departure, arrival, req.DepartureRadius, req.ArrivalRadius, minDate, maxDate, req.Count) if err != nil { log.Error().Err(err).Msg("error finding driver journeys") return nil, err } results := []*proto.CarpoolServiceDriverJourney{} for _, j := range journeys { properties := j.Route.ExtraMembers["properties"].(map[string]any) usermap := properties["user"].(map[string]any) journeyId := j.ID driverDepartureLat := j.Route.Features[0].Point().Lat() driverDepartureLng := j.Route.Features[0].Point().Lon() driverArrivalLat := j.Route.Features[1].Point().Lat() driverArrivalLng := j.Route.Features[1].Point().Lon() duration := time.Duration(0) var distance *int64 if len(j.Itinerary.Legs) > 2 { duration = j.Itinerary.Legs[1].Duration dist := int64(j.Itinerary.Legs[1].Distance) distance = &dist } results = append(results, &proto.CarpoolServiceDriverJourney{ Driver: &proto.CarpoolServiceUser{ Id: usermap["id"].(string), Operator: usermap["operator"].(string), Alias: usermap["alias"].(string), }, Operator: "ridygo.fr", PassengerPickupLat: req.DepartureLat, PassengerPickupLng: req.DepartureLng, PassengerDropLat: req.ArrivalLat, PassengerDropLng: req.ArrivalLng, DriverDepartureLat: &driverDepartureLat, DriverDepartureLng: &driverDepartureLng, DriverArrivalLat: &driverArrivalLat, DriverArrivalLng: &driverArrivalLng, Duration: int64(duration), Distance: distance, Id: journeyId, PassengerPickupDate: timestamppb.New(j.DepartureDate.Add(j.Itinerary.Legs[0].Duration)), DriverDepartureDate: timestamppb.New(j.DepartureDate), Type: proto.CarpoolServiceJourneyType_PLANNED, }) } return &proto.DriverJourneysResponse{ DriverJourneys: results, }, nil } func (s *CarpoolServiceServerImpl) PassengerJourneys(ctx context.Context, req *proto.PassengerJourneysRequest) (*proto.PassengerJourneysResponse, error) { log.Debug(). Str("departure date", req.DepartureDate.String()). Msg("grpc server - PassengerJourneys") departure := orb.Point{req.DepartureLng, req.DepartureLat} arrival := orb.Point{req.ArrivalLng, req.ArrivalLat} td := 900 * time.Second if req.TimeDelta != nil { td = time.Duration(*req.TimeDelta) * time.Second } minDate := req.DepartureDate.AsTime().Add(-td) maxDate := req.DepartureDate.AsTime().Add(td) journeys, err := s.Handler.GetPassengerJourneys(departure, arrival, req.DepartureRadius, req.ArrivalRadius, minDate, maxDate, req.Count) if err != nil { return nil, err } results := []*proto.CarpoolServicePassengerJourney{} for _, j := range journeys { properties := j.Route.ExtraMembers["properties"].(map[string]any) usermap := properties["user"].(map[string]any) journeyId := j.ID passengerDepartureLat := j.Route.Features[0].Point().Lat() passengerDepartureLng := j.Route.Features[0].Point().Lon() passengerArrivalLat := j.Route.Features[1].Point().Lat() passengerArrivalLng := j.Route.Features[1].Point().Lon() passengerDepartureDate := timestamppb.New(j.DepartureDate) driverDepartureDate := timestamppb.New(j.DepartureDate.Add(-j.Itinerary.Legs[0].Duration)) duration := time.Duration(0) var distance *int64 log.Debug().Any("itinerary", j.Itinerary).Msg("debug itinerary") if len(j.Itinerary.Legs) > 2 { duration = j.Itinerary.Legs[1].Duration / time.Second dist := int64(j.Itinerary.Legs[1].Distance) distance = &dist } results = append(results, &proto.CarpoolServicePassengerJourney{ Passenger: &proto.CarpoolServiceUser{ Id: usermap["id"].(string), Operator: usermap["operator"].(string), Alias: usermap["alias"].(string), }, Operator: "ridygo.fr", PassengerPickupLat: passengerDepartureLat, PassengerPickupLng: passengerDepartureLng, PassengerDropLat: passengerArrivalLat, PassengerDropLng: passengerArrivalLng, DriverDepartureLat: &req.DepartureLat, DriverDepartureLng: &req.DepartureLng, DriverArrivalLat: &req.ArrivalLat, DriverArrivalLng: &req.ArrivalLng, Duration: int64(duration), Distance: distance, Id: journeyId, PassengerPickupDate: passengerDepartureDate, DriverDepartureDate: driverDepartureDate, Type: proto.CarpoolServiceJourneyType_PLANNED, }) } return &proto.PassengerJourneysResponse{ PassengerJourneys: results, }, nil } func (s *CarpoolServiceServerImpl) DriverRegularTrips(context.Context, *proto.DriverRegularTripsRequest) (*proto.DriverRegularTripsResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method DriverRegularTrips not implemented") } func (s *CarpoolServiceServerImpl) PassengerRegularTrips(context.Context, *proto.PassengerRegularTripsRequest) (*proto.PassengerRegularTripsResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method PassengerRegularTrips not implemented") }