Skip to content

Commit 9098811

Browse files
authored
feat: list upcoming invoices (#875)
1 parent 78b34c7 commit 9098811

3 files changed

Lines changed: 40 additions & 16 deletions

File tree

internal/cmd/invoice_flag.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
var invoiceType string
6+
7+
func AddInvoiceType(cmd *cobra.Command) {
8+
cmd.Flags().StringVar(&invoiceType, "type", "", "type of the invoice. Possible values: 'all', 'upcoming', 'issued'")
9+
}

internal/cmd/invoice_list.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
func init() {
1111
invoiceCmd.AddCommand(listInvoicesCmd)
12+
AddInvoiceType(listInvoicesCmd)
1213
}
1314

1415
var listInvoicesCmd = &cobra.Command{
@@ -23,7 +24,15 @@ var listInvoicesCmd = &cobra.Command{
2324
return err
2425
}
2526

26-
invoices, err := client.Invoices.List()
27+
if invoiceType == "" {
28+
invoiceType = "issued"
29+
}
30+
31+
if invoiceType != "all" && invoiceType != "upcoming" && invoiceType != "issued" {
32+
return fmt.Errorf("invalid invoice type: %s", invoiceType)
33+
}
34+
35+
invoices, err := client.Invoices.List(invoiceType)
2736
if err != nil {
2837
return err
2938
}
@@ -36,7 +45,7 @@ var listInvoicesCmd = &cobra.Command{
3645
printInvoiceListTable(invoices)
3746
fmt.Println()
3847
fmt.Println()
39-
printInvoiceListPdfs(invoices)
48+
printInvoiceListLinks(invoices)
4049
return nil
4150
},
4251
}
@@ -46,25 +55,29 @@ func printInvoiceListTable(invoices []turso.Invoice) {
4655
printTable(headers, data)
4756
}
4857

49-
func printInvoiceListPdfs(invoices []turso.Invoice) {
50-
headers, data := invoiceListPdfs(invoices)
58+
func printInvoiceListLinks(invoices []turso.Invoice) {
59+
headers, data := invoiceListLinks(invoices)
5160
printTable(headers, data)
5261
}
5362

5463
func invoiceListTable(invoices []turso.Invoice) ([]string, [][]string) {
55-
headers := []string{"ID", "Amount Due", "Due Date", "Paid At", "Payment Failed At"}
64+
headers := []string{"ID", "Amount Due", "Status", "Due Date", "Paid At", "Payment Failed At"}
5665
data := make([][]string, len(invoices))
5766
for i, invoice := range invoices {
58-
data[i] = []string{invoice.Number, invoice.Amount, invoice.DueDate, invoice.PaidAt, invoice.PaymentFailedAt}
67+
data[i] = []string{invoice.Number, invoice.Amount, invoice.Status, invoice.DueDate, invoice.PaidAt, invoice.PaymentFailedAt}
5968
}
6069
return headers, data
6170
}
6271

63-
func invoiceListPdfs(invoices []turso.Invoice) ([]string, [][]string) {
72+
func invoiceListLinks(invoices []turso.Invoice) ([]string, [][]string) {
6473
headers := []string{"ID", "Link"}
6574
data := make([][]string, len(invoices))
6675
for i, invoice := range invoices {
67-
data[i] = []string{invoice.Number, invoice.InvoicePdf}
76+
invoiceLink := invoice.InvoicePdf
77+
if invoice.InvoicePdf == "" {
78+
invoiceLink = invoice.HostedInvoiceUrl
79+
}
80+
data[i] = []string{invoice.Number, invoiceLink}
6881
}
6982
return headers, data
7083
}

internal/turso/invoices.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ import (
88
type InvoicesClient client
99

1010
type Invoice struct {
11-
Number string `json:"invoice_number"`
12-
Amount string `json:"amount_due"`
13-
DueDate string `json:"due_date"`
14-
PaidAt string `json:"paid_at"`
15-
PaymentFailedAt string `json:"payment_failed_at"`
16-
InvoicePdf string `json:"invoice_pdf"`
11+
Number string `json:"invoice_number"`
12+
Amount string `json:"amount_due"`
13+
DueDate string `json:"due_date"`
14+
PaidAt string `json:"paid_at"`
15+
PaymentFailedAt string `json:"payment_failed_at"`
16+
InvoicePdf string `json:"invoice_pdf"`
17+
Status string `json:"status"`
18+
HostedInvoiceUrl string `json:"hosted_invoice_url"`
1719
}
1820

19-
func (i *InvoicesClient) List() ([]Invoice, error) {
20-
r, err := i.client.Get(i.URL(""), nil)
21+
func (i *InvoicesClient) List(invoiceType string) ([]Invoice, error) {
22+
r, err := i.client.Get(i.URL(fmt.Sprintf("?type=%s", invoiceType)), nil)
2123
if err != nil {
2224
return nil, fmt.Errorf("failed to get invoices: %w", err)
2325
}

0 commit comments

Comments
 (0)