diff --git a/handler/bookings.go b/handler/bookings.go index 401be8d..634b8a2 100644 --- a/handler/bookings.go +++ b/handler/bookings.go @@ -9,7 +9,7 @@ import ( "github.com/rs/zerolog/log" ) -func (h Handler) BookDriverJourney(passengerid string, driverid string, journeyid string, returnWaitingDuration time.Duration, priceAmount float64, priceCurrency string, driverCompensationAmount float64, driverCompensationCurrency string, data map[string]any) (*types.Booking, error) { +func (h Handler) BookDriverJourney(passengerid string, driverid string, journeyid string, returnWaitingDuration time.Duration, priceAmount float64, priceCurrency string, driverCompensationAmount float64, driverCompensationCurrency string, data map[string]any, groupId string) (*types.Booking, error) { journey, err := h.Storage.GetDriverJourney(journeyid) if err != nil { log.Error().Err(err).Msg("could not find driver journey") @@ -25,9 +25,14 @@ func (h Handler) BookDriverJourney(passengerid string, driverid string, journeyi log.Debug().Float64("Price", priceAmount).Any("journey", journey.Price).Msg("store booking") + // Use provided group_id if set, otherwise generate a new one + if groupId == "" { + groupId = uuid.NewString() + } + booking := types.Booking{ Id: uuid.NewString(), - GroupId: uuid.NewString(), + GroupId: groupId, Status: "WAITING_CONFIRMATION", PassengerId: passengerid, DriverId: driverid, diff --git a/servers/grpc/proto/gen/solidarity-transport.pb.go b/servers/grpc/proto/gen/solidarity-transport.pb.go index 0861e94..8e11e2c 100644 --- a/servers/grpc/proto/gen/solidarity-transport.pb.go +++ b/servers/grpc/proto/gen/solidarity-transport.pb.go @@ -618,6 +618,7 @@ type BookDriverJourneyRequest struct { Data *structpb.Struct `protobuf:"bytes,7,opt,name=data,proto3" json:"data,omitempty"` DriverCompensationAmount float64 `protobuf:"fixed64,8,opt,name=driver_compensation_amount,json=driverCompensationAmount,proto3" json:"driver_compensation_amount,omitempty"` DriverCompensationCurrency string `protobuf:"bytes,9,opt,name=driver_compensation_currency,json=driverCompensationCurrency,proto3" json:"driver_compensation_currency,omitempty"` + GroupId *string `protobuf:"bytes,10,opt,name=group_id,json=groupId,proto3,oneof" json:"group_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -715,6 +716,13 @@ func (x *BookDriverJourneyRequest) GetDriverCompensationCurrency() string { return "" } +func (x *BookDriverJourneyRequest) GetGroupId() string { + if x != nil && x.GroupId != nil { + return *x.GroupId + } + return "" +} + type BookDriverJourneyResponse struct { state protoimpl.MessageState `protogen:"open.v1"` Booking *SolidarityTransportBooking `protobuf:"bytes,1,opt,name=booking,proto3" json:"booking,omitempty"` @@ -1262,7 +1270,7 @@ const file_solidarity_transport_proto_rawDesc = "" + "\n" + "journey_id\x18\x02 \x01(\tR\tjourneyId\"d\n" + "\x18GetDriverJourneyResponse\x12H\n" + - "\x0edriver_journey\x18\x01 \x01(\v2!.SolidarityTransportDriverJourneyR\rdriverJourney\"\xb5\x03\n" + + "\x0edriver_journey\x18\x01 \x01(\v2!.SolidarityTransportDriverJourneyR\rdriverJourney\"\xe2\x03\n" + "\x18BookDriverJourneyRequest\x12!\n" + "\fpassenger_id\x18\x01 \x01(\tR\vpassengerId\x12\x1b\n" + "\tdriver_id\x18\x02 \x01(\tR\bdriverId\x12*\n" + @@ -1272,7 +1280,10 @@ const file_solidarity_transport_proto_rawDesc = "" + "\x0eprice_currency\x18\x06 \x01(\tR\rpriceCurrency\x12+\n" + "\x04data\x18\a \x01(\v2\x17.google.protobuf.StructR\x04data\x12<\n" + "\x1adriver_compensation_amount\x18\b \x01(\x01R\x18driverCompensationAmount\x12@\n" + - "\x1cdriver_compensation_currency\x18\t \x01(\tR\x1adriverCompensationCurrency\"R\n" + + "\x1cdriver_compensation_currency\x18\t \x01(\tR\x1adriverCompensationCurrency\x12\x1e\n" + + "\bgroup_id\x18\n" + + " \x01(\tH\x00R\agroupId\x88\x01\x01B\v\n" + + "\t_group_id\"R\n" + "\x19BookDriverJourneyResponse\x125\n" + "\abooking\x18\x01 \x01(\v2\x1b.SolidarityTransportBookingR\abooking\"\xef\x01\n" + "%GetSolidarityTransportBookingsRequest\x129\n" + @@ -1416,6 +1427,7 @@ func file_solidarity_transport_proto_init() { } file_solidarity_transport_types_proto_init() file_solidarity_transport_proto_msgTypes[8].OneofWrappers = []any{} + file_solidarity_transport_proto_msgTypes[12].OneofWrappers = []any{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ diff --git a/servers/grpc/proto/solidarity-transport.proto b/servers/grpc/proto/solidarity-transport.proto index 739c7b0..a209b77 100644 --- a/servers/grpc/proto/solidarity-transport.proto +++ b/servers/grpc/proto/solidarity-transport.proto @@ -90,6 +90,7 @@ message BookDriverJourneyRequest { google.protobuf.Struct data = 7; double driver_compensation_amount = 8; string driver_compensation_currency = 9; + optional string group_id = 10; } message BookDriverJourneyResponse { diff --git a/servers/grpc/server/bookings.go b/servers/grpc/server/bookings.go index d310fb3..faa11dc 100644 --- a/servers/grpc/server/bookings.go +++ b/servers/grpc/server/bookings.go @@ -21,8 +21,9 @@ func (s SolidarityTransportServerImpl) BookDriverJourney(ctx context.Context, re driverCompensationCurrency := req.DriverCompensationCurrency returnWaitingDuration := time.Duration(req.ReturnWaitingDuration) data := req.Data.AsMap() + groupId := req.GetGroupId() - booking, err := s.Handler.BookDriverJourney(passengerId, driverId, journeyId, returnWaitingDuration, priceAmount, priceCurrency, driverCompensationAmount, driverCompensationCurrency, data) + booking, err := s.Handler.BookDriverJourney(passengerId, driverId, journeyId, returnWaitingDuration, priceAmount, priceCurrency, driverCompensationAmount, driverCompensationCurrency, data, groupId) if err != nil { log.Error().Err(err).Msg("issue in BookDriverJourney handler") return nil, status.Errorf(codes.Internal, "could not create booking : %v", err) diff --git a/storage/memory.go b/storage/memory.go index fe4e282..9ff563f 100644 --- a/storage/memory.go +++ b/storage/memory.go @@ -161,3 +161,15 @@ func (s MemoryStorage) UpdateBookingStatus(bookingid string, newStatus string, r return nil } + +func (s MemoryStorage) GetBookingsByGroupId(groupId string) ([]*types.Booking, error) { + var bookings []*types.Booking + + for _, booking := range s.Bookings { + if booking.GroupId == groupId { + bookings = append(bookings, booking) + } + } + + return bookings, nil +} diff --git a/storage/mock.go b/storage/mock.go index 6ee4f9d..218129a 100644 --- a/storage/mock.go +++ b/storage/mock.go @@ -241,6 +241,20 @@ func (m *MockStorage) UpdateBookingStatus(bookingid string, newStatus string, re return nil } +func (m *MockStorage) GetBookingsByGroupId(groupId string) ([]*types.Booking, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + var bookings []*types.Booking + for _, booking := range m.bookings { + if booking.GroupId == groupId { + bookings = append(bookings, booking) + } + } + + return bookings, nil +} + // Helper methods for testing // Reset clears all data - useful for test setup diff --git a/storage/mongodb.go b/storage/mongodb.go index a33c8fc..8c18fd6 100644 --- a/storage/mongodb.go +++ b/storage/mongodb.go @@ -229,3 +229,20 @@ func (s MongoDBStorage) UpdateBookingStatus(bookingid string, newStatus string, return nil } + +func (s MongoDBStorage) GetBookingsByGroupId(groupId string) ([]*types.Booking, error) { + filter := bson.M{"groupid": groupId} + + cursor, err := s.Collections.Bookings.Find(context.Background(), filter) + if err != nil { + return nil, err + } + defer cursor.Close(context.Background()) + + var bookings []*types.Booking + if err := cursor.All(context.Background(), &bookings); err != nil { + return nil, err + } + + return bookings, nil +} diff --git a/storage/storage.go b/storage/storage.go index 55b905a..49937ff 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -23,6 +23,7 @@ type Storage interface { CreateBooking(types.Booking) error GetAllBookings() ([]*types.Booking, error) GetBooking(id string) (*types.Booking, error) + GetBookingsByGroupId(groupId string) ([]*types.Booking, error) UpdateBooking(booking types.Booking) error UpdateBookingStatus(bookingid string, newStatus string, reason string) error }