Files
accounting/internal/api/httpinvoce/model.go
schreifuchs 9a5ea229bf
All checks were successful
Go / build (push) Successful in 14s
feat/user-templates (#9)
feat: user templates
2025-12-03 21:27:46 +01:00

71 lines
1.8 KiB
Go

package httpinvoce
import (
"fmt"
"log/slog"
"net/http"
"strings"
"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 jtype.Duration `json:"durationThreshold"`
HourlyRate float64 `json:"hourlyRate"`
Repos []string `json:"repositories"`
CustomTemplate string `json:"template,omitempty"`
}
func (i invoiceReq) GetRepos() (repos []invoice.Repo, err error) {
for i, repo := range i.Repos {
parts := strings.Split(repo, "/")
if len(parts) != 2 {
err = fmt.Errorf("cannot read body: repo with index %d does not split into 2 parts", i)
return repos, err
}
repos = append(repos, invoice.Repo{
Owner: parts[0],
Repo: parts[1],
})
}
return repos, err
}
type sendReq struct {
To []string `json:"to"`
Cc []string `json:"cc"`
Bcc []string `json:"bcc"`
Subject string `json:"subject"`
Body string `json:"body"`
Invoice invoiceReq `json:"invoice"`
}
func (s sendReq) ToEMail() email.Mail {
return email.Mail{
To: s.To,
Cc: s.Cc,
Bcc: s.Bcc,
Subject: s.Subject,
Body: s.Body,
}
}
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.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.WriteHeader(statusCode)
w.Write([]byte(msg))
}