mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
refactor: booking creation
This commit is contained in:
parent
c6ec3a3a97
commit
9edad056c6
3 changed files with 107 additions and 24 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue