Skip to content

fix(billing): keep the synced invoice amount up to date - #1906

Merged
whoAbhishekSah merged 1 commit into
mainfrom
fix/invoice-sync-amount
Aug 28, 2026
Merged

fix(billing): keep the synced invoice amount up to date#1906
whoAbhishekSah merged 1 commit into
mainfrom
fix/invoice-sync-amount

Conversation

@whoAbhishekSah

@whoAbhishekSah whoAbhishekSah commented Aug 28, 2026

Copy link
Copy Markdown
Member

Found through review on #1881 (#1881 (comment)): the invoice sync's update path only ever wrote state, effective_at, and hosted_url on an existing local row. The amount was frozen at whatever the first sync saw.

Concrete failure: Stripe creates next month's invoice as an empty draft, our sync stores it with amount 0, Stripe fills it with usage to a real total — and the local copy says 0 forever. Everything reading local rows inherits the lie: billing pages show stale totals, and CheckOrganizationDelete skips the invoice (its non-zero filter sees 0), so the delete button promises a delete the server then refuses against live provider data.

The fix is the sync writing the total when it changes, plus the repository update actually including the amount column. Zero is a real total (a draft whose items were credited away), so it is written unconditionally rather than behind a not-set guard; both UpdateByID callers pass full invoice rows.

What this deliberately does not solve: an open invoice fully covered by the customer's credit balance still has a positive total with nothing due, so the delete check can grey the button until the provider marks it paid (minutes, self-clearing, and in the safe direction). Storing amount_remaining locally would close that too — filed separately as a follow-up.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Aug 28, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
frontier Ready Ready Preview Aug 28, 2026 8:00am

@coderabbitai

coderabbitai Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

Next included review available in 47 minutes.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 6ad40c6f-a0fd-4ecf-bd22-054e42fd7fcd

📥 Commits

Reviewing files that changed from the base of the PR and between f6974a6 and ddab84e.

📒 Files selected for processing (2)
  • billing/invoice/service.go
  • internal/store/postgres/billing_invoice_repository.go

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 26c2c538-8e3d-47fc-a097-59f42425c24b

📥 Commits

Reviewing files that changed from the base of the PR and between c453bec and f6974a6.

📒 Files selected for processing (2)
  • billing/invoice/service.go
  • internal/store/postgres/billing_invoice_repository.go

Included review availability: Your plan provides up to 2 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Invoice synchronization now reflects provider total changes, including updates to zero.
    • Correctly saves invoices with zero-value totals, such as fully credited drafts.
    • Ensures updated invoice amounts persist reliably.
    • Prevents outdated invoice amounts from remaining after provider-side changes.
    • Improves billing accuracy for invoices whose items are fully credited or otherwise reduced to zero.

Walkthrough

Invoice synchronization now detects provider total changes, including changes to zero. The invoice repository writes the invoice amount during updates.

Changes

Invoice synchronization

Layer / File(s) Summary
Persist changed invoice totals
billing/invoice/service.go, internal/store/postgres/billing_invoice_repository.go
upsert persists changed provider totals, including zero. UpdateByID writes toUpdate.Amount to the amount column.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: 🔵 Low · up to f6974

The PR updates stored invoice totals, including valid zero amounts, fixing stale billing data. If synchronization and reconciliation overlap, an older provider snapshot could temporarily overwrite a newer total in local billing and deletion-preflight views, while provider-side deletion checks remain authoritative. The change is mergeable with owner awareness or follow-up on update ordering.

Suggested reviewers: amangit07

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

@coveralls

coveralls commented Aug 28, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 33153602246

Coverage decreased (-0.007%) to 49.161%

Details

  • Coverage decreased (-0.007%) from the base build.
  • Patch coverage: 5 uncovered changes across 2 files (0 of 5 lines covered, 0.0%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
billing/invoice/service.go 4 0 0.0%
internal/store/postgres/billing_invoice_repository.go 1 0 0.0%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 40516
Covered Lines: 19918
Line Coverage: 49.16%
Coverage Strength: 15.74 hits per line

💛 - Coveralls

The invoice sync only ever updated the state, effective date, and
hosted URL of an existing local row — the amount stayed frozen at
whatever the first sync saw. A draft that Stripe created empty and
filled with usage later kept amount 0 locally forever, so everything
reading the local rows saw wrong totals: billing pages showed stale
numbers, and the org delete check skipped a real unpaid invoice and
enabled a delete the server then refused.

The sync now writes the total when it changes, and the repository
update includes the amount column. Zero is a real value (a draft whose
items were credited away), so it is written like any other total.
@whoAbhishekSah
whoAbhishekSah merged commit 2e9870e into main Aug 28, 2026
8 checks passed
@whoAbhishekSah
whoAbhishekSah deleted the fix/invoice-sync-amount branch August 28, 2026 08:14
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