feat(api): 404 on no siutable issues

This commit is contained in:
2025-08-27 20:09:13 +02:00
parent 21ca6f0701
commit 75dfeaffd2
3 changed files with 18 additions and 7 deletions

View File

@@ -29,13 +29,19 @@ func (s Service) createInvoice(w http.ResponseWriter, r *http.Request) {
return return
} }
invoice, err := s.invoice.Generate(req.Creditor, req.Debtor, time.Duration(req.DurationThreshold), req.HourlyRate, repos) invoice, report, err := s.invoice.Generate(req.Creditor, req.Debtor, time.Duration(req.DurationThreshold), req.HourlyRate, repos)
if err != nil { if err != nil {
log.Println("error while processing invoice:", err) log.Println("error while processing invoice:", err)
fmt.Fprint(w, "internal server error") fmt.Fprint(w, "internal server error")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return 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")
return
}
w.Header().Set("Content-type", "application/pdf") w.Header().Set("Content-type", "application/pdf")
w.Header().Set( w.Header().Set(
"Content-Disposition", "Content-Disposition",
@@ -51,7 +57,6 @@ func (s Service) createInvoice(w http.ResponseWriter, r *http.Request) {
func (s Service) sendInvoice(w http.ResponseWriter, r *http.Request) { func (s Service) sendInvoice(w http.ResponseWriter, r *http.Request) {
var req sendReq var req sendReq
err := json.NewDecoder(r.Body).Decode(&req) err := json.NewDecoder(r.Body).Decode(&req)
if err != nil { if err != nil {
s.sendErrf(w, http.StatusBadRequest, "cannot read body: %v", err) s.sendErrf(w, http.StatusBadRequest, "cannot read body: %v", err)
@@ -63,11 +68,16 @@ func (s Service) sendInvoice(w http.ResponseWriter, r *http.Request) {
s.sendErr(w, 500, err.Error()) s.sendErr(w, 500, err.Error())
return return
} }
invoice, err := s.invoice.Generate(req.Invoice.Creditor, req.Invoice.Debtor, time.Duration(req.Invoice.DurationThreshold), req.Invoice.HourlyRate, repos) invoice, report, err := s.invoice.Generate(req.Invoice.Creditor, req.Invoice.Debtor, time.Duration(req.Invoice.DurationThreshold), req.Invoice.HourlyRate, repos)
if err != nil { if err != nil {
s.sendErr(w, http.StatusInternalServerError, "error while processing invoice:", err) s.sendErr(w, http.StatusInternalServerError, "error while processing invoice:", err)
return 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")
return
}
invoiceData, err := io.ReadAll(invoice) invoiceData, err := io.ReadAll(invoice)
if err != nil { if err != nil {

View File

@@ -8,6 +8,7 @@ import (
"git.schreifuchs.ch/lou-taylor/accounting/internal/email" "git.schreifuchs.ch/lou-taylor/accounting/internal/email"
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice" "git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice"
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/model" "git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/model"
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/report"
) )
type Service struct { type Service struct {
@@ -21,7 +22,7 @@ func New(log *slog.Logger, invoice invoicer, mail mailer) *Service {
} }
type invoicer interface { type invoicer interface {
Generate(creditor, deptor model.Entity, mindur time.Duration, rate float64, repos []invoice.Repo) (document io.ReadCloser, err error) Generate(creditor, deptor model.Entity, mindur time.Duration, rate float64, repos []invoice.Repo) (document io.ReadCloser, report report.Report, err error)
} }
type mailer interface { type mailer interface {

View File

@@ -10,7 +10,7 @@ import (
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/report" "git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/report"
) )
func (s *Service) Generate(creditor, deptor model.Entity, mindur time.Duration, rate float64, repos []Repo) (document io.ReadCloser, err error) { func (s *Service) Generate(creditor, deptor model.Entity, mindur time.Duration, rate float64, repos []Repo) (document io.ReadCloser, r *report.Report, err error) {
var is []*gitea.Issue var is []*gitea.Issue
for _, repo := range repos { for _, repo := range repos {
iss, _, err := s.gitea.ListRepoIssues( iss, _, err := s.gitea.ListRepoIssues(
@@ -24,7 +24,7 @@ func (s *Service) Generate(creditor, deptor model.Entity, mindur time.Duration,
}, },
) )
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
is = append(is, iss...) is = append(is, iss...)
@@ -37,7 +37,7 @@ func (s *Service) Generate(creditor, deptor model.Entity, mindur time.Duration,
}, },
) )
issues := issue.FromGiteas(is, mindur) issues := issue.FromGiteas(is, mindur)
r := report.New( r = report.New(
issues, issues,
creditor, creditor,
deptor, deptor,