mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
parent
6bdbd36869
commit
9a6b6ef0c3
4 changed files with 53 additions and 163 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue