Files
descrybe/apps/api/internal/httpapi/admin_invoice_export_handlers.go
T

39 lines
1.5 KiB
Go
Raw Normal View History

2026-10-16 00:34:06 +02:00
package httpapi
import (
"net/http"
"strconv"
"strings"
"github.com/google/uuid"
)
// Platform-admin export of actual Stripe invoice amounts (not estimated usage).
// Scope must be a single company; customer IDs are resolved server-side.
func (s *Server) handleAdminExportCompanyInvoices(w http.ResponseWriter, r *http.Request) {
id, err := uuid.Parse(strings.TrimSpace(r.URL.Query().Get("company_id")))
if err != nil {
Error(w, http.StatusBadRequest, "company_id is required")
return
}
rows, err := s.stripeSvc().ListCompanyInvoices(r.Context(), id)
if err != nil {
LogAndError(w, http.StatusBadGateway, "could not export Stripe invoices", err)
return
}
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Header().Set("Content-Disposition", `attachment; filename="descrybe-stripe-invoices.csv"`)
w.Header().Set("Cache-Control", "private, no-store")
2026-10-20 17:03:09 +02:00
out, err := newUTF8CSVWriter(w)
if err != nil {
return
}
2026-10-16 00:34:06 +02:00
_ = out.Write([]string{"company_id", "invoice_id", "number", "status", "currency", "created_unix", "amount_due_minor", "amount_paid_minor", "amount_remaining_minor", "hosted_invoice_url", "invoice_pdf"})
for _, row := range rows {
_ = out.Write([]string{id.String(), row.ID, row.Number, row.Status, row.Currency,
strconv.FormatInt(row.Created, 10), strconv.FormatInt(row.AmountDue, 10),
strconv.FormatInt(row.AmountPaid, 10), strconv.FormatInt(row.AmountRemaining, 10),
row.HostedInvoiceURL, row.InvoicePDF})
}
out.Flush()
}