From 9edad056c62b66db3b20ba2d6a23a805b4f9179b Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 8 Dec 2024 21:52:13 +0100 Subject: [PATCH] refactor: booking creation --- internal/service/booking/models.go | 22 +++++++ internal/service/booking/service.go | 27 +------- internal/service/booking/service_test.go | 82 ++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 24 deletions(-) diff --git a/internal/service/booking/models.go b/internal/service/booking/models.go index 582156f..77743ca 100644 --- a/internal/service/booking/models.go +++ b/internal/service/booking/models.go @@ -27,6 +27,22 @@ type Booking struct { Canceled bool `gorm:"default:false"` } +// NewBooking creates a new booking with the given parameters +func NewBooking(from, to time.Time, name, phoneNumber, email, platform string, + customerNumber int, platformFees float64, externalId *string) *Booking { + return &Booking{ + From: from, + To: to, + Name: name, + PhoneNumber: phoneNumber, + CustomerNumber: customerNumber, + Email: email, + Platform: platform, + PlatformFees: platformFees, + ExternalId: externalId, + } +} + func (b Booking) InvoiceNumber(hc *config.Host) string { return fmt.Sprintf("%s%04s", hc.InvoicePrefix, strconv.Itoa(b.Id+hc.CustomerSeed)) } @@ -62,6 +78,12 @@ func (b Booking) Serialize(hc *config.Host) map[string]any { } } +// WithId returns a copy of the booking with the given ID +func (b *Booking) WithId(id int) *Booking { + b.Id = id + return b +} + type BookingRequest struct { gorm.Model From time.Time diff --git a/internal/service/booking/service.go b/internal/service/booking/service.go index b1df108..25d42ac 100644 --- a/internal/service/booking/service.go +++ b/internal/service/booking/service.go @@ -52,17 +52,7 @@ func (bs Service) Search(value string) []*Line { func (bs Service) Create(From time.Time, To time.Time, Name, PhoneNumber, Email, Platform string, CustomerNumber int, PlatformFees float64, externalId *string, ) *Booking { - b := &Booking{ - Name: Name, - PhoneNumber: PhoneNumber, - CustomerNumber: CustomerNumber, - Email: Email, - From: From, - To: To, - Platform: Platform, - PlatformFees: PlatformFees, - ExternalId: externalId, - } + b := NewBooking(From, To, Name, PhoneNumber, Email, Platform, CustomerNumber, PlatformFees, externalId) err := bs.store.Create(b) if err != nil { log.Println(err) @@ -74,22 +64,11 @@ func (bs Service) One(id int) *Booking { return bs.store.Get(id) } -// TODO: return the error +// Update updates an existing booking with new data func (bs Service) Update(id int, From time.Time, To time.Time, Name string, PhoneNumber string, Email string, Platform string, CustomerNumber int, PlatformFees float64, externalId *string, ) *Booking { - b := &Booking{ - Id: id, - Name: Name, - PhoneNumber: PhoneNumber, - CustomerNumber: CustomerNumber, - Email: Email, - From: From, - To: To, - Platform: Platform, - PlatformFees: PlatformFees, - ExternalId: externalId, - } + b := NewBooking(From, To, Name, PhoneNumber, Email, Platform, CustomerNumber, PlatformFees, externalId).WithId(id) if err := bs.store.Update(b); err != nil { log.Println(err) } diff --git a/internal/service/booking/service_test.go b/internal/service/booking/service_test.go index 54ff6a6..dbd2636 100644 --- a/internal/service/booking/service_test.go +++ b/internal/service/booking/service_test.go @@ -64,3 +64,85 @@ func TestService_All(t *testing.T) { }) } } + +func TestService_Search(t *testing.T) { + sampleBookings := []*Line{ + { + Id: 1, + CustomerName: "John Doe", + From: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + To: time.Date(2024, 1, 5, 0, 0, 0, 0, time.UTC), + Platform: "Airbnb", + Total: 500.0, + Canceled: false, + }, + { + Id: 2, + CustomerName: "Jane Smith", + From: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC), + To: time.Date(2024, 2, 3, 0, 0, 0, 0, time.UTC), + Platform: "Booking.com", + Total: 300.0, + Canceled: true, + }, + } + + tests := []struct { + name string + searchQuery string + expectedLines []*Line + expectedCalled bool + }{ + { + name: "empty search query returns no results", + searchQuery: "", + expectedLines: []*Line{}, + expectedCalled: false, + }, + { + name: "search for 'John' returns matching booking", + searchQuery: "John", + expectedLines: []*Line{ + sampleBookings[0], + }, + expectedCalled: true, + }, + { + name: "search for 'Smith' returns matching booking", + searchQuery: "Smith", + expectedLines: []*Line{ + sampleBookings[1], + }, + expectedCalled: true, + }, + { + name: "search for non-existent name returns empty list", + searchQuery: "NonExistent", + expectedLines: []*Line{}, + expectedCalled: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create mocks + mockStore := new(MockStore) + mockCalendar := new(MockCalendarClient) + mockPDF := new(MockPDFClient) + + // Set up expectations + mockStore.On("Search", tt.searchQuery).Return(tt.expectedLines) + + // Create service with mocks + service, err := NewService(mockStore, mockCalendar, mockPDF) + assert.NoError(t, err) + + // Call the method + result := service.Search(tt.searchQuery) + + // Assert expectations + assert.Equal(t, tt.expectedLines, result) + mockStore.AssertExpectations(t) + }) + } +}