feat: cli app

This commit is contained in:
2025-11-04 19:17:50 +01:00
parent 7d160d5f59
commit 8f5ae15ef0
14 changed files with 357 additions and 94 deletions

View File

@@ -28,7 +28,7 @@ func (s Service) createInvoice(w http.ResponseWriter, r *http.Request) {
return
}
invoice, report, 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 {
s.sendErr(w, http.StatusInternalServerError, "internal server error")
return
@@ -65,7 +65,7 @@ func (s Service) sendInvoice(w http.ResponseWriter, r *http.Request) {
s.sendErr(w, 500, err.Error())
return
}
invoice, report, 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 {
s.sendErr(w, http.StatusInternalServerError, "error while processing invoice:", err)
return

View File

@@ -22,11 +22,11 @@ import (
// Exported versions of the request structs for testing
type InvoiceReq struct {
Debtor model.Entity `json:"debtor"`
Creditor model.Entity `json:"creditor"`
DurationThreshold string `json:"durationThreshold"` // Changed to string
HourlyRate float64 `json:"hourlyRate"`
Repos []string `json:"repositories"`
Debtor model.Entity `json:"debtor"`
Creditor model.Entity `json:"creditor"`
DurationThreshold string `json:"durationThreshold"` // Changed to string
HourlyRate float64 `json:"hourlyRate"`
Repos []string `json:"repositories"`
}
func (i InvoiceReq) GetRepos() (repos []invoice.Repo, err error) {
@@ -34,14 +34,14 @@ func (i InvoiceReq) GetRepos() (repos []invoice.Repo, err error) {
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
return repos, err
}
repos = append(repos, invoice.Repo{
Owner: parts[0],
Repo: parts[1],
})
}
return
return repos, err
}
type SendReq struct {
@@ -65,10 +65,10 @@ func (s SendReq) ToEMail() email.Mail {
// MockInvoiceService mocks the invoice.Service interface
type MockInvoiceService struct {
GenerateFunc func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error)
GenerateFunc func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error)
}
func (m *MockInvoiceService) Generate(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
func (m *MockInvoiceService) Generate(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
if m.GenerateFunc != nil {
return m.GenerateFunc(creditor, debtor, durationThreshold, hourlyRate, repos)
}
@@ -94,23 +94,23 @@ func TestCreateInvoice(t *testing.T) {
tests := []struct {
name string
requestBody InvoiceReq
mockGenerate func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error)
mockGenerate func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error)
expectedStatus int
expectedBody string // For error messages or specific content
}{
{
name: "successful invoice creation",
requestBody: InvoiceReq{
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
DurationThreshold: "1h", // Changed to string
HourlyRate: 100,
Repos: []string{"owner/repo1"},
HourlyRate: 100,
Repos: []string{"owner/repo1"},
},
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
pdfContent := "mock PDF content"
// Create a report with a positive total duration
mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, model.Entity{}, 0)
mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, &model.Entity{}, 0)
return io.NopCloser(bytes.NewReader([]byte(pdfContent))), mockReport, nil
},
expectedStatus: http.StatusOK,
@@ -119,26 +119,26 @@ func TestCreateInvoice(t *testing.T) {
{
name: "invalid request body",
requestBody: InvoiceReq{ // Malformed request body
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
DurationThreshold: "invalid", // Changed to string
HourlyRate: 100,
Repos: []string{"owner/repo1"},
HourlyRate: 100,
Repos: []string{"owner/repo1"},
},
mockGenerate: nil, // Not called for invalid body
mockGenerate: nil, // Not called for invalid body
expectedStatus: http.StatusBadRequest,
expectedBody: "cannot read body", // Partial match for error message
},
{
name: "invoice generation error",
requestBody: InvoiceReq{
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
DurationThreshold: "1h", // Changed to string
HourlyRate: 100,
Repos: []string{"owner/repo1"},
HourlyRate: 100,
Repos: []string{"owner/repo1"},
},
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
mockGenerate: func(creditor model.Entity, 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")
},
expectedStatus: http.StatusInternalServerError,
@@ -147,15 +147,15 @@ func TestCreateInvoice(t *testing.T) {
{
name: "no suitable issues to be billed",
requestBody: InvoiceReq{
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
DurationThreshold: "1h", // Changed to string
HourlyRate: 100,
Repos: []string{"owner/repo1"},
HourlyRate: 100,
Repos: []string{"owner/repo1"},
},
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
// Create a report with zero total duration
mockReport := report.New([]issue.Issue{}, model.Entity{}, model.Entity{}, 0)
mockReport := report.New([]issue.Issue{}, model.Entity{}, &model.Entity{}, 0)
return io.NopCloser(bytes.NewReader([]byte("mock PDF content"))), mockReport, nil
},
expectedStatus: http.StatusNotFound,
@@ -196,7 +196,7 @@ func TestSendInvoice(t *testing.T) {
tests := []struct {
name string
requestBody SendReq
mockGenerate func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error)
mockGenerate func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error)
mockSend func(mail email.Mail) error
expectedStatus int
expectedBody string
@@ -208,16 +208,16 @@ func TestSendInvoice(t *testing.T) {
Subject: "Test Invoice",
Body: "Here is your invoice.",
Invoice: InvoiceReq{
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
DurationThreshold: "1h", // Changed to string
HourlyRate: 100,
Repos: []string{"owner/repo1"},
HourlyRate: 100,
Repos: []string{"owner/repo1"},
},
},
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
pdfContent := "mock PDF content for send"
mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, model.Entity{}, 0)
mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, &model.Entity{}, 0)
return io.NopCloser(bytes.NewReader([]byte(pdfContent))), mockReport, nil
},
mockSend: func(mail email.Mail) error {
@@ -233,14 +233,14 @@ func TestSendInvoice(t *testing.T) {
Subject: "Test Invoice",
Body: "Here is your invoice.",
Invoice: InvoiceReq{
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
DurationThreshold: "1h", // Changed to string
HourlyRate: 100,
Repos: []string{"owner/repo1"},
HourlyRate: 100,
Repos: []string{"owner/repo1"},
},
},
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
mockGenerate: func(creditor model.Entity, 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 for send")
},
mockSend: nil, // Not called
@@ -254,16 +254,16 @@ func TestSendInvoice(t *testing.T) {
Subject: "Test Invoice",
Body: "Here is your invoice.",
Invoice: InvoiceReq{
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
DurationThreshold: "1h", // Changed to string
HourlyRate: 100,
Repos: []string{"owner/repo1"},
HourlyRate: 100,
Repos: []string{"owner/repo1"},
},
},
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
pdfContent := "mock PDF content for send"
mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, model.Entity{}, 0)
mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, &model.Entity{}, 0)
return io.NopCloser(bytes.NewReader([]byte(pdfContent))), mockReport, nil
},
mockSend: func(mail email.Mail) error {
@@ -279,15 +279,15 @@ func TestSendInvoice(t *testing.T) {
Subject: "Test Invoice",
Body: "Here is your invoice.",
Invoice: InvoiceReq{
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
Creditor: model.Entity{Name: "Creditor"},
Debtor: model.Entity{Name: "Debtor"},
DurationThreshold: "1h", // Changed to string
HourlyRate: 100,
Repos: []string{"owner/repo1"},
HourlyRate: 100,
Repos: []string{"owner/repo1"},
},
},
mockGenerate: func(creditor, debtor model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
mockReport := report.New([]issue.Issue{}, model.Entity{}, model.Entity{}, 0)
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) {
mockReport := report.New([]issue.Issue{}, model.Entity{}, &model.Entity{}, 0)
return io.NopCloser(bytes.NewReader([]byte("mock PDF content"))), mockReport, nil
},
expectedStatus: http.StatusNotFound,

View File

@@ -22,7 +22,7 @@ func New(log *slog.Logger, invoice invoicer, mail mailer) *Service {
}
type invoicer interface {
Generate(creditor, deptor model.Entity, mindur time.Duration, rate float64, repos []invoice.Repo) (document io.ReadCloser, report *report.Report, err error)
Generate(creditor model.Entity, deptor *model.Entity, mindur time.Duration, rate float64, repos []invoice.Repo) (document io.ReadCloser, report *report.Report, err error)
}
type mailer interface {