fix(billing): advance credit overdraft invoice window past last invoiced range#1730
Open
rohilsurana wants to merge 1 commit into
Open
fix(billing): advance credit overdraft invoice window past last invoiced range#1730rohilsurana wants to merge 1 commit into
rohilsurana wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
|
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 (2)
👮 Files not reviewed due to content moderation or server errors (2)
📝 Walkthrough
🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
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 |
Coverage Report for CI Build 28587052787Coverage increased (+0.03%) to 44.865%Details
Uncovered Changes
Coverage Regressions1 previously-covered line in 1 file lost coverage.
Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
GenerateForCreditscomputes the invoice window start from the last credit overdraft invoice with this condition:It compares the anchor item's start against the customer's creation time. That is only true for an invoice whose window began at creation — and even then only because providers store item ranges in whole seconds, truncating the creation timestamp's sub-second part. Any later invoice starts after creation, the condition is false, and the window snaps back to the customer's creation time.
Since #1502 excluded overdraft payment credits from the range balance, a snapped-back window counts all previously invoiced usage again. Combined with #1498 (anchor = newest invoice), the result alternates: one tick produces the correct invoice, the next tick anchors on it, snaps back, and bills the customer's entire history as a duplicate.
Observed in a deployment: a customer expecting one $386 invoice on Jul 1 received $386 (correct window) at 00:02 and $2,756 (= all usage since account creation, including two already-paid invoices) at 00:07, one sync tick later.
Two more defects in the same block:
alreadyInvoicedequality check compares againststartRangeafter the same item mutated it, so it can never match a single-item invoice; same-day dedup only worked by accident via the empty-window balance.item.TimeRangeStart.Before(...)is dereferenced with onlyTimeRangeEndnil-checked — an item with a nullrange_startpanics the whole sync tick.Fix
Extract the window computation into
computeOverdraftWindowand base it solely onTimeRangeEnd:TimeRangeEndwhenever that end is after the current start,alreadyInvoicedwhen the anchor's end already reaches the current range end,TimeRangeEndinstead of dereferencing a possibly-nilTimeRangeStart.Tests
Table-driven tests for
computeOverdraftWindowcovering: no prior invoice, first-invoice anchoring (both truncated and whole-second creation times), later-invoice anchoring (the snap-back reproduction), same-day rerun dedup, and nil range start/end. The snap-back, whole-second, same-day, and nil-start cases fail on the previous logic (the nil-start case panicked).