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"
"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 {

View file

@ -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
// }

View file

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

View file

@ -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 {

View file

@ -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{

View file

@ -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))

View file

@ -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),
}

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)
}
// 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),