feat: user templates
This commit was merged in pull request #9.
This commit is contained in:
@@ -31,6 +31,7 @@ func (s Service) createInvoice(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
opts := invoice.DefaultOptions
|
||||
opts.Mindur = time.Duration(req.DurationThreshold)
|
||||
opts.CustomTemplate = req.CustomTemplate
|
||||
invoice, report, err := s.invoice.Generate(req.Creditor, &req.Debtor, req.HourlyRate, repos, &opts)
|
||||
if err != nil {
|
||||
s.sendErr(w, http.StatusInternalServerError, "internal server error")
|
||||
@@ -71,6 +72,7 @@ func (s Service) sendInvoice(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
opts := invoice.DefaultOptions
|
||||
opts.Mindur = time.Duration(req.Invoice.DurationThreshold)
|
||||
opts.CustomTemplate = req.Invoice.CustomTemplate
|
||||
invoice, report, err := s.invoice.Generate(req.Invoice.Creditor, &req.Invoice.Debtor, req.Invoice.HourlyRate, repos, &opts)
|
||||
if err != nil {
|
||||
s.sendErr(w, http.StatusInternalServerError, "error while processing invoice:", err)
|
||||
|
||||
@@ -65,14 +65,14 @@ func (s SendReq) ToEMail() email.Mail {
|
||||
|
||||
// MockInvoiceService mocks the invoice.Service interface
|
||||
type MockInvoiceService struct {
|
||||
GenerateFunc func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, *report.Report, error)
|
||||
GenerateFunc func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, report.Report, error)
|
||||
}
|
||||
|
||||
func (m *MockInvoiceService) Generate(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, *report.Report, error) {
|
||||
func (m *MockInvoiceService) Generate(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, report.Report, error) {
|
||||
if m.GenerateFunc != nil {
|
||||
return m.GenerateFunc(creditor, debtor, hourlyRate, repos, opts)
|
||||
}
|
||||
return nil, &report.Report{}, nil
|
||||
return nil, report.Report{}, nil
|
||||
}
|
||||
|
||||
// MockEmailService mocks the email.Service interface
|
||||
@@ -94,7 +94,7 @@ func TestCreateInvoice(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
requestBody InvoiceReq
|
||||
mockGenerate func(creditor model.Entity, 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
|
||||
}{
|
||||
@@ -107,7 +107,7 @@ func TestCreateInvoice(t *testing.T) {
|
||||
HourlyRate: 100,
|
||||
Repos: []string{"owner/repo1"},
|
||||
},
|
||||
mockGenerate: func(creditor model.Entity, 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)
|
||||
@@ -138,8 +138,8 @@ func TestCreateInvoice(t *testing.T) {
|
||||
HourlyRate: 100,
|
||||
Repos: []string{"owner/repo1"},
|
||||
},
|
||||
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")
|
||||
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error) {
|
||||
return nil, report.Report{}, errors.New("failed to generate invoice")
|
||||
},
|
||||
expectedStatus: http.StatusInternalServerError,
|
||||
expectedBody: "internal server error",
|
||||
@@ -153,7 +153,7 @@ func TestCreateInvoice(t *testing.T) {
|
||||
HourlyRate: 100,
|
||||
Repos: []string{"owner/repo1"},
|
||||
},
|
||||
mockGenerate: func(creditor model.Entity, 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)
|
||||
return io.NopCloser(bytes.NewReader([]byte("mock PDF content"))), mockReport, nil
|
||||
@@ -166,7 +166,7 @@ func TestCreateInvoice(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockInvoiceService := &MockInvoiceService{
|
||||
GenerateFunc: func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, *report.Report, error) {
|
||||
GenerateFunc: func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, report.Report, error) {
|
||||
if opts == nil {
|
||||
opts = &invoice.DefaultOptions
|
||||
}
|
||||
@@ -201,7 +201,7 @@ func TestSendInvoice(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
requestBody SendReq
|
||||
mockGenerate func(creditor model.Entity, 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
|
||||
@@ -220,7 +220,7 @@ func TestSendInvoice(t *testing.T) {
|
||||
Repos: []string{"owner/repo1"},
|
||||
},
|
||||
},
|
||||
mockGenerate: func(creditor model.Entity, 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)
|
||||
return io.NopCloser(bytes.NewReader([]byte(pdfContent))), mockReport, nil
|
||||
@@ -245,8 +245,8 @@ func TestSendInvoice(t *testing.T) {
|
||||
Repos: []string{"owner/repo1"},
|
||||
},
|
||||
},
|
||||
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")
|
||||
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error) {
|
||||
return nil, report.Report{}, errors.New("failed to generate invoice for send")
|
||||
},
|
||||
mockSend: nil, // Not called
|
||||
expectedStatus: http.StatusInternalServerError,
|
||||
@@ -266,7 +266,7 @@ func TestSendInvoice(t *testing.T) {
|
||||
Repos: []string{"owner/repo1"},
|
||||
},
|
||||
},
|
||||
mockGenerate: func(creditor model.Entity, 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)
|
||||
return io.NopCloser(bytes.NewReader([]byte(pdfContent))), mockReport, nil
|
||||
@@ -291,7 +291,7 @@ func TestSendInvoice(t *testing.T) {
|
||||
Repos: []string{"owner/repo1"},
|
||||
},
|
||||
},
|
||||
mockGenerate: func(creditor model.Entity, 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) {
|
||||
mockReport := report.New([]issue.Issue{}, model.Entity{}, &model.Entity{}, 0)
|
||||
return io.NopCloser(bytes.NewReader([]byte("mock PDF content"))), mockReport, nil
|
||||
},
|
||||
@@ -303,7 +303,7 @@ func TestSendInvoice(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockInvoiceService := &MockInvoiceService{
|
||||
GenerateFunc: func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, *report.Report, error) {
|
||||
GenerateFunc: func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, report.Report, error) {
|
||||
if opts == nil {
|
||||
opts = &invoice.DefaultOptions
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ type invoiceReq struct {
|
||||
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) {
|
||||
|
||||
@@ -21,7 +21,7 @@ func New(log *slog.Logger, invoice invoicer, mail mailer) *Service {
|
||||
}
|
||||
|
||||
type invoicer interface {
|
||||
Generate(creditor model.Entity, deptor *model.Entity, rate float64, repos []invoice.Repo, opts *invoice.Options) (document io.ReadCloser, report *report.Report, err error)
|
||||
Generate(creditor model.Entity, deptor *model.Entity, rate float64, repos []invoice.Repo, opts *invoice.Options) (document io.ReadCloser, report report.Report, err error)
|
||||
}
|
||||
|
||||
type mailer interface {
|
||||
|
||||
Reference in New Issue
Block a user