rentease/main.go
Ruidy cf1620592a
create invoice (#39)
### TL;DR

Enhanced invoice generation with improved formatting and Euro symbol display

### What changed?

- Added Euro symbol (€) to monetary values in the invoice template
- Implemented new invoice data structure with dedicated types for lines and payments
- Created ToInvoice method to properly format booking data for invoice generation
- Added HTML template parsing and rendering functionality
- Improved date formatting for consistency
- Added new API endpoint for booking creation

### How to test?

1. Create a new booking through the API
2. Navigate to the PDF generation endpoint
3. Verify that monetary values display with Euro symbol
4. Check that dates are properly formatted
5. Confirm that payment history and totals are correctly calculated
6. Validate that the generated HTML maintains proper formatting

### Why make this change?

To improve invoice readability and consistency by standardizing monetary value display and providing better data structure for invoice generation. This change also makes the system more maintainable by separating concerns between data transformation and presentation.
2025-02-04 11:34:14 +01:00

116 lines
2.8 KiB
Go

package main
import (
"context"
"embed"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"github.com/getsentry/sentry-go"
"github.com/rjNemo/rentease/internal/config"
"github.com/rjNemo/rentease/internal/driver/calendar"
"github.com/rjNemo/rentease/internal/driver/database"
"github.com/rjNemo/rentease/internal/driver/parser"
"github.com/rjNemo/rentease/internal/driver/pdf"
bookingRepo "github.com/rjNemo/rentease/internal/repository/booking"
"github.com/rjNemo/rentease/internal/server"
"github.com/rjNemo/rentease/internal/service/auth"
"github.com/rjNemo/rentease/internal/service/booking"
)
//go:embed assets
var static embed.FS
func main() {
ctx := context.Background()
if err := run(ctx, config.NewConfig()); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(c context.Context, getEnv func(string) string) error {
ctx, cancel := signal.NotifyContext(c, os.Interrupt)
defer cancel()
// init sentry
if err := sentry.Init(sentry.ClientOptions{
Dsn: getEnv("SENTRY_DSN"),
EnableTracing: true,
TracesSampleRate: 1.0,
}); err != nil {
return fmt.Errorf("error initializing sentry %w", err)
}
// init database
db, err := database.New(getEnv("DATABASE_URL"))
if err != nil {
return fmt.Errorf("error connecting to the database %w", err)
}
if err = database.Migrate(db, &booking.Booking{}, &booking.Item{}, &booking.Payment{}); err != nil {
return fmt.Errorf("error migrating the database %w", err)
}
bookingStore := bookingRepo.NewPgStore(db)
gc, err := calendar.NewGoogleClient(ctx, getEnv("CALENDAR_CREDENTIALS"))
if err != nil {
return fmt.Errorf("error building calendar client %w", err)
}
// build pdf client
pc, err := pdf.NewPdfClient()
if err != nil {
return fmt.Errorf("error starting pdf client %w", err)
}
parsingClient := parser.NewBookingAgentParser(getEnv("PARSER_BASE_URL"))
bookingService, err := booking.NewService(bookingStore, parsingClient, gc, pc)
if err != nil {
return fmt.Errorf("error creating booking service: %w", err)
}
// build authentication service
as, err := auth.NewService(
getEnv("SESSION_SECRET"),
getEnv("ADMIN"),
getEnv("ADMIN_SECRET"),
getEnv("API_KEY"),
)
if err != nil {
return fmt.Errorf("error starting auth service %w", err)
}
p := getEnv("PORT")
port, err := strconv.Atoi(p)
if err != nil {
return fmt.Errorf("error parsing PORT env %w", err)
}
ogs := getEnv("ORIGINS")
origins := strings.Split(ogs, ",")
srv, err := server.New(
bookingService,
as,
config.NewHost(), // TODO: move to the database at some point
server.WithPort(port),
server.WithFileSystem(static),
server.WithDebug(strings.ToLower(getEnv("DEBUG")) == "true"),
server.WithSecretKey(getEnv("SECRET_KEY")),
server.WithOrigins(origins),
)
if err != nil {
return fmt.Errorf("error starting server %w", err)
}
srv.Start(ctx)
return nil
}