Booking
This commit is contained in:
64
handler/booking.go
Normal file
64
handler/booking.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/internal"
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/interoperability/ocss"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (h *CarpoolServiceHandler) Book(booking ocss.Booking) (*internal.Booking, error) {
|
||||
futureBooking := internal.Booking{}
|
||||
futureBooking.Roles = []string{}
|
||||
if booking.Driver.Operator == "ridygo.fr" {
|
||||
futureBooking.Roles = append(futureBooking.Roles, "driver")
|
||||
previous_search_result, err := h.Storage.GetSearchResult("driver", booking.DriverJourneyID)
|
||||
if err == nil {
|
||||
futureBooking.DriverJourney = &internal.PlannedJourney{
|
||||
Route: internal.RegularRoute(*previous_search_result.Route),
|
||||
DepartureDate: previous_search_result.DepartureDate,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if booking.Passenger.Operator == "ridygo.fr" {
|
||||
futureBooking.Roles = append(futureBooking.Roles, "passenger")
|
||||
previous_search_result, err := h.Storage.GetSearchResult("passenger", booking.PassengerJourneyID)
|
||||
if err == nil {
|
||||
futureBooking.PassengerJourney = &internal.PlannedJourney{
|
||||
Route: internal.RegularRoute(*previous_search_result.Route),
|
||||
DepartureDate: previous_search_result.DepartureDate,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(futureBooking.Roles) == 0 {
|
||||
return nil, errors.New("couldn't find the right operator id : \"ridygo.fr\" should be set for driver or passenger")
|
||||
}
|
||||
|
||||
if _, err := uuid.Parse(booking.ID); err != nil {
|
||||
return nil, errors.New("bookingid is not a valid uuid")
|
||||
}
|
||||
|
||||
futureBooking.ID = booking.ID
|
||||
futureBooking.BookingDefinition = booking
|
||||
|
||||
err := h.Storage.CreateBooking(futureBooking)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("issue creating booking in database")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &futureBooking, nil
|
||||
}
|
||||
|
||||
func (h *CarpoolServiceHandler) GetBooking(id string) (*internal.Booking, error) {
|
||||
booking, err := h.Storage.GetBooking(id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("issue retrieving booking in storage")
|
||||
return nil, errors.New("booking id not found")
|
||||
}
|
||||
return booking, nil
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/helpers"
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/internal"
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/tiles"
|
||||
"git.coopgo.io/coopgo-platform/routing-service/encoding/polylines"
|
||||
"github.com/google/uuid"
|
||||
@@ -57,18 +57,18 @@ func (h *CarpoolServiceHandler) CreateRegularRoutes(routes []*geojson.FeatureCol
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *CarpoolServiceHandler) GetUserPlanning(userid string, minDepartureDate time.Time, maxDepartureDate time.Time) (map[string][]helpers.PlannedRouteSchedule, error) {
|
||||
func (h *CarpoolServiceHandler) GetUserPlanning(userid string, minDepartureDate time.Time, maxDepartureDate time.Time) (map[string][]internal.PlannedRouteSchedule, error) {
|
||||
log.Debug().
|
||||
Str("user_id", userid).
|
||||
Time("min_departure_date", minDepartureDate).
|
||||
Time("max_departure_date", maxDepartureDate).
|
||||
Msg("carpool service handler - GetUserPlanning")
|
||||
|
||||
results := map[string][]helpers.PlannedRouteSchedule{}
|
||||
results := map[string][]internal.PlannedRouteSchedule{}
|
||||
|
||||
current_date := minDepartureDate
|
||||
for current_date.Before(maxDepartureDate) {
|
||||
results[current_date.Format("2006-01-02")] = []helpers.PlannedRouteSchedule{}
|
||||
results[current_date.Format("2006-01-02")] = []internal.PlannedRouteSchedule{}
|
||||
current_date = current_date.Add(24 * time.Hour)
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ func (h *CarpoolServiceHandler) GetUserPlanning(userid string, minDepartureDate
|
||||
}
|
||||
|
||||
for _, r := range routes {
|
||||
rr := helpers.RegularRoute(*r)
|
||||
rr := internal.RegularRoute(*r)
|
||||
schedules, err := rr.PlannedJourneySchedules(minDepartureDate, maxDepartureDate)
|
||||
if err != nil {
|
||||
log.Error().Err(err)
|
||||
|
||||
@@ -4,21 +4,13 @@ import (
|
||||
"time"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/geoutils"
|
||||
"git.coopgo.io/coopgo-platform/routing-service"
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/internal"
|
||||
"git.coopgo.io/coopgo-platform/routing-service/encoding/polylines"
|
||||
"github.com/paulmach/orb"
|
||||
"github.com/paulmach/orb/geojson"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type SearchResult struct {
|
||||
ID string
|
||||
Route *geojson.FeatureCollection
|
||||
DepartureDate time.Time
|
||||
Itinerary *routing.Route
|
||||
}
|
||||
|
||||
func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]SearchResult, error) {
|
||||
func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.SearchResult, error) {
|
||||
|
||||
log.Debug().
|
||||
Any("departure", departure).
|
||||
@@ -61,7 +53,7 @@ func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival o
|
||||
|
||||
candidate_routes := tileset.GetTiledRoutes()
|
||||
|
||||
journeys := []SearchResult{}
|
||||
journeys := []internal.SearchResult{}
|
||||
|
||||
counted := int64(0)
|
||||
for _, r := range candidate_routes {
|
||||
@@ -81,7 +73,7 @@ func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival o
|
||||
log.Error().Err(err).Msg("error getting route with viapoints")
|
||||
continue
|
||||
}
|
||||
journeys = append(journeys, SearchResult{
|
||||
journeys = append(journeys, internal.SearchResult{
|
||||
ID: r.ID,
|
||||
Route: r.Route,
|
||||
DepartureDate: r.DepartureDate,
|
||||
@@ -94,10 +86,16 @@ func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival o
|
||||
}
|
||||
}
|
||||
|
||||
err = h.Storage.StoreSearchResults("driver", journeys)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error saving search results")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return journeys, nil
|
||||
}
|
||||
|
||||
func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]SearchResult, error) {
|
||||
func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.SearchResult, error) {
|
||||
|
||||
log.Debug().
|
||||
Any("departure", departure).
|
||||
@@ -155,7 +153,7 @@ func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arriva
|
||||
|
||||
candidate_routes := tileset.GetTiledRoutes()
|
||||
|
||||
journeys := []SearchResult{}
|
||||
journeys := []internal.SearchResult{}
|
||||
|
||||
counted := int64(0)
|
||||
for _, r := range candidate_routes {
|
||||
@@ -173,7 +171,7 @@ func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arriva
|
||||
log.Error().Err(err).Msg("error getting route with viapoints")
|
||||
continue
|
||||
}
|
||||
journeys = append(journeys, SearchResult{
|
||||
journeys = append(journeys, internal.SearchResult{
|
||||
ID: r.ID,
|
||||
Route: r.Route,
|
||||
DepartureDate: r.DepartureDate,
|
||||
@@ -186,6 +184,12 @@ func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arriva
|
||||
}
|
||||
}
|
||||
|
||||
err = h.Storage.StoreSearchResults("passenger", journeys)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error saving search results")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return journeys, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user