diff --git a/internal/api/httpinvoce/controller.go b/internal/api/httpinvoce/controller.go index 93cbdc4..6e9f850 100644 --- a/internal/api/httpinvoce/controller.go +++ b/internal/api/httpinvoce/controller.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "log" "net/http" "time" @@ -19,26 +18,24 @@ func (s Service) createInvoice(w http.ResponseWriter, r *http.Request) { err := json.NewDecoder(r.Body).Decode(&req) if err != nil { - s.sendErrf(w, http.StatusBadRequest, "cannot read body: %v", err) + s.sendErr(w, http.StatusBadRequest, "cannot read body") return } repos, err := req.GetRepos() if err != nil { - s.sendErr(w, 500, err.Error()) + s.sendErr(w, http.StatusInternalServerError, "cannot get repos") return } invoice, report, 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") - w.WriteHeader(http.StatusInternalServerError) + s.sendErr(w, http.StatusInternalServerError, "internal server error") return } // if no time has to be billed aka if bill for 0 CHF if report.Total() <= time.Duration(0) { - s.sendErr(w, http.StatusNotFound, "no suitable issues to be billed") + s.sendErrf(w, http.StatusNotFound, "no suitable issues to be billed") return } diff --git a/internal/api/httpinvoce/controller_test.go b/internal/api/httpinvoce/controller_test.go index 97e6321..9cb6168 100644 --- a/internal/api/httpinvoce/controller_test.go +++ b/internal/api/httpinvoce/controller_test.go @@ -14,7 +14,6 @@ import ( "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/issue" "git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/model" @@ -106,7 +105,7 @@ func TestCreateInvoice(t *testing.T) { Debtor: model.Entity{Name: "Debtor"}, DurationThreshold: "1h", // Changed to string HourlyRate: 100, - Repos: []string{"repo1"}, + Repos: []string{"owner/repo1"}, }, mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { pdfContent := "mock PDF content" @@ -124,7 +123,7 @@ func TestCreateInvoice(t *testing.T) { Debtor: model.Entity{Name: "Debtor"}, DurationThreshold: "invalid", // Changed to string HourlyRate: 100, - Repos: []string{"repo1"}, + Repos: []string{"owner/repo1"}, }, mockGenerate: nil, // Not called for invalid body expectedStatus: http.StatusBadRequest, @@ -137,7 +136,7 @@ func TestCreateInvoice(t *testing.T) { Debtor: model.Entity{Name: "Debtor"}, DurationThreshold: "1h", // Changed to string HourlyRate: 100, - Repos: []string{"repo1"}, + Repos: []string{"owner/repo1"}, }, mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { return nil, nil, errors.New("failed to generate invoice") @@ -152,7 +151,7 @@ func TestCreateInvoice(t *testing.T) { Debtor: model.Entity{Name: "Debtor"}, DurationThreshold: "1h", // Changed to string HourlyRate: 100, - Repos: []string{"repo1"}, + Repos: []string{"owner/repo1"}, }, mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { // Create a report with zero total duration @@ -213,7 +212,7 @@ func TestSendInvoice(t *testing.T) { Debtor: model.Entity{Name: "Debtor"}, DurationThreshold: "1h", // Changed to string HourlyRate: 100, - Repos: []string{"repo1"}, + Repos: []string{"owner/repo1"}, }, }, mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { @@ -238,7 +237,7 @@ func TestSendInvoice(t *testing.T) { Debtor: model.Entity{Name: "Debtor"}, DurationThreshold: "1h", // Changed to string HourlyRate: 100, - Repos: []string{"repo1"}, + Repos: []string{"owner/repo1"}, }, }, mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { @@ -259,7 +258,7 @@ func TestSendInvoice(t *testing.T) { Debtor: model.Entity{Name: "Debtor"}, DurationThreshold: "1h", // Changed to string HourlyRate: 100, - Repos: []string{"repo1"}, + Repos: []string{"owner/repo1"}, }, }, mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { @@ -284,7 +283,7 @@ func TestSendInvoice(t *testing.T) { Debtor: model.Entity{Name: "Debtor"}, DurationThreshold: "1h", // Changed to string HourlyRate: 100, - Repos: []string{"repo1"}, + Repos: []string{"owner/repo1"}, }, }, mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { diff --git a/internal/api/httpinvoce/model.go b/internal/api/httpinvoce/model.go index bb98e29..c891ef6 100644 --- a/internal/api/httpinvoce/model.go +++ b/internal/api/httpinvoce/model.go @@ -57,13 +57,13 @@ func (s sendReq) ToEMail() email.Mail { func (s *Service) sendErrf(w http.ResponseWriter, statusCode int, format string, a ...any) { msg := fmt.Sprintf(format, a...) s.log.Error(msg, slog.Any("statusCode", statusCode)) - w.Write([]byte(msg)) w.WriteHeader(statusCode) + w.Write([]byte(msg)) } func (s *Service) sendErr(w http.ResponseWriter, statusCode int, a ...any) { msg := fmt.Sprint(a...) s.log.Error(msg, slog.Any("statusCode", statusCode)) - w.Write([]byte(msg)) w.WriteHeader(statusCode) + w.Write([]byte(msg)) }