2022-08-12 12:53:54 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2022-12-05 19:06:22 +00:00
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
|
2022-08-12 12:53:54 +00:00
|
|
|
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
|
2022-12-05 19:06:22 +00:00
|
|
|
"git.coopgo.io/coopgo-platform/fleets/storage"
|
2024-11-11 18:50:17 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-08-12 12:53:54 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FleetsService struct {
|
|
|
|
fleets.FleetsClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFleetsService(dial string) (*FleetsService, error) {
|
|
|
|
conn, err := grpc.Dial(dial, grpc.WithInsecure())
|
|
|
|
|
|
|
|
client := fleets.NewFleetsClient(conn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FleetsService{
|
|
|
|
FleetsClient: client,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-12-05 19:06:22 +00:00
|
|
|
|
|
|
|
func (s *ServicesHandler) GetBooking(bookingid string) (booking storage.Booking, err error) {
|
|
|
|
request := &fleets.GetBookingRequest{
|
|
|
|
Bookingid: bookingid,
|
|
|
|
}
|
|
|
|
resp, err := s.GRPC.Fleets.GetBooking(context.TODO(), request)
|
|
|
|
if err == nil {
|
|
|
|
booking = resp.Booking.ToStorageType()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServicesHandler) GetBookings() (bookings []storage.Booking, err error) {
|
|
|
|
bookings = []storage.Booking{}
|
|
|
|
|
|
|
|
request := &fleets.GetBookingsRequest{}
|
|
|
|
resp, err := s.GRPC.Fleets.GetBookings(context.TODO(), request)
|
|
|
|
if err == nil {
|
|
|
|
for _, booking := range resp.Bookings {
|
|
|
|
bookings = append(bookings, booking.ToStorageType())
|
|
|
|
}
|
|
|
|
sort.Sort(sorting.BookingsByStartdate(bookings))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-01-17 07:31:07 +00:00
|
|
|
|
|
|
|
func (s *ServicesHandler) GetVehiclesMap() (vehicles map[string]storage.Vehicle, err error) {
|
|
|
|
vehicles = map[string]storage.Vehicle{}
|
|
|
|
|
2023-12-08 12:29:25 +00:00
|
|
|
request := &fleets.GetVehiclesRequest{
|
|
|
|
Namespaces: []string{"parcoursmob"},
|
|
|
|
}
|
2023-01-17 07:31:07 +00:00
|
|
|
resp, err := s.GRPC.Fleets.GetVehicles(context.TODO(), request)
|
2023-12-08 12:29:25 +00:00
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-12-08 12:29:25 +00:00
|
|
|
} else {
|
2023-01-17 07:31:07 +00:00
|
|
|
for _, vehicle := range resp.Vehicles {
|
|
|
|
vehicles[vehicle.Id] = vehicle.ToStorageType()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 12:29:25 +00:00
|
|
|
return vehicles, err
|
2023-01-17 07:31:07 +00:00
|
|
|
}
|