feat(api): fix POST /invoice

This commit is contained in:
2025-08-26 22:40:49 +02:00
parent 788571162d
commit 794558a007
12 changed files with 181 additions and 84 deletions

View File

@@ -5,11 +5,13 @@ import (
"log/slog"
"net/http"
"time"
"git.schreifuchs.ch/lou-taylor/accounting/internal/config"
)
func Start(log *slog.Logger, address string) error {
func Start(log *slog.Logger, address string, cfg *config.Config) error {
mux := http.NewServeMux()
RegisterRoutes(log, mux)
RegisterRoutes(log, mux, cfg)
s := &http.Server{
Addr: address,

View File

@@ -29,7 +29,7 @@ func (s Service) createInvoice(w http.ResponseWriter, r *http.Request) {
return
}
invoice, err := s.invoice.Generate(req.Creditor, req.Debtor, req.DurationThreshold, req.HourlyRate, repos)
invoice, err := s.invoice.Generate(req.Creditor, req.Debtor, time.Duration(req.DurationThreshold), req.HourlyRate, repos)
if err != nil {
log.Println("error while processing invoice:", err)
fmt.Fprint(w, "internal server error")
@@ -63,7 +63,7 @@ func (s Service) sendInvoice(w http.ResponseWriter, r *http.Request) {
s.sendErr(w, 500, err.Error())
return
}
invoice, err := s.invoice.Generate(req.Invoice.Creditor, req.Invoice.Debtor, req.Invoice.DurationThreshold, req.Invoice.HourlyRate, repos)
invoice, err := s.invoice.Generate(req.Invoice.Creditor, req.Invoice.Debtor, time.Duration(req.Invoice.DurationThreshold), req.Invoice.HourlyRate, repos)
if err != nil {
s.sendErr(w, http.StatusInternalServerError, "error while processing invoice:", err)
return

View File

@@ -5,19 +5,19 @@ import (
"log/slog"
"net/http"
"strings"
"time"
"git.schreifuchs.ch/lou-taylor/accounting/internal/email"
"git.schreifuchs.ch/lou-taylor/accounting/internal/jtype"
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice"
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/model"
)
type invoiceReq struct {
Debtor model.Entity `json:"debtor"`
Creditor model.Entity `json:"creditor"`
DurationThreshold time.Duration `json:"durationThreshold"`
HourlyRate float64 `json:"hourlyRate"`
Repos []string `json:"repositories"`
Debtor model.Entity `json:"debtor"`
Creditor model.Entity `json:"creditor"`
DurationThreshold jtype.Duration `json:"durationThreshold"`
HourlyRate float64 `json:"hourlyRate"`
Repos []string `json:"repositories"`
}
func (i invoiceReq) GetRepos() (repos []invoice.Repo, err error) {

View File

@@ -4,6 +4,8 @@ import (
"net/http"
)
// RegisterRoutes registers the HTTP routes for the invoice service.
func (s Service) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /invoice", s.createInvoice)
mux.HandleFunc("POST /invoice/send", s.sendInvoice)
}

View File

@@ -6,26 +6,27 @@ import (
"code.gitea.io/sdk/gitea"
"git.schreifuchs.ch/lou-taylor/accounting/internal/api/httpinvoce"
"git.schreifuchs.ch/lou-taylor/accounting/internal/config"
"git.schreifuchs.ch/lou-taylor/accounting/internal/email"
"git.schreifuchs.ch/lou-taylor/accounting/pdf"
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice"
)
func RegisterRoutes(log *slog.Logger, mux *http.ServeMux) error {
gotenberg, err := pdf.New("http://localhost:3030")
func RegisterRoutes(log *slog.Logger, mux *http.ServeMux, cfg *config.Config) error {
gotenberg, err := pdf.New(cfg.PDF.Hostname)
if err != nil {
panic(err)
}
giteaC, err := gitea.NewClient(
"https://git.schreifuchs.ch",
gitea.SetToken("6a8ea8f9de039b0950c634bfea40c6f97f94b06b"),
cfg.Gitea.URL,
gitea.SetToken(cfg.Gitea.Token),
)
if err != nil {
panic(err)
}
invoicer := invoice.New(log, giteaC, gotenberg)
mailer, err := email.New(email.Config{})
mailer, err := email.New(cfg.Email)
if err != nil {
return err
}