From 78ca63780728db7b611b8501bd287c9c0db7d428 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 11 Sep 2024 09:38:22 +0200 Subject: [PATCH] call calendar driver from the booking service --- internal/booking/service.go | 13 ++++-- internal/cron/job_report.go | 66 ++++++++++++----------------- internal/driver/calendar/option.go | 2 +- internal/driver/calendar/service.go | 20 +++++---- internal/server/handle_bookings.go | 22 +++++----- internal/server/routes.go | 2 +- internal/server/server.go | 5 +-- main.go | 15 +++---- 8 files changed, 69 insertions(+), 76 deletions(-) diff --git a/internal/booking/service.go b/internal/booking/service.go index dc279a6..f498acb 100644 --- a/internal/booking/service.go +++ b/internal/booking/service.go @@ -5,14 +5,21 @@ import ( "time" "gorm.io/gorm" + + "github.com/rjNemo/rentease/internal/driver/calendar" ) type Service struct { - store *PgStore + store *PgStore + calendar calendar.Client } -func NewService(db *gorm.DB) (*Service, error) { - return &Service{store: NewPgStore(db)}, nil +func NewService(db *gorm.DB, calendar calendar.Client) (*Service, error) { + return &Service{ + store: NewPgStore(db), + calendar: calendar, + }, + nil } func (bs Service) All() []*Line { diff --git a/internal/cron/job_report.go b/internal/cron/job_report.go index 1e0df05..d8dfae7 100644 --- a/internal/cron/job_report.go +++ b/internal/cron/job_report.go @@ -1,42 +1,28 @@ package cron -import ( - "fmt" - "log" - "os" - "time" - - "github.com/joho/godotenv" - "gorm.io/driver/postgres" - "gorm.io/gorm" - - "github.com/rjNemo/rentease/internal/booking" - "github.com/rjNemo/rentease/internal/pdf" -) - -func JobMonthlyBookingReport() error { - _ = godotenv.Load() - db, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{}) - if err != nil { - return fmt.Errorf("error connecting to the database %s", err) - } - - now := time.Now() - log.Println("Start Monthly Booking Report job at:", now) - service, _ := booking.NewService(db) - report := service.BuildReport("monthly", int(now.Month()), now.Year()) - - ps, err := pdf.NewPdfService( - os.Getenv("HTMLDOCS_PROJECT_ID"), - os.Getenv("HTMLDOCS_REPORT_PROJECT_ID"), - os.Getenv("HTMLDOCS_URL"), - os.Getenv("HTMLDOCS_KEY"), - ) - if err != nil { - return fmt.Errorf("error starting pdf service %s", err) - } - - _ = ps.BuildReport(report, "", int(now.Month()), now.Year()) - log.Printf("Executed Monthly Booking Report job at %v with errors: %s:", time.Now().Format(time.DateTime), err) - return err -} +// func JobMonthlyBookingReport() error { +// _ = godotenv.Load() +// db, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{}) +// if err != nil { +// return fmt.Errorf("error connecting to the database %s", err) +// } +// +// now := time.Now() +// log.Println("Start Monthly Booking Report job at:", now) +// service, _ := booking.NewService(db) +// report := service.BuildReport("monthly", int(now.Month()), now.Year()) +// +// ps, err := pdf.NewPdfService( +// os.Getenv("HTMLDOCS_PROJECT_ID"), +// os.Getenv("HTMLDOCS_REPORT_PROJECT_ID"), +// os.Getenv("HTMLDOCS_URL"), +// os.Getenv("HTMLDOCS_KEY"), +// ) +// if err != nil { +// return fmt.Errorf("error starting pdf service %s", err) +// } +// +// _ = ps.BuildReport(report, "", int(now.Month()), now.Year()) +// log.Printf("Executed Monthly Booking Report job at %v with errors: %s:", time.Now().Format(time.DateTime), err) +// return err +// } diff --git a/internal/driver/calendar/option.go b/internal/driver/calendar/option.go index e7bb6e6..76fe51d 100644 --- a/internal/driver/calendar/option.go +++ b/internal/driver/calendar/option.go @@ -1,3 +1,3 @@ package calendar -type Option func(*Service) error +type Option func(*GoogleClient) error diff --git a/internal/driver/calendar/service.go b/internal/driver/calendar/service.go index c17355a..ea35f26 100644 --- a/internal/driver/calendar/service.go +++ b/internal/driver/calendar/service.go @@ -16,12 +16,16 @@ import ( "google.golang.org/api/option" ) -type Service struct { +type Client interface { + Create(calendarId, name, description string, from, to time.Time) error +} + +type GoogleClient struct { calIds map[string]struct{} *calendar.Service } -func NewService(ctx context.Context, credJson string, opts ...Option) (*Service, error) { +func NewGoogleClient(ctx context.Context, credJson string, opts ...Option) (*GoogleClient, error) { b := []byte(credJson) config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope) @@ -35,22 +39,22 @@ func NewService(ctx context.Context, credJson string, opts ...Option) (*Service, log.Fatalf("Unable to retrieve Calendar client: %v", err) } - s := &Service{ + g := &GoogleClient{ Service: srv, calIds: make(map[string]struct{}), } for _, opt := range opts { - err := opt(s) + err := opt(g) if err != nil { return nil, err } } - return &Service{Service: srv}, nil + return &GoogleClient{Service: srv}, nil } -func (s *Service) Create(calendarId, name, description string, from, to time.Time) error { +func (s *GoogleClient) Create(calendarId, name, description string, from, to time.Time) error { // add calendarId to list of known calendars _, ok := s.calIds[calendarId] if !ok { @@ -77,8 +81,8 @@ func (s *Service) Create(calendarId, name, description string, from, to time.Tim return err } -func (s *Service) List(from, to time.Time) (*calendar.Events, error) { return nil, nil } -func (s *Service) Delete(id int) error { return nil } +func (s *GoogleClient) List(from, to time.Time) (*calendar.Events, error) { return nil, nil } +func (s *GoogleClient) Delete(id int) error { return nil } // Retrieve a token, saves the token, then returns the generated client. func getClient(ctx context.Context, config *oauth2.Config) *http.Client { diff --git a/internal/server/handle_bookings.go b/internal/server/handle_bookings.go index 02fceb8..5738c8d 100644 --- a/internal/server/handle_bookings.go +++ b/internal/server/handle_bookings.go @@ -16,7 +16,6 @@ import ( "github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/config" "github.com/rjNemo/rentease/internal/constant" - "github.com/rjNemo/rentease/internal/driver/calendar" "github.com/rjNemo/rentease/internal/view" myTime "github.com/rjNemo/rentease/pkg/time" ) @@ -229,7 +228,7 @@ func handleLineItemForm(bs *booking.Service) echo.HandlerFunc { } } -func handleCreateItem(bs *booking.Service, cs *calendar.Service, hc *config.Host) echo.HandlerFunc { +func handleCreateItem(bs *booking.Service, hc *config.Host) echo.HandlerFunc { type NewItem struct { Item string `form:"item"` PaymentMethod string `form:"method"` @@ -259,15 +258,16 @@ func handleCreateItem(bs *booking.Service, cs *calendar.Service, hc *config.Host newItems := bs.CreateItem(b.Id, itm, ni.Quantity, ni.Price, ni.PaymentMethod, b.CustomerNumber) - if err = cs.Create( - itm.CalendarId, - b.Name, - fmt.Sprintf("Reservation: %s\n %d voyageur(s)\n", b.Name, b.CustomerNumber), - b.From, b.To, - ); err != nil { - log.Warnf("could not create event: %s", err) - captureError(c, err) - } + // TODO: fix the calendar integration + // if err = cs.Create( + // itm.CalendarId, + // b.Name, + // fmt.Sprintf("Reservation: %s\n %d voyageur(s)\n", b.Name, b.CustomerNumber), + // b.From, b.To, + // ); err != nil { + // log.Warnf("could not create event: %s", err) + // captureError(c, err) + // } for _, i := range newItems { _ = renderTempl(c, http.StatusCreated, view.LineItem(&view.ItemViewModel{ diff --git a/internal/server/routes.go b/internal/server/routes.go index b321c86..a147e87 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -27,7 +27,7 @@ func (s Server) MountHandlers() { private.GET("/bookings/:id", handleBookingPage(s.bs, s.hc)) private.PUT("/bookings/:id", handleBookingUpdate(s.bs, s.hc)) private.PATCH("/bookings/:id/cancel", handleBookingCancel(s.bs)) - private.POST("/bookings/:id/items", handleCreateItem(s.bs, s.cs, s.hc)) + private.POST("/bookings/:id/items", handleCreateItem(s.bs, s.hc)) private.GET("/bookings/pdf/:id", handlePdfCreateInvoice(s.bs, s.ps, s.hc)) private.POST("/items/:id", handleItemPay(s.bs)) private.PUT("/items/:id", handleItemUpdate(s.bs)) diff --git a/internal/server/server.go b/internal/server/server.go index 07c1a18..4db1b66 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -20,7 +20,6 @@ import ( "github.com/rjNemo/rentease/internal/auth" "github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/config" - "github.com/rjNemo/rentease/internal/driver/calendar" "github.com/rjNemo/rentease/internal/pdf" ) @@ -29,12 +28,11 @@ type Server struct { bs *booking.Service as *auth.Service ps *pdf.PdfService - cs *calendar.Service hc *config.Host addr string } -func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar.Service, hc *config.Host, opts ...Option) (*Server, error) { +func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, hc *config.Host, opts ...Option) (*Server, error) { option := new(options) for _, opt := range opts { err := opt(option) @@ -48,7 +46,6 @@ func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar bs: bs, as: as, ps: ps, - cs: cs, hc: hc, addr: fmt.Sprintf("0.0.0.0:%d", *option.port), } diff --git a/main.go b/main.go index 0c16c0d..9b1db1f 100644 --- a/main.go +++ b/main.go @@ -56,8 +56,14 @@ func run(c context.Context, getEnv func(string) string) error { return fmt.Errorf("error connecting to the database %s", err) } + // build calendar client + gc, err := calendar.NewGoogleClient(ctx, getEnv("CALENDAR_CREDENTIALS")) + if err != nil { + return fmt.Errorf("error building calendar client %s", err) + } + // build booking service - bs, err := booking.NewService(db) + bs, err := booking.NewService(db, gc) if err != nil { return fmt.Errorf("error starting booking service %s", err) } @@ -84,12 +90,6 @@ func run(c context.Context, getEnv func(string) string) error { return fmt.Errorf("error starting auth service %s", err) } - // build calendar service - cs, err := calendar.NewService(ctx, getEnv("CALENDAR_CREDENTIALS")) - if err != nil { - return fmt.Errorf("error starting calendar service %s", err) - } - // starting server p := getEnv("PORT") port, err := strconv.Atoi(p) @@ -104,7 +104,6 @@ func run(c context.Context, getEnv func(string) string) error { bs, as, ps, - cs, config.NewHost(), // TODO: move to the database at some point server.WithPort(port), server.WithFileSystem(static),