Skip to content

Commit

Permalink
feat: list upcoming invoices (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
gris authored Jun 26, 2024
1 parent 78b34c7 commit 9098811
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
9 changes: 9 additions & 0 deletions internal/cmd/invoice_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cmd

import "github.com/spf13/cobra"

var invoiceType string

func AddInvoiceType(cmd *cobra.Command) {
cmd.Flags().StringVar(&invoiceType, "type", "", "type of the invoice. Possible values: 'all', 'upcoming', 'issued'")
}
29 changes: 21 additions & 8 deletions internal/cmd/invoice_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func init() {
invoiceCmd.AddCommand(listInvoicesCmd)
AddInvoiceType(listInvoicesCmd)
}

var listInvoicesCmd = &cobra.Command{
Expand All @@ -23,7 +24,15 @@ var listInvoicesCmd = &cobra.Command{
return err
}

invoices, err := client.Invoices.List()
if invoiceType == "" {
invoiceType = "issued"
}

if invoiceType != "all" && invoiceType != "upcoming" && invoiceType != "issued" {
return fmt.Errorf("invalid invoice type: %s", invoiceType)
}

invoices, err := client.Invoices.List(invoiceType)
if err != nil {
return err
}
Expand All @@ -36,7 +45,7 @@ var listInvoicesCmd = &cobra.Command{
printInvoiceListTable(invoices)
fmt.Println()
fmt.Println()
printInvoiceListPdfs(invoices)
printInvoiceListLinks(invoices)
return nil
},
}
Expand All @@ -46,25 +55,29 @@ func printInvoiceListTable(invoices []turso.Invoice) {
printTable(headers, data)
}

func printInvoiceListPdfs(invoices []turso.Invoice) {
headers, data := invoiceListPdfs(invoices)
func printInvoiceListLinks(invoices []turso.Invoice) {
headers, data := invoiceListLinks(invoices)
printTable(headers, data)
}

func invoiceListTable(invoices []turso.Invoice) ([]string, [][]string) {
headers := []string{"ID", "Amount Due", "Due Date", "Paid At", "Payment Failed At"}
headers := []string{"ID", "Amount Due", "Status", "Due Date", "Paid At", "Payment Failed At"}
data := make([][]string, len(invoices))
for i, invoice := range invoices {
data[i] = []string{invoice.Number, invoice.Amount, invoice.DueDate, invoice.PaidAt, invoice.PaymentFailedAt}
data[i] = []string{invoice.Number, invoice.Amount, invoice.Status, invoice.DueDate, invoice.PaidAt, invoice.PaymentFailedAt}
}
return headers, data
}

func invoiceListPdfs(invoices []turso.Invoice) ([]string, [][]string) {
func invoiceListLinks(invoices []turso.Invoice) ([]string, [][]string) {
headers := []string{"ID", "Link"}
data := make([][]string, len(invoices))
for i, invoice := range invoices {
data[i] = []string{invoice.Number, invoice.InvoicePdf}
invoiceLink := invoice.InvoicePdf
if invoice.InvoicePdf == "" {
invoiceLink = invoice.HostedInvoiceUrl
}
data[i] = []string{invoice.Number, invoiceLink}
}
return headers, data
}
18 changes: 10 additions & 8 deletions internal/turso/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import (
type InvoicesClient client

type Invoice struct {
Number string `json:"invoice_number"`
Amount string `json:"amount_due"`
DueDate string `json:"due_date"`
PaidAt string `json:"paid_at"`
PaymentFailedAt string `json:"payment_failed_at"`
InvoicePdf string `json:"invoice_pdf"`
Number string `json:"invoice_number"`
Amount string `json:"amount_due"`
DueDate string `json:"due_date"`
PaidAt string `json:"paid_at"`
PaymentFailedAt string `json:"payment_failed_at"`
InvoicePdf string `json:"invoice_pdf"`
Status string `json:"status"`
HostedInvoiceUrl string `json:"hosted_invoice_url"`
}

func (i *InvoicesClient) List() ([]Invoice, error) {
r, err := i.client.Get(i.URL(""), nil)
func (i *InvoicesClient) List(invoiceType string) ([]Invoice, error) {
r, err := i.client.Get(i.URL(fmt.Sprintf("?type=%s", invoiceType)), nil)
if err != nil {
return nil, fmt.Errorf("failed to get invoices: %w", err)
}
Expand Down

0 comments on commit 9098811

Please sign in to comment.