From 8de581ae737241416633c2f0841c450d6adf2a03 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 23 Feb 2025 10:50:51 +0100 Subject: [PATCH] fix cronjob --- Dockerfile.dev | 1 + internal/cron/job_report.go | 66 +++++++----- internal/driver/pdf/htmldocs.go | 137 ------------------------ internal/repository/booking/pg_store.go | 12 ++- 4 files changed, 53 insertions(+), 163 deletions(-) delete mode 100644 internal/driver/pdf/htmldocs.go diff --git a/Dockerfile.dev b/Dockerfile.dev index e16243b..58c9886 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -4,6 +4,7 @@ WORKDIR /app RUN apk update \ && apk add --no-cache build-base \ + && apk add --no-cache golangci-lint \ && go install github.com/air-verse/air@latest \ && go install github.com/a-h/templ/cmd/templ@latest diff --git a/internal/cron/job_report.go b/internal/cron/job_report.go index 1ca8de5..f3730e1 100644 --- a/internal/cron/job_report.go +++ b/internal/cron/job_report.go @@ -1,32 +1,48 @@ package cron -// 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 %w", err) -// } +import ( + "fmt" + "log" + "os" + "time" -// now := time.Now() -// log.Println("Start Monthly Booking Report job at:", now) + "github.com/joho/godotenv" + "github.com/rjNemo/rentease/internal/driver/pdf" + "github.com/rjNemo/rentease/internal/repository/booking" + bookingService "github.com/rjNemo/rentease/internal/service/booking" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) -// ps, err := pdf.NewPdfClient( -// 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 %w", 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 %w", err) + } -// service, _ := booking.NewService(db, nil, ps) -// period := "monthly" -// month := int(now.Month()) -// year := now.Year() + now := time.Now() + log.Println("Start Monthly Booking Report job at:", now) + ps, err := pdf.NewPdfClient() + if err != nil { + return fmt.Errorf("error starting pdf service %w", err) + } -// err = service.BuildReport(service.Report(period, month, year), period, month, year) + store := booking.NewPgStore(db) + service, err := bookingService.NewService(store, nil, nil, ps) + if err != nil { + return fmt.Errorf("error creating booking service: %w", err) + } + period := "monthly" + month := int(now.Month()) + year := now.Year() -// log.Printf("Executed Monthly Booking Report job at %v with errors: %s:", time.Now().Format(time.DateTime), err) -// return err -// } + _, err = service.BuildReport(service.Report(period, month, year), period, month, year) + if err != nil { + log.Printf("Executed Monthly Booking Report job at %v with error: %v", time.Now().Format(time.DateTime), err) + return fmt.Errorf("error building report: %w", err) + } + + log.Printf("Executed Monthly Booking Report job successfully at %v", time.Now().Format(time.DateTime)) + return nil +} diff --git a/internal/driver/pdf/htmldocs.go b/internal/driver/pdf/htmldocs.go deleted file mode 100644 index ce0e4ed..0000000 --- a/internal/driver/pdf/htmldocs.go +++ /dev/null @@ -1,137 +0,0 @@ -package pdf - -import ( - "bytes" - "encoding/json" - "errors" - "io" - "net/http" - "os" - - "github.com/labstack/gommon/log" -) - -type PdfClient struct { - path string - invoiceId string - reportId string - url string - apiKey string -} - -func NewApiPdfClient(pid, rid, url, key string) (*PdfClient, error) { - if pid == "" || rid == "" || url == "" || key == "" { - return nil, errors.New("error building Pdf service. Verify your env variables") - } - return &PdfClient{ - path: "index.html", - invoiceId: pid, - reportId: rid, - url: url, - apiKey: key, - }, nil -} - -func (ps PdfClient) BuildInvoice(context map[string]any) error { - data := struct { - Context map[string]any `json:"context"` - Path string `json:"path"` - ProjectId string `json:"projectId"` - }{ - Context: context, - Path: ps.path, - ProjectId: ps.invoiceId, - } - - payload, err := json.Marshal(data) - if err != nil { - log.Warnf("Error marshalling JSON: %s", err) - return err - } - - return ps.sendData(payload) -} - -func (ps PdfClient) BuildReport(context map[string]any, period string, month, year int) error { - data := struct { - Context map[string]any `json:"context"` - Path string `json:"path"` - ProjectId string `json:"projectId"` - }{ - Context: context, - Path: ps.path, - ProjectId: ps.reportId, - } - - payload, err := json.Marshal(data) - if err != nil { - log.Warnf("Error marshalling JSON: %s", err) - return err - } - - return ps.sendData(payload) -} - -func (ps PdfClient) sendData(payload []byte) error { - req, err := http.NewRequest("POST", ps.url, bytes.NewBuffer(payload)) - if err != nil { - log.Warnf("Error creating request: %s", err) - return err - } - - req.Header.Set("Authorization", "Bearer "+ps.apiKey) - req.Header.Set("Content-Type", "application/json") - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - log.Warnf("Error sending request: %s", err) - return err - } - defer resp.Body.Close() - - res := new(struct { - Url string `json:"url"` - Error string `json:"error"` - }) - body, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - - err = json.Unmarshal(body, res) - if err != nil { - log.Warnf("error decoding response: %s", err) - return err - } - - if res.Error != "" { - log.Warnf("error building pdf file %s", err) - return errors.New(res.Error) - } - - resp, err = http.Get(res.Url) - if err != nil { - log.Warnf("Error retrieving file") - return err - } - defer resp.Body.Close() - - file, err := os.Create("tmp.pdf") - if err != nil { - log.Fatal(err) - } - defer file.Close() - - body, err = io.ReadAll(resp.Body) - if err != nil { - return err - } - - _, err = file.Write(body) - if err != nil { - log.Error("Error copying file content") - return err - } - return nil -} diff --git a/internal/repository/booking/pg_store.go b/internal/repository/booking/pg_store.go index 34cd746..2ee35bb 100644 --- a/internal/repository/booking/pg_store.go +++ b/internal/repository/booking/pg_store.go @@ -91,7 +91,17 @@ func (ps *PgStore) Create(b *booking.Booking) error { } func (ps *PgStore) Update(b *booking.Booking) error { - return ps.db.Save(b).Error + return ps.db.Model(&booking.Booking{}).Where("id = ?", b.Id).Updates(map[string]any{ + "from": b.From, + "to": b.To, + "customer_name": b.Name, + "phone_number": b.PhoneNumber, + "email": b.Email, + "platform": b.Platform, + "external_id": b.ExternalId, + "customers": b.CustomerNumber, + "platform_fees": b.PlatformFees, + }).Error } func (ps *PgStore) Cancel(id int) error {