solidarity-transport/handler/bookings.go

95 lines
2.8 KiB
Go

package handler
import (
"errors"
"time"
"git.coopgo.io/coopgo-platform/solidarity-transport/types"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
)
func (h Handler) BookDriverJourney(passengerid string, driverid string, journeyid string, returnWaitingDuration time.Duration, priceAmount float64, priceCurrency string, data map[string]any) (*types.Booking, error) {
journey, err := h.Storage.GetDriverJourney(journeyid)
if err != nil {
log.Error().Err(err).Msg("could not find driver journey")
return nil, err
}
if journey.DriverId != driverid {
return nil, errors.New("not authorized : journey id driver and driverid mismatch")
}
journey.Price.Amount = priceAmount
journey.Price.Currency = priceCurrency
log.Debug().Float64("Price", priceAmount).Any("journey", journey.Price).Msg("store booking")
booking := types.Booking{
Id: uuid.NewString(),
GroupId: uuid.NewString(),
Status: "WAITING_CONFIRMATION",
PassengerId: passengerid,
DriverId: driverid,
Journey: journey,
ReturnWaitingDuration: returnWaitingDuration,
Data: data,
}
if err := h.Storage.CreateBooking(booking); err != nil {
log.Error().Err(err).Msg("error creating booking")
return nil, err
}
return &booking, nil
}
func (h Handler) GetBookings(startdate time.Time, enddate time.Time, passengerid string, driverid string) ([]*types.Booking, error) {
if enddate.Before(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) {
enddate = time.Date(9999, 12, 31, 0, 0, 0, 0, time.UTC)
}
log.Debug().Time("startdate", startdate).Time("enddate", enddate).Str("passengerid", passengerid).Str("driverid", driverid).Msg("GetBookings")
res := []*types.Booking{}
bookings, err := h.Storage.GetAllBookings()
if err != nil {
log.Error().Err(err).Msg("error retrieving bookings")
return nil, err
}
for _, b := range bookings {
if b.Journey.DriverDepartureDate.After(startdate) && b.Journey.DriverDepartureDate.Before(enddate) {
if (passengerid == "" || b.PassengerId == passengerid) && (driverid == "" || b.DriverId == driverid) {
res = append(res, b)
}
}
}
return res, nil
}
func (h Handler) GetBooking(id string) (*types.Booking, error) {
booking, err := h.Storage.GetBooking(id)
if err != nil {
log.Error().Err(err).Msg("could not get booking")
return nil, err
}
return booking, nil
}
func (h Handler) UpdateBooking(booking types.Booking) error {
if err := h.Storage.UpdateBooking(booking); err != nil {
log.Error().Err(err).Msg("could not update booking")
return err
}
return nil
}
func (h Handler) UpdateBookingStatus(bookingid string, newStatus string, reason string) error {
if err := h.Storage.UpdateBookingStatus(bookingid, newStatus, reason); err != nil {
log.Error().Err(err).Msg("could not update booking")
return err
}
return nil
}