call calendar driver from the booking service

This commit is contained in:
Ruidy 2024-09-11 09:38:22 +02:00
parent 4c209da0f6
commit 78ca637807
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
8 changed files with 69 additions and 76 deletions

View file

@ -5,14 +5,21 @@ import (
"time" "time"
"gorm.io/gorm" "gorm.io/gorm"
"github.com/rjNemo/rentease/internal/driver/calendar"
) )
type Service struct { type Service struct {
store *PgStore store *PgStore
calendar calendar.Client
} }
func NewService(db *gorm.DB) (*Service, error) { func NewService(db *gorm.DB, calendar calendar.Client) (*Service, error) {
return &Service{store: NewPgStore(db)}, nil return &Service{
store: NewPgStore(db),
calendar: calendar,
},
nil
} }
func (bs Service) All() []*Line { func (bs Service) All() []*Line {

View file

@ -1,42 +1,28 @@
package cron package cron
import ( // func JobMonthlyBookingReport() error {
"fmt" // _ = godotenv.Load()
"log" // db, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{})
"os" // if err != nil {
"time" // return fmt.Errorf("error connecting to the database %s", err)
// }
"github.com/joho/godotenv" //
"gorm.io/driver/postgres" // now := time.Now()
"gorm.io/gorm" // log.Println("Start Monthly Booking Report job at:", now)
// service, _ := booking.NewService(db)
"github.com/rjNemo/rentease/internal/booking" // report := service.BuildReport("monthly", int(now.Month()), now.Year())
"github.com/rjNemo/rentease/internal/pdf" //
) // ps, err := pdf.NewPdfService(
// os.Getenv("HTMLDOCS_PROJECT_ID"),
func JobMonthlyBookingReport() error { // os.Getenv("HTMLDOCS_REPORT_PROJECT_ID"),
_ = godotenv.Load() // os.Getenv("HTMLDOCS_URL"),
db, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{}) // os.Getenv("HTMLDOCS_KEY"),
if err != nil { // )
return fmt.Errorf("error connecting to the database %s", err) // if err != nil {
} // return fmt.Errorf("error starting pdf service %s", err)
// }
now := time.Now() //
log.Println("Start Monthly Booking Report job at:", now) // _ = ps.BuildReport(report, "", int(now.Month()), now.Year())
service, _ := booking.NewService(db) // log.Printf("Executed Monthly Booking Report job at %v with errors: %s:", time.Now().Format(time.DateTime), err)
report := service.BuildReport("monthly", int(now.Month()), now.Year()) // return err
// }
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
}

View file

@ -1,3 +1,3 @@
package calendar package calendar
type Option func(*Service) error type Option func(*GoogleClient) error

View file

@ -16,12 +16,16 @@ import (
"google.golang.org/api/option" "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{} calIds map[string]struct{}
*calendar.Service *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) b := []byte(credJson)
config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope) 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) log.Fatalf("Unable to retrieve Calendar client: %v", err)
} }
s := &Service{ g := &GoogleClient{
Service: srv, Service: srv,
calIds: make(map[string]struct{}), calIds: make(map[string]struct{}),
} }
for _, opt := range opts { for _, opt := range opts {
err := opt(s) err := opt(g)
if err != nil { if err != nil {
return nil, err 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 // add calendarId to list of known calendars
_, ok := s.calIds[calendarId] _, ok := s.calIds[calendarId]
if !ok { if !ok {
@ -77,8 +81,8 @@ func (s *Service) Create(calendarId, name, description string, from, to time.Tim
return err return err
} }
func (s *Service) List(from, to time.Time) (*calendar.Events, error) { return nil, nil } func (s *GoogleClient) List(from, to time.Time) (*calendar.Events, error) { return nil, nil }
func (s *Service) Delete(id int) error { return nil } func (s *GoogleClient) Delete(id int) error { return nil }
// Retrieve a token, saves the token, then returns the generated client. // Retrieve a token, saves the token, then returns the generated client.
func getClient(ctx context.Context, config *oauth2.Config) *http.Client { func getClient(ctx context.Context, config *oauth2.Config) *http.Client {

View file

@ -16,7 +16,6 @@ import (
"github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/booking"
"github.com/rjNemo/rentease/internal/config" "github.com/rjNemo/rentease/internal/config"
"github.com/rjNemo/rentease/internal/constant" "github.com/rjNemo/rentease/internal/constant"
"github.com/rjNemo/rentease/internal/driver/calendar"
"github.com/rjNemo/rentease/internal/view" "github.com/rjNemo/rentease/internal/view"
myTime "github.com/rjNemo/rentease/pkg/time" 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 { type NewItem struct {
Item string `form:"item"` Item string `form:"item"`
PaymentMethod string `form:"method"` 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) newItems := bs.CreateItem(b.Id, itm, ni.Quantity, ni.Price, ni.PaymentMethod, b.CustomerNumber)
if err = cs.Create( // TODO: fix the calendar integration
itm.CalendarId, // if err = cs.Create(
b.Name, // itm.CalendarId,
fmt.Sprintf("Reservation: %s\n %d voyageur(s)\n", b.Name, b.CustomerNumber), // b.Name,
b.From, b.To, // fmt.Sprintf("Reservation: %s\n %d voyageur(s)\n", b.Name, b.CustomerNumber),
); err != nil { // b.From, b.To,
log.Warnf("could not create event: %s", err) // ); err != nil {
captureError(c, err) // log.Warnf("could not create event: %s", err)
} // captureError(c, err)
// }
for _, i := range newItems { for _, i := range newItems {
_ = renderTempl(c, http.StatusCreated, view.LineItem(&view.ItemViewModel{ _ = renderTempl(c, http.StatusCreated, view.LineItem(&view.ItemViewModel{

View file

@ -27,7 +27,7 @@ func (s Server) MountHandlers() {
private.GET("/bookings/:id", handleBookingPage(s.bs, s.hc)) private.GET("/bookings/:id", handleBookingPage(s.bs, s.hc))
private.PUT("/bookings/:id", handleBookingUpdate(s.bs, s.hc)) private.PUT("/bookings/:id", handleBookingUpdate(s.bs, s.hc))
private.PATCH("/bookings/:id/cancel", handleBookingCancel(s.bs)) 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.GET("/bookings/pdf/:id", handlePdfCreateInvoice(s.bs, s.ps, s.hc))
private.POST("/items/:id", handleItemPay(s.bs)) private.POST("/items/:id", handleItemPay(s.bs))
private.PUT("/items/:id", handleItemUpdate(s.bs)) private.PUT("/items/:id", handleItemUpdate(s.bs))

View file

@ -20,7 +20,6 @@ import (
"github.com/rjNemo/rentease/internal/auth" "github.com/rjNemo/rentease/internal/auth"
"github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/booking"
"github.com/rjNemo/rentease/internal/config" "github.com/rjNemo/rentease/internal/config"
"github.com/rjNemo/rentease/internal/driver/calendar"
"github.com/rjNemo/rentease/internal/pdf" "github.com/rjNemo/rentease/internal/pdf"
) )
@ -29,12 +28,11 @@ type Server struct {
bs *booking.Service bs *booking.Service
as *auth.Service as *auth.Service
ps *pdf.PdfService ps *pdf.PdfService
cs *calendar.Service
hc *config.Host hc *config.Host
addr string 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) option := new(options)
for _, opt := range opts { for _, opt := range opts {
err := opt(option) err := opt(option)
@ -48,7 +46,6 @@ func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar
bs: bs, bs: bs,
as: as, as: as,
ps: ps, ps: ps,
cs: cs,
hc: hc, hc: hc,
addr: fmt.Sprintf("0.0.0.0:%d", *option.port), addr: fmt.Sprintf("0.0.0.0:%d", *option.port),
} }

15
main.go
View file

@ -56,8 +56,14 @@ func run(c context.Context, getEnv func(string) string) error {
return fmt.Errorf("error connecting to the database %s", err) 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 // build booking service
bs, err := booking.NewService(db) bs, err := booking.NewService(db, gc)
if err != nil { if err != nil {
return fmt.Errorf("error starting booking service %s", err) 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) 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 // starting server
p := getEnv("PORT") p := getEnv("PORT")
port, err := strconv.Atoi(p) port, err := strconv.Atoi(p)
@ -104,7 +104,6 @@ func run(c context.Context, getEnv func(string) string) error {
bs, bs,
as, as,
ps, ps,
cs,
config.NewHost(), // TODO: move to the database at some point config.NewHost(), // TODO: move to the database at some point
server.WithPort(port), server.WithPort(port),
server.WithFileSystem(static), server.WithFileSystem(static),