Some checks failed
Build and Push Docker Image / build_and_push (push) Failing after 2m59s
Add DriverCompensationAmount and DriverCompensationCurrency fields to the Booking struct to support storing driver compensation information in the database.
123 lines
4.8 KiB
Go
123 lines
4.8 KiB
Go
package ocss
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/bsontype"
|
|
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
|
|
)
|
|
|
|
// BookingStatuus describes the status of a booking
|
|
type BookingStatus int64
|
|
|
|
const (
|
|
BookingInitiated BookingStatus = iota
|
|
// BookingWaitingConfirmation
|
|
BookingWaitingDriverConfirmation
|
|
BookingWaitingPassengerConfirmation
|
|
BookingConfirmed
|
|
BookingCancelled
|
|
BookingCompletedPendingValidation
|
|
BookingValidated
|
|
)
|
|
|
|
var bookingStatustoID = map[string]BookingStatus{
|
|
"INITIATED": BookingInitiated,
|
|
// "WAITING_CONFIRMATION": BookingWaitingConfirmation,
|
|
"WAITING_DRIVER_CONFIRMATION": BookingWaitingDriverConfirmation,
|
|
"WAITING_PASSENGER_CONFIRMATION": BookingWaitingPassengerConfirmation,
|
|
"CONFIRMED": BookingConfirmed,
|
|
"CANCELLED": BookingCancelled,
|
|
"COMPLETED_PENDING_VALIDATION": BookingCompletedPendingValidation,
|
|
"VALIDATED": BookingValidated,
|
|
}
|
|
|
|
var bookingStatustoString = map[BookingStatus]string{
|
|
BookingInitiated: "INITIATED",
|
|
// BookingWaitingConfirmation: "WAITING_CONFIRMATION",
|
|
BookingWaitingDriverConfirmation: "WAITING_DRIVER_CONFIRMATION",
|
|
BookingWaitingPassengerConfirmation: "WAITING_PASSENGER_CONFIRMATION",
|
|
BookingConfirmed: "CONFIRMED",
|
|
BookingCancelled: "CANCELLED",
|
|
BookingCompletedPendingValidation: "COMPLETED_PENDING_VALIDATION",
|
|
BookingValidated: "VALIDATED",
|
|
}
|
|
|
|
// MarshalJSON transforms a booking status to its JSON representation
|
|
func (s BookingStatus) MarshalJSON() ([]byte, error) {
|
|
buffer := bytes.NewBufferString(`"`)
|
|
buffer.WriteString(bookingStatustoString[s])
|
|
buffer.WriteString(`"`)
|
|
return buffer.Bytes(), nil
|
|
}
|
|
|
|
// MarshalBSON transforms a booking status to its BSON representation for MongoDB storage
|
|
func (s BookingStatus) MarshalBSONValue() (bsontype.Type, []byte, error) {
|
|
return bson.MarshalValue(bookingStatustoString[s])
|
|
}
|
|
|
|
// UnmarshalJSON transforms JSON to a BookingStatus
|
|
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
|
|
}
|
|
|
|
// UnmarshalBSONValue transforms BSON from MongoDB to a BookingStatus
|
|
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
|
|
}
|
|
|
|
// String returns the string corresponding to the BookingStatus
|
|
func (bs *BookingStatus) String() string {
|
|
if bs == nil {
|
|
return ""
|
|
}
|
|
return bookingStatustoString[*bs]
|
|
}
|
|
|
|
// BookingStatusFromString creates a new BookingStatus from the given string
|
|
func BookingStatusFromString(bs string) BookingStatus {
|
|
return bookingStatustoID[bs]
|
|
}
|
|
|
|
// Booking describes the booking parameters and status, in the context of a "Booking by API" OCSS flow
|
|
type Booking struct {
|
|
ID string `json:"id" bson:"_id"`
|
|
Driver User `json:"driver"`
|
|
Passenger User `json:"passenger"`
|
|
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"`
|
|
Status BookingStatus `json:"status"`
|
|
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"`
|
|
Price Price `json:"price"`
|
|
Car *Car `json:"car,omitempty" bson:"car,omitempty"`
|
|
DriverJourneyID string `json:"driverJourneyId" bson:"driverJourneyId"`
|
|
PassengerJourneyID string `json:"passengerJourneyId" bson:"passengerJourneyId"`
|
|
DriverCompensationAmount *float64 `json:"driverCompensationAmount,omitempty" bson:"driverCompensationAmount,omitempty"`
|
|
DriverCompensationCurrency *string `json:"driverCompensationCurrency,omitempty" bson:"driverCompensationCurrency,omitempty"`
|
|
}
|