feat(invoice): add send route
This commit is contained in:
114
main.go
114
main.go
@@ -1,106 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"git.schreifuchs.ch/lou-taylor/accounting/issue"
|
||||
"git.schreifuchs.ch/lou-taylor/accounting/mailer"
|
||||
"git.schreifuchs.ch/lou-taylor/accounting/pdf"
|
||||
"git.schreifuchs.ch/lou-taylor/accounting/report"
|
||||
"git.schreifuchs.ch/lou-taylor/accounting/internal/api"
|
||||
)
|
||||
|
||||
type Repo struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg, err := LoadConfig("config.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
client, err := gitea.NewClient(
|
||||
cfg.GiteaURL,
|
||||
gitea.SetToken(cfg.GiteaToken),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var is []*gitea.Issue
|
||||
for _, repo := range cfg.Repos {
|
||||
iss, _, err := client.ListRepoIssues(
|
||||
repo.Owner,
|
||||
repo.Repo,
|
||||
gitea.ListIssueOption{
|
||||
ListOptions: gitea.ListOptions{Page: 0, PageSize: 99999},
|
||||
Since: time.Now().AddDate(0, -1, 0),
|
||||
Before: time.Now(),
|
||||
State: gitea.StateClosed,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
is = append(is, iss...)
|
||||
}
|
||||
|
||||
is = Filter(
|
||||
is,
|
||||
func(i *gitea.Issue) bool {
|
||||
return i.Closed != nil && i.Closed.After(time.Now().AddDate(0, -1, 0))
|
||||
},
|
||||
)
|
||||
issues := issue.FromGiteas(is, time.Duration(cfg.MinDuration))
|
||||
r := report.New(
|
||||
issues,
|
||||
cfg.FromEntity,
|
||||
cfg.ToEntity,
|
||||
cfg.Hourly,
|
||||
)
|
||||
html := r.ToHTML()
|
||||
|
||||
pdfs, err := pdf.New(cfg.PdfGeneratorURL)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
document, err := pdfs.HtmlToPdf(html)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
mlr, err := mailer.New(cfg.Mailer)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
mail := cfg.Mail
|
||||
mail.Attachments = []mailer.Attachment{
|
||||
{
|
||||
Name: "invoice.pdf",
|
||||
MimeType: "pdf",
|
||||
Content: document,
|
||||
},
|
||||
}
|
||||
|
||||
err = mlr.Send(mail)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Filter[T any](slice []T, ok func(T) bool) []T {
|
||||
out := make([]T, 0, len(slice))
|
||||
|
||||
for _, item := range slice {
|
||||
if ok(item) {
|
||||
out = append(out, item)
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
// var cfg invoice.Config
|
||||
// file, err := os.Open("config.json")
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// defer file.Close()
|
||||
// decoder := json.NewDecoder(file)
|
||||
// err = decoder.Decode(&cfg)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
//
|
||||
// invoice.Generate(cfg)
|
||||
api.Start(":8080")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user