fix(billing): stop duplicate credit overdraft invoices#1498
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughIn Changes
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. Comment |
Pull Request Test Coverage Report for Build 23836203953Details
💛 - Coveralls |
Summary
Fixes two bugs in
GenerateForCreditsthat caused:Bug 1: Duplicate invoices (inner loop break)
The
breakon finding a credit item only exited the inneritemsloop. The outerinvoicesloop (ordered newest-first) kept iterating, overwritinglastOverdraftInvoicewith the oldest credit invoice. ThealreadyInvoicedcheck 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 equalsc.CreatedAt,startRangewas never advanced to the previous invoice's end. This caused:CreateInProvider(Stripe) failures for the overlapping rangeFix: Use
!item.TimeRangeStart.After(startRange)(equivalent to<=) sostartRangecorrectly advances past previously invoiced periods.Verified with production data
a5328c9ae2bcf44c847b93bb9be297b2Test plan
go build ./billing/invoice/passesa5328c9astops getting duplicatese2bcf44cgets a new invoice for Sep 2025 – Apr 2026