2023-03-27 18:54:56 +00:00
|
|
|
package ocss
|
|
|
|
|
2023-03-30 06:44:58 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-05-07 23:29:59 +00:00
|
|
|
"fmt"
|
2023-03-30 06:44:58 +00:00
|
|
|
"time"
|
2023-05-07 23:29:59 +00:00
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
|
|
|
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
|
2023-03-30 06:44:58 +00:00
|
|
|
)
|
2023-03-27 18:54:56 +00:00
|
|
|
|
2023-09-26 14:42:46 +00:00
|
|
|
// BookingStatuus describes the status of a booking
|
2023-03-27 18:54:56 +00:00
|
|
|
type BookingStatus int64
|
|
|
|
|
|
|
|
const (
|
2023-05-07 23:29:59 +00:00
|
|
|
BookingInitiated BookingStatus = iota
|
|
|
|
// BookingWaitingConfirmation
|
|
|
|
BookingWaitingDriverConfirmation
|
|
|
|
BookingWaitingPassengerConfirmation
|
2023-03-27 18:54:56 +00:00
|
|
|
BookingConfirmed
|
|
|
|
BookingCancelled
|
|
|
|
BookingCompletedPendingValidation
|
|
|
|
BookingValidated
|
|
|
|
)
|
|
|
|
|
2023-03-30 06:44:58 +00:00
|
|
|
var bookingStatustoID = map[string]BookingStatus{
|
2023-05-07 23:29:59 +00:00
|
|
|
"INITIATED": BookingInitiated,
|
|
|
|
// "WAITING_CONFIRMATION": BookingWaitingConfirmation,
|
|
|
|
"WAITING_DRIVER_CONFIRMATION": BookingWaitingDriverConfirmation,
|
|
|
|
"WAITING_PASSENGER_CONFIRMATION": BookingWaitingPassengerConfirmation,
|
|
|
|
"CONFIRMED": BookingConfirmed,
|
|
|
|
"CANCELLED": BookingCancelled,
|
|
|
|
"COMPLETED_PENDING_VALIDATION": BookingCompletedPendingValidation,
|
|
|
|
"VALIDATED": BookingValidated,
|
2023-03-30 06:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var bookingStatustoString = map[BookingStatus]string{
|
2023-05-07 23:29:59 +00:00
|
|
|
BookingInitiated: "INITIATED",
|
|
|
|
// BookingWaitingConfirmation: "WAITING_CONFIRMATION",
|
|
|
|
BookingWaitingDriverConfirmation: "WAITING_DRIVER_CONFIRMATION",
|
|
|
|
BookingWaitingPassengerConfirmation: "WAITING_PASSENGER_CONFIRMATION",
|
|
|
|
BookingConfirmed: "CONFIRMED",
|
|
|
|
BookingCancelled: "CANCELLED",
|
|
|
|
BookingCompletedPendingValidation: "COMPLETED_PENDING_VALIDATION",
|
|
|
|
BookingValidated: "VALIDATED",
|
2023-03-30 06:44:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 14:42:46 +00:00
|
|
|
// MarshalJSON transforms a booking status to its JSON representation
|
2023-03-30 06:44:58 +00:00
|
|
|
func (s BookingStatus) MarshalJSON() ([]byte, error) {
|
|
|
|
buffer := bytes.NewBufferString(`"`)
|
|
|
|
buffer.WriteString(bookingStatustoString[s])
|
|
|
|
buffer.WriteString(`"`)
|
|
|
|
return buffer.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:42:46 +00:00
|
|
|
// MarshalBSON transforms a booking status to its BSON representation for MongoDB storage
|
2023-05-07 23:29:59 +00:00
|
|
|
func (s BookingStatus) MarshalBSONValue() (bsontype.Type, []byte, error) {
|
|
|
|
return bson.MarshalValue(bookingStatustoString[s])
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:42:46 +00:00
|
|
|
// UnmarshalJSON transforms JSON to a BookingStatus
|
2023-03-30 06:44:58 +00:00
|
|
|
func (bs *BookingStatus) UnmarshalJSON(b []byte) error {
|
|
|
|
var j string
|
|
|
|
err := json.Unmarshal(b, &j)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*bs = bookingStatustoID[j]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:42:46 +00:00
|
|
|
// UnmarshalBSONValue transforms BSON from MongoDB to a BookingStatus
|
2023-05-07 23:29:59 +00:00
|
|
|
func (bs *BookingStatus) UnmarshalBSONValue(t bsontype.Type, b []byte) error {
|
|
|
|
if t == bsontype.Null || len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
j, _, ok := bsoncore.ReadString(b)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("cannot parse status")
|
|
|
|
}
|
|
|
|
*bs = bookingStatustoID[j]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:42:46 +00:00
|
|
|
// String returns the string corresponding to the BookingStatus
|
2023-05-07 23:29:59 +00:00
|
|
|
func (bs *BookingStatus) String() string {
|
|
|
|
if bs == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return bookingStatustoString[*bs]
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:42:46 +00:00
|
|
|
// BookingStatusFromString creates a new BookingStatus from the given string
|
2023-05-07 23:29:59 +00:00
|
|
|
func BookingStatusFromString(bs string) BookingStatus {
|
|
|
|
return bookingStatustoID[bs]
|
|
|
|
}
|
|
|
|
|
2023-09-26 14:42:46 +00:00
|
|
|
// Booking describes the booking parameters and status, in the context of a "Booking by API" OCSS flow
|
2023-03-27 18:54:56 +00:00
|
|
|
type Booking struct {
|
2023-05-07 23:29:59 +00:00
|
|
|
ID string `json:"id" bson:"_id"`
|
2023-03-30 06:44:58 +00:00
|
|
|
Driver User `json:"driver"`
|
2023-03-27 18:54:56 +00:00
|
|
|
Passenger User `json:"passenger"`
|
2023-05-07 23:29:59 +00:00
|
|
|
PassengerPickupDate OCSSTime `json:"passengerPickupDate" bson:"passengerPickupDate"`
|
|
|
|
PassengerPickupLat float64 `json:"passengerPickupLat" bson:"passengerPickupLat"`
|
|
|
|
PassengerPickupLng float64 `json:"passengerPickupLng" bson:"passengerPickupLng"`
|
|
|
|
PassengerDropLat float64 `json:"passengerDropLat" bson:"passengerDropLat"`
|
|
|
|
PassengerDropLng float64 `json:"passengerDropLng" bson:"passengerDropLng"`
|
|
|
|
PassengerPickupAddress *string `json:"passengerPickupAddress,omitempty" bson:"passengerPickupAddress,omitempty"`
|
|
|
|
PassengerDropAddress *string `json:"passengerDropAddress,omitempty" bson:"passengerDropAddress,omitempty"`
|
2023-03-27 18:54:56 +00:00
|
|
|
Status BookingStatus `json:"status"`
|
2023-05-07 23:29:59 +00:00
|
|
|
Distance *int64 `json:"distance,omitempty" bson:"distance,omitempty"`
|
|
|
|
Duration *time.Duration `json:"duration,omitempty" bson:"duration,omitempty"`
|
|
|
|
WebUrl *string `json:"webUrl,omitempty" bson:"webUrl,omitempty"`
|
2023-03-27 18:54:56 +00:00
|
|
|
Price Price `json:"price"`
|
2023-05-07 23:29:59 +00:00
|
|
|
Car *Car `json:"car,omitempty" bson:"car,omitempty"`
|
|
|
|
DriverJourneyID string `json:"driverJourneyId" bson:"driverJourneyId"`
|
|
|
|
PassengerJourneyID string `json:"passengerJourneyId" bson:"passengerJourneyId"`
|
2023-03-27 18:54:56 +00:00
|
|
|
}
|