Skip to content

fix(billing): stop duplicate credit overdraft invoices#1498

Merged
rohilsurana merged 1 commit intomainfrom
fix/credit-overdraft-duplicate-invoices
Apr 1, 2026
Merged

fix(billing): stop duplicate credit overdraft invoices#1498
rohilsurana merged 1 commit intomainfrom
fix/credit-overdraft-duplicate-invoices

Conversation

@rohilsurana
Copy link
Copy Markdown
Member

@rohilsurana rohilsurana commented Apr 1, 2026

Summary

Fixes two bugs in GenerateForCredits that caused:

  1. Duplicate invoices for the same customer every run
  2. Other customers never receiving invoices after their first billing period

Bug 1: Duplicate invoices (inner loop break)

The break on finding a credit item only exited the inner items loop. The outer invoices loop (ordered newest-first) kept iterating, overwriting lastOverdraftInvoice with the oldest credit invoice. The alreadyInvoiced check then compared against the wrong invoice and never matched.

Fix: Break out of outer loop once the first (most recent) credit overdraft invoice is found.

Bug 2: Start range never advancing (strict Before comparison)

item.TimeRangeStart.Before(startRange) uses strict comparison — when the previous invoice's start exactly equals c.CreatedAt, startRange was never advanced to the previous invoice's end. This caused:

  • New invoices to use overlapping date ranges
  • CreateInProvider (Stripe) failures for the overlapping range
  • Customers silently stuck after their first credit overdraft invoice

Fix: Use !item.TimeRangeStart.After(startRange) (equivalent to <=) so startRange correctly advances past previously invoiced periods.

Verified with production data

Customer Old Behavior After Fix
a5328c9a 20+ duplicate invoices for same range Correctly skipped (already invoiced)
e2bcf44c Stuck since Sep 2025, range never advanced Will invoice for 2025-09-01 → 2026-04-01 (balance: -800)
847b93bb Stuck since Feb 2026 Correctly skipped (range balance: +5000)
9be297b2 Stuck since Feb 2026 Correctly skipped (no transactions in new range)

Test plan

  • go build ./billing/invoice/ passes
  • Deploy → a5328c9a stops getting duplicates
  • Deploy → e2bcf44c gets a new invoice for Sep 2025 – Apr 2026
  • Customers with positive range balance are correctly skipped

The inner break only exited the items loop, so the outer invoices loop
kept overwriting lastOverdraftInvoice with older invoices. This caused
the alreadyInvoiced check to compare against the oldest credit invoice
instead of the most recent one, never matching the current period and
generating duplicate invoices on every run.

Break out of the outer loop as soon as the first (most recent) credit
overdraft invoice is found.
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
frontier Ready Ready Preview, Comment Apr 1, 2026 6:59am

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 1, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 10a5ac60-0129-44d9-ae8e-28d2dc240b0d

📥 Commits

Reviewing files that changed from the base of the PR and between fd403b0 and f3acaf0.

📒 Files selected for processing (1)
  • billing/invoice/service.go

📝 Walkthrough

Summary by CodeRabbit

  • Refactor
    • Improved invoice generation performance by optimizing how recent invoices are identified and processed during billing calculations.

Walkthrough

In billing/invoice/service.go, the GenerateForCredits function's inner loop now breaks immediately upon finding a lastOverdraftInvoice, rather than continuing to scan all invoices and items. This reduces the scope of invoices considered when selecting the most recent credit-overdraft invoice.

Changes

Cohort / File(s) Summary
Invoice Service Logic
billing/invoice/service.go
Added early loop break in GenerateForCredits when lastOverdraftInvoice is found, optimizing invoice scanning by limiting iteration depth.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 23836203953

Details

  • 0 of 2 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.003%) to 41.187%

Changes Missing Coverage Covered Lines Changed/Added Lines %
billing/invoice/service.go 0 2 0.0%
Totals Coverage Status
Change from base Build 23836031833: -0.003%
Covered Lines: 14839
Relevant Lines: 36028

💛 - Coveralls

@rohilsurana rohilsurana merged commit 8ef1890 into main Apr 1, 2026
8 checks passed
@rohilsurana rohilsurana deleted the fix/credit-overdraft-duplicate-invoices branch April 1, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants