carpool-service/servers/grpc/proto/helpers.go

444 lines
14 KiB
Go

package proto
import (
"time"
"git.coopgo.io/coopgo-platform/carpool-service/internal"
"git.coopgo.io/coopgo-platform/carpool-service/interoperability/ocss"
"google.golang.org/protobuf/types/known/timestamppb"
)
func (j *CarpoolServiceDriverJourney) ToOCSS() ocss.DriverJourney {
var departureToPickupWalkingDuration *time.Duration
if j.DepartureToPickupWalkingDuration != nil {
dtpw := time.Duration(*j.DepartureToPickupWalkingDuration)
departureToPickupWalkingDuration = &dtpw
}
var dropoffToArrivalWalkingDuration *time.Duration
if j.DropoffToArrivalWalkingDuration != nil {
dtpw := time.Duration(*j.DropoffToArrivalWalkingDuration)
dropoffToArrivalWalkingDuration = &dtpw
}
var car *ocss.Car
if j.Car != nil {
car = &ocss.Car{
Model: j.Car.Model,
Brand: j.Car.Brand,
}
}
var preferences *ocss.Preferences
if j.Preferences != nil {
preferences = &ocss.Preferences{
Smoking: j.Preferences.Smoking,
Animals: j.Preferences.Animals,
Music: j.Preferences.Music,
IsTalker: j.Preferences.IsTalker,
LuggageSize: j.Preferences.LuggageSize,
}
}
var driverDepartureDate *ocss.OCSSTime
if j.DriverDepartureDate != nil {
ddd := ocss.OCSSTime(j.DriverDepartureDate.AsTime())
driverDepartureDate = &ddd
}
var price *ocss.Price
if j.Price != nil {
var priceType *ocss.PriceType
if j.Price.Type != nil {
if *j.Price.Type == CarpoolServicePriceType_FREE {
pt := ocss.Free
priceType = &pt
} else if *j.Price.Type == CarpoolServicePriceType_PAYING {
pt := ocss.Paying
priceType = &pt
} else if *j.Price.Type == CarpoolServicePriceType_UNKNOWN {
pt := ocss.Unknown
priceType = &pt
}
}
price = &ocss.Price{
Amount: j.Price.Amount,
Currency: j.Price.Currency,
Type: priceType,
}
}
return ocss.DriverJourney{
DriverTrip: ocss.DriverTrip{
Driver: ocss.User{
ID: j.Driver.Id,
Operator: j.Driver.Operator,
Alias: j.Driver.Alias,
FirstName: j.Driver.FirstName,
LastName: j.Driver.LastName,
Grade: j.Driver.Grade,
Picture: j.Driver.Picture,
Gender: GenderToOCSS(j.Driver.Gender),
VerifiedIdentity: j.Driver.VerifiedIdentity,
},
DepartureToPickupWalkingDistance: j.DepartureToPickupWalkingDistance,
DepartureToPickupWalkingDuration: departureToPickupWalkingDuration,
DepartureToPickupWalkingPolyline: j.DepartureToPickupWalkingPolyline,
DropoffToArrivalWalkingDistance: j.DropoffToArrivalWalkingDistance,
DropoffToArrivalWalkingDuration: dropoffToArrivalWalkingDuration,
DropoffToArrivalWalkingPolyline: j.DropoffToArrivalWalkingPolyline,
Car: car,
Trip: ocss.Trip{
Operator: j.Operator,
PassengerPickupLat: j.PassengerPickupLat,
PassengerPickupLng: j.PassengerPickupLng,
PassengerDropLat: j.PassengerDropLat,
PassengerDropLng: j.PassengerDropLng,
PassengerPickupAddress: j.PassengerPickupAddress,
PassengerDropAddress: j.PassengerDropAddress,
Duration: time.Duration(j.Duration),
Distance: j.Distance,
DriverDepartureLat: j.DriverDepartureLat,
DriverDepartureLng: j.DriverDepartureLng,
DriverArrivalLat: j.DriverArrivalLat,
DriverArrivalLng: j.DriverArrivalLng,
DriverDepartureAddress: j.DriverDepartureAddress,
DriverArrivalAddress: j.DriverArrivalAddress,
JourneyPolyline: j.JourneyPolyline,
Preferences: preferences,
},
},
JourneySchedule: ocss.JourneySchedule{
ID: &j.Id,
PassengerPickupDate: ocss.OCSSTime(j.PassengerPickupDate.AsTime()),
DriverDepartureDate: driverDepartureDate,
WebUrl: j.WebUrl,
Type: j.Type.ToOCSS(),
},
AvailableSteats: j.AvailableSeats,
Price: price,
}
}
func (j *CarpoolServicePassengerJourney) ToOCSS() ocss.PassengerJourney {
var preferences *ocss.Preferences
if j.Preferences != nil {
preferences = &ocss.Preferences{
Smoking: j.Preferences.Smoking,
Animals: j.Preferences.Animals,
Music: j.Preferences.Music,
IsTalker: j.Preferences.IsTalker,
LuggageSize: j.Preferences.LuggageSize,
}
}
var driverDepartureDate *ocss.OCSSTime
if j.DriverDepartureDate != nil {
ddd := ocss.OCSSTime(j.DriverDepartureDate.AsTime())
driverDepartureDate = &ddd
}
return ocss.PassengerJourney{
PassengerTrip: ocss.PassengerTrip{
Passenger: ocss.User{
ID: j.Passenger.Id,
Operator: j.Passenger.Operator,
Alias: j.Passenger.Alias,
FirstName: j.Passenger.FirstName,
LastName: j.Passenger.LastName,
Grade: j.Passenger.Grade,
Picture: j.Passenger.Picture,
Gender: GenderToOCSS(j.Passenger.Gender),
VerifiedIdentity: j.Passenger.VerifiedIdentity,
},
Trip: ocss.Trip{
Operator: j.Operator,
PassengerPickupLat: j.PassengerPickupLat,
PassengerPickupLng: j.PassengerPickupLng,
PassengerDropLat: j.PassengerDropLat,
PassengerDropLng: j.PassengerDropLng,
PassengerPickupAddress: j.PassengerPickupAddress,
PassengerDropAddress: j.PassengerDropAddress,
Duration: time.Duration(j.Duration),
Distance: j.Distance,
DriverDepartureLat: j.DriverDepartureLat,
DriverDepartureLng: j.DriverDepartureLng,
DriverArrivalLat: j.DriverArrivalLat,
DriverArrivalLng: j.DriverArrivalLng,
DriverDepartureAddress: j.DriverDepartureAddress,
DriverArrivalAddress: j.DriverArrivalAddress,
JourneyPolyline: j.JourneyPolyline,
Preferences: preferences,
},
},
JourneySchedule: ocss.JourneySchedule{
ID: &j.Id,
PassengerPickupDate: ocss.OCSSTime(j.PassengerPickupDate.AsTime()),
DriverDepartureDate: driverDepartureDate,
WebUrl: j.WebUrl,
Type: j.Type.ToOCSS(),
},
RequestedSteats: j.RequestedSeats,
}
}
func (b *CarpoolServiceBooking) ToOCSS() ocss.Booking {
var duration *time.Duration
if b.Duration != nil {
d := time.Duration(*b.Duration) * time.Second
duration = &d
}
pt := ocss.Free
am := 0.0
cu := "EUR"
price := ocss.Price{
Type: &pt,
Amount: &am,
Currency: &cu,
}
if b.Price != nil {
pricetype := ocss.Unknown
if *b.Price.Type == CarpoolServicePriceType_FREE {
pricetype = ocss.Free
} else if *b.Price.Type == CarpoolServicePriceType_PAYING {
pricetype = ocss.Paying
}
price = ocss.Price{
Type: &pricetype,
Amount: b.Price.Amount,
Currency: b.Price.Currency,
}
}
status := ocss.BookingInitiated
if b.Status == CarpoolServiceBookingStatus_WAITING_DRIVER_CONFIRMATION {
status = ocss.BookingWaitingDriverConfirmation
} else if b.Status == CarpoolServiceBookingStatus_WAITING_PASSENGER_CONFIRMATION {
status = ocss.BookingWaitingPassengerConfirmation
} else if b.Status == CarpoolServiceBookingStatus_CONFIRMED {
status = ocss.BookingConfirmed
} else if b.Status == CarpoolServiceBookingStatus_COMPLETED_PENDING_VALIDATION {
status = ocss.BookingCompletedPendingValidation
} else if b.Status == CarpoolServiceBookingStatus_VALIDATED {
status = ocss.BookingValidated
} else if b.Status == CarpoolServiceBookingStatus_CANCELLED {
status = ocss.BookingCancelled
}
return ocss.Booking{
ID: b.Id,
Driver: ocss.User{
ID: b.Driver.Id,
Operator: b.Driver.Operator,
Alias: b.Driver.Alias,
FirstName: b.Driver.FirstName,
LastName: b.Driver.LastName,
Grade: b.Driver.Grade,
Picture: b.Driver.Picture,
Gender: GenderToOCSS(b.Driver.Gender),
VerifiedIdentity: b.Driver.VerifiedIdentity,
},
Passenger: ocss.User{
ID: b.Passenger.Id,
Operator: b.Passenger.Operator,
Alias: b.Passenger.Alias,
FirstName: b.Passenger.FirstName,
LastName: b.Passenger.LastName,
Grade: b.Passenger.Grade,
Picture: b.Passenger.Picture,
Gender: GenderToOCSS(b.Passenger.Gender),
VerifiedIdentity: b.Passenger.VerifiedIdentity,
},
PassengerPickupDate: ocss.OCSSTime(b.PassengerPickupDate.AsTime()),
PassengerPickupLat: b.PassengerPickupLat,
PassengerPickupLng: b.PassengerPickupLng,
PassengerDropLat: b.PassengerDropLat,
PassengerDropLng: b.PassengerDropLng,
PassengerPickupAddress: b.PassengerPickupAddress,
PassengerDropAddress: b.PassengerDropAddress,
Distance: b.Distance,
Duration: duration,
WebUrl: b.WebUrl,
Price: price,
Status: status,
DriverJourneyID: b.DriverJourneyId,
PassengerJourneyID: b.PassengerJourneyId,
}
}
func BookingFromOCSS(b ocss.Booking) *CarpoolServiceBooking {
passengerPickupDate := b.PassengerPickupDate.ToTime()
status := CarpoolServiceBookingStatus_INITIATED
if b.Status == ocss.BookingWaitingDriverConfirmation {
status = CarpoolServiceBookingStatus_WAITING_DRIVER_CONFIRMATION
} else if b.Status == ocss.BookingWaitingPassengerConfirmation {
status = CarpoolServiceBookingStatus_WAITING_PASSENGER_CONFIRMATION
} else if b.Status == ocss.BookingConfirmed {
status = CarpoolServiceBookingStatus_CONFIRMED
} else if b.Status == ocss.BookingCompletedPendingValidation {
status = CarpoolServiceBookingStatus_COMPLETED_PENDING_VALIDATION
} else if b.Status == ocss.BookingValidated {
status = CarpoolServiceBookingStatus_VALIDATED
} else if b.Status == ocss.BookingCancelled {
status = CarpoolServiceBookingStatus_CANCELLED
}
var duration *int64
if b.Duration != nil {
d := int64(b.Duration.Seconds())
duration = &d
}
var pricetype *CarpoolServicePriceType
if b.Price.Type != nil {
if *b.Price.Type == ocss.Free {
pt := CarpoolServicePriceType_FREE
pricetype = &pt
} else if *b.Price.Type == ocss.Paying {
pt := CarpoolServicePriceType_PAYING
pricetype = &pt
} else if *b.Price.Type == ocss.Unknown {
pt := CarpoolServicePriceType_UNKNOWN
pricetype = &pt
}
}
price := CarpoolServicePrice{
Amount: b.Price.Amount,
Currency: b.Price.Currency,
Type: pricetype,
}
return &CarpoolServiceBooking{
Id: b.ID,
Driver: &CarpoolServiceUser{
Id: b.Driver.ID,
Operator: b.Driver.Operator,
Alias: b.Driver.Alias,
FirstName: b.Driver.FirstName,
LastName: b.Driver.LastName,
Grade: b.Driver.Grade,
Picture: b.Driver.Picture,
Gender: b.Driver.Gender.ToString(),
VerifiedIdentity: b.Driver.VerifiedIdentity,
},
Passenger: &CarpoolServiceUser{
Id: b.Passenger.ID,
Operator: b.Passenger.Operator,
Alias: b.Passenger.Alias,
FirstName: b.Passenger.FirstName,
LastName: b.Passenger.LastName,
Grade: b.Passenger.Grade,
Picture: b.Passenger.Picture,
Gender: b.Passenger.Gender.ToString(),
VerifiedIdentity: b.Passenger.VerifiedIdentity,
},
PassengerPickupDate: timestamppb.New(*passengerPickupDate),
PassengerPickupLat: b.PassengerPickupLat,
PassengerPickupLng: b.PassengerPickupLng,
PassengerDropLat: b.PassengerDropLat,
PassengerDropLng: b.PassengerDropLng,
PassengerPickupAddress: b.PassengerPickupAddress,
PassengerDropAddress: b.PassengerDropAddress,
Status: status,
Distance: b.Distance,
Duration: duration,
WebUrl: b.WebUrl,
Price: &price,
DriverJourneyId: b.DriverJourneyID,
PassengerJourneyId: b.PassengerJourneyID,
}
}
func BookingFromInternal(b internal.Booking) *CarpoolServiceBooking {
booking := BookingFromOCSS(b.Booking)
if b.DriverRoute != nil {
serializedDriverRoute, err := b.DriverRoute.MarshalJSON()
if err == nil {
booking.DriverRoute = &CarpoolFeatureCollection{
Serialized: string(serializedDriverRoute),
}
}
}
if b.PassengerRoute != nil {
serializedPassengerRoute, err := b.PassengerRoute.MarshalJSON()
if err == nil {
booking.PassengerRoute = &CarpoolFeatureCollection{
Serialized: string(serializedPassengerRoute),
}
}
}
return booking
}
func (s CarpoolServiceBookingStatus) ToOCSS() ocss.BookingStatus {
if s == CarpoolServiceBookingStatus_INITIATED {
return ocss.BookingInitiated
} else if s == CarpoolServiceBookingStatus_WAITING_DRIVER_CONFIRMATION {
return ocss.BookingWaitingDriverConfirmation
} else if s == CarpoolServiceBookingStatus_WAITING_PASSENGER_CONFIRMATION {
return ocss.BookingWaitingPassengerConfirmation
} else if s == CarpoolServiceBookingStatus_CONFIRMED {
return ocss.BookingConfirmed
} else if s == CarpoolServiceBookingStatus_COMPLETED_PENDING_VALIDATION {
return ocss.BookingCompletedPendingValidation
} else if s == CarpoolServiceBookingStatus_VALIDATED {
return ocss.BookingValidated
} else if s == CarpoolServiceBookingStatus_CANCELLED {
return ocss.BookingCancelled
}
return ocss.BookingInitiated
}
func BookingStatusFromOCSS(b ocss.BookingStatus) CarpoolServiceBookingStatus {
if b == ocss.BookingInitiated {
return CarpoolServiceBookingStatus_INITIATED
} else if b == ocss.BookingWaitingDriverConfirmation {
return CarpoolServiceBookingStatus_WAITING_DRIVER_CONFIRMATION
} else if b == ocss.BookingWaitingPassengerConfirmation {
return CarpoolServiceBookingStatus_WAITING_PASSENGER_CONFIRMATION
} else if b == ocss.BookingConfirmed {
return CarpoolServiceBookingStatus_CONFIRMED
} else if b == ocss.BookingCompletedPendingValidation {
return CarpoolServiceBookingStatus_COMPLETED_PENDING_VALIDATION
} else if b == ocss.BookingValidated {
return CarpoolServiceBookingStatus_VALIDATED
} else if b == ocss.BookingCancelled {
return CarpoolServiceBookingStatus_CANCELLED
}
return CarpoolServiceBookingStatus_INITIATED
}
func (t CarpoolServiceJourneyType) ToOCSS() ocss.JourneyScheduleType {
if t == CarpoolServiceJourneyType_DYNAMIC {
return ocss.Dynamic
} else if t == CarpoolServiceJourneyType_LINE {
return ocss.Line
} else {
return ocss.Planned
}
}
func GenderToOCSS(g *string) *ocss.Gender {
if g == nil {
return nil
}
var gender ocss.Gender
if *g == "F" {
gender = ocss.Female
} else if *g == "M" {
gender = ocss.Male
} else if *g == "O" {
gender = ocss.Other
}
return &gender
}