diff --git a/config/host.go b/config/host.go index b8d3f83..b63a1cf 100644 --- a/config/host.go +++ b/config/host.go @@ -17,6 +17,7 @@ type Host struct { } type HostItem struct { + Name string CalendarId string // Price is the daily price in EUR Price float64 @@ -41,6 +42,7 @@ func NewHost() *Host { Platforms: []string{"Booking", "AirBnb", "TripAdvisor", "Other"}, // TODO: add to DB Items: map[string]HostItem{ // TODO: move to DB "T2": { + Name: "T2", Price: 59.0, CalendarId: os.Getenv("CALENDAR_ID_T2"), MustSyncCalendar: true, @@ -49,6 +51,7 @@ func NewHost() *Host { }, "T3": { + Name: "T3", Price: 80.0, CalendarId: os.Getenv("CALENDAR_ID_T3"), MustSyncCalendar: true, @@ -56,15 +59,19 @@ func NewHost() *Host { Taxable: true, }, "Airport": { + Name: "Airport", Price: 25.0, }, "Port": { + Name: "Port", Price: 20.0, }, "Transport": { + Name: "Transport", Price: 20.0, }, "Taxes": { // TODO: remove after auto creation enabled + Name: "Taxes", Price: 1.5, }, }, diff --git a/internal/calendar/option.go b/internal/calendar/option.go index 5ac7438..e7bb6e6 100644 --- a/internal/calendar/option.go +++ b/internal/calendar/option.go @@ -1,18 +1,3 @@ package calendar -import ( - "fmt" -) - type Option func(*Service) error - -func WithCalendar(key string, calendarId string) Option { - return func(s *Service) error { - _, err := s.CalendarList.Get(calendarId).Do() - if err != nil { - return fmt.Errorf("cannot find calendar entry with id %q: %w", calendarId, err) - } - s.calIds[key] = calendarId - return nil - } -} diff --git a/internal/calendar/service.go b/internal/calendar/service.go index 0d3b826..22d1b72 100644 --- a/internal/calendar/service.go +++ b/internal/calendar/service.go @@ -19,7 +19,7 @@ import ( const tokFile = "token.json" type Service struct { - calIds map[string]string + calIds map[string]struct{} *calendar.Service } @@ -39,7 +39,7 @@ func NewService(ctx context.Context, credJson string, opts ...Option) (*Service, s := &Service{ Service: srv, - calIds: make(map[string]string), + calIds: make(map[string]struct{}), } for _, opt := range opts { @@ -53,6 +53,16 @@ func NewService(ctx context.Context, credJson string, opts ...Option) (*Service, } func (s *Service) Create(calendarId, name, description string, from, to time.Time) error { + // add calendarId to list of known calendars + _, ok := s.calIds[calendarId] + if !ok { + _, err := s.CalendarList.Get(calendarId).Do() + if err != nil { + return fmt.Errorf("cannot find calendar entry with id %q: %w", calendarId, err) + } + s.calIds[calendarId] = struct{}{} + } + ne, err := s.Events.Insert(calendarId, &calendar.Event{ Description: name, End: &calendar.EventDateTime{ diff --git a/internal/server/handle_bookings.go b/internal/server/handle_bookings.go index 8a7fb82..c052999 100644 --- a/internal/server/handle_bookings.go +++ b/internal/server/handle_bookings.go @@ -2,7 +2,6 @@ package server import ( "context" - "errors" "fmt" "io" "net/http" @@ -129,8 +128,15 @@ func handleBookingPage(bs *booking.Service, hc *config.Host) echo.HandlerFunc { Total: strconv.FormatFloat(u.Reduce(b.Items, func(i booking.Item, sum float64) float64 { return sum + i.Price*float64(i.Quantity) }, 0.0), 'f', 2, 64), - Platforms: hc.Platforms, - ItemList: u.Map(hc.Items, func(i config.HostItem) string { return i.Name }), + Platforms: hc.Platforms, + ItemList: u.OrderBy(func(items map[string]config.HostItem) (out []string) { + for _, item := range items { + out = append(out, item.Name) + } + return out + }(hc.Items), + func(l, r string) bool { return l < r }, + ), PaymentMethods: hc.PaymentMethods, } return renderTempl(c, http.StatusOK, view.BookingById(bvm)) @@ -142,11 +148,11 @@ func handleBookingUpdate(bs *booking.Service, hc *config.Host) echo.HandlerFunc type UpdateBooking struct { From time.Time `json:"from"` To time.Time `json:"to"` + ExternalId *string `form:"external_id"` Name string `form:"name"` PhoneNumber string `form:"phone_number"` Email string `form:"email"` Platform string `form:"platform"` - ExternalId *string `form:"external_id"` Id int `param:"id"` CustomerNumber int `form:"customer_number"` PlatformFees float64 `form:"platform_fees"` @@ -238,7 +244,7 @@ func handleCreateItem(bs *booking.Service, cs *calendar.Service, hc *config.Host itm, ok := hc.Items[ni.Item] if !ok { - return errors.New(fmt.Sprintf("invalid item name %q", ni.Item)) + return fmt.Errorf("invalid item name %q", ni.Item) } i := bs.CreateItem(b.Id, ni.Item, ni.Quantity, ni.Price, ni.PaymentMethod) diff --git a/internal/server/routes.go b/internal/server/routes.go index d1848da..51dff6a 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -32,7 +32,7 @@ func (s Server) MountHandlers() { g.GET("/bookings/:id", handleBookingPage(s.bs, s.hc)) g.PUT("/bookings/:id", handleBookingUpdate(s.bs, s.hc)) g.PATCH("/bookings/:id/cancel", handleBookingCancel(s.bs)) - g.POST("/bookings/:id/items", handleCreateItem(s.bs)) + g.POST("/bookings/:id/items", handleCreateItem(s.bs, s.cs, s.hc)) g.POST("/items/:id", handleItemPay(s.bs)) g.PUT("/items/:id", handleItemUpdate(s.bs)) g.GET("/items/:id", handleLineItemForm(s.bs))