Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: split lines should not be returned for gathering invoices #2124

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions openmeter/billing/service/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *Service) ListInvoices(ctx context.Context, input billing.ListInvoicesIn
}

if input.Expand.GatheringTotals {
invoices.Items[i], err = s.recalculateGatheringInvoice(ctx, invoices.Items[i])
invoices.Items[i], err = s.recalculateGatheringInvoice(ctx, invoices.Items[i], input.Expand)
if err != nil {
return billing.ListInvoicesResponse{}, fmt.Errorf("error recalculating gathering invoice [%s]: %w", invoices.Items[i].ID, err)
}
Expand Down Expand Up @@ -85,12 +85,14 @@ func (s *Service) resolveStatusDetails(ctx context.Context, invoice billing.Invo
return invoice, nil
}

func (s *Service) recalculateGatheringInvoice(ctx context.Context, invoice billing.Invoice) (billing.Invoice, error) {
func (s *Service) recalculateGatheringInvoice(ctx context.Context, invoice billing.Invoice, expand billing.InvoiceExpand) (billing.Invoice, error) {
if invoice.Status != billing.InvoiceStatusGathering {
return invoice, nil
}

if invoice.Lines.IsAbsent() {
wasLinesAbsent := invoice.Lines.IsAbsent()

if wasLinesAbsent {
// Let's load the lines, if not expanded. This can happen when we are responding to a list request, however
// this at least allows us to not to expand all the invoices.
lines, err := s.adapter.ListInvoiceLines(ctx, billing.ListInvoiceLinesAdapterInput{
Expand Down Expand Up @@ -123,6 +125,26 @@ func (s *Service) recalculateGatheringInvoice(ctx context.Context, invoice billi
return invoice, fmt.Errorf("calculating invoice: %w", err)
}

if wasLinesAbsent {
// If the original user intent was to not to receive the lines, let's not send them
invoice.Lines = billing.LineChildren{}
} else {
// For calulcations we fetch the split lines, but we don't want to expose them for the response
invoice.Lines = billing.NewLineChildren(
lo.Filter(invoice.Lines.OrEmpty(), func(line *billing.Line, _ int) bool {
if !expand.DeletedLines && line.DeletedAt != nil {
return false
}

if !expand.SplitLines && line.Status == billing.InvoiceLineStatusSplit {
return false
}

return true
}),
)
}

return invoice, nil
}

Expand All @@ -143,7 +165,7 @@ func (s *Service) GetInvoiceByID(ctx context.Context, input billing.GetInvoiceBy
}

if input.Expand.GatheringTotals {
invoice, err = s.recalculateGatheringInvoice(ctx, invoice)
invoice, err = s.recalculateGatheringInvoice(ctx, invoice, input.Expand)
if err != nil {
return billing.Invoice{}, fmt.Errorf("error recalculating gathering invoice [%s]: %w", invoice.ID, err)
}
Expand Down
Loading