carpool-service/handler/booking.go

65 lines
2.0 KiB
Go

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
}