Skip to content

fix(billing): exclude overdraft credits from credit overdraft balance#1502

Merged
rohilsurana merged 2 commits intomainfrom
fix/credit-overdraft-start-range
Apr 7, 2026
Merged

fix(billing): exclude overdraft credits from credit overdraft balance#1502
rohilsurana merged 2 commits intomainfrom
fix/credit-overdraft-start-range

Conversation

@rohilsurana
Copy link
Copy Markdown
Member

@rohilsurana rohilsurana commented Apr 1, 2026

Follows up on #1498.

When a customer pays an overdraft invoice, reconciliation adds a system.overdraft credit transaction. GenerateForCredits uses GetBalanceForRange to check if a customer has negative balance — but the reconciliation credit inflates the balance, masking new usage that should be billed.

Changes

  • Add GetBalanceForRangeWithoutOverdraft to the transaction repository, credit service, and invoice service — excludes system.overdraft credits from the balance sum
  • Switch GenerateForCredits to call GetBalanceForRangeWithoutOverdraft

@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 7, 2026 7:07am

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 1, 2026

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Invoice generation now uses corrected account balance calculations that exclude system overdraft transactions when evaluating balances over date ranges, improving overdraft detection, invoice line quantities, and resulting invoice creation so billed amounts reflect true account activity.

Walkthrough

Added a balance query that excludes overdraft-source credits and wired it into invoice generation; extended credit transaction interfaces and mocks; implemented a Postgres repository method that sums debits and credits while excluding overdraft-source credits for a time range.

Changes

Cohort / File(s) Summary
Invoice service
billing/invoice/service.go
GenerateForCredits now calls GetBalanceForRangeWithoutOverdraft and uses its returned balance for overdraft detection, invoice line quantity, and invoice create/upsert flow.
Credit service & mocks
billing/credit/service.go, billing/credit/mocks/transaction_repository.go
Added GetBalanceForRangeWithoutOverdraft(ctx context.Context, accountID string, start time.Time, end time.Time) (int64, error) to TransactionRepository and Service; added corresponding mock method, expectation type, and call helpers (Run, Return, RunAndReturn).
Postgres repository implementation
internal/store/postgres/billing_transactions_repository.go
Implemented GetBalanceForRangeWithoutOverdraft and helper getCreditBalanceExcludingSource: runs in a serializable tx, sums debits and credits while excluding credit.SourceSystemOverdraftEvent, normalizes NULL sums to zero, and returns excludedCredits - debits.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • rsbh

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 09eb074a-1454-4a49-adb4-6b803cb518de

📥 Commits

Reviewing files that changed from the base of the PR and between 3660325 and 19dd037.

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

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 1, 2026

Coverage Report for CI Build 24068983263

Coverage decreased (-0.06%) to 41.147%

Details

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

Uncovered Changes

File Changed Covered %
internal/store/postgres/billing_transactions_repository.go 51 0 0.0%
billing/credit/service.go 3 0 0.0%
billing/invoice/service.go 1 0 0.0%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 36289
Covered Lines: 14932
Line Coverage: 41.15%
Coverage Strength: 11.89 hits per line

💛 - Coveralls

@rohilsurana rohilsurana changed the title fix(billing): advance credit overdraft invoice start range past previous period fix(billing): exclude overdraft credits from credit overdraft balance Apr 6, 2026
@rohilsurana rohilsurana force-pushed the fix/credit-overdraft-start-range branch from 1af73cd to b64c958 Compare April 6, 2026 18:45
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
internal/store/postgres/billing_transactions_repository.go (2)

534-536: Mark the transaction as read-only for this balance query.

This method only reads; setting ReadOnly: true improves intent and can reduce lock pressure.

♻️ Proposed change
 	if err := r.dbc.WithTxn(ctx, sql.TxOptions{
 		Isolation: sql.LevelSerializable,
+		ReadOnly:  true,
 	}, func(tx *sqlx.Tx) error {

500-503: Consider an index strategy for the new filtered credit-sum path.

This path is used for invoice generation; add/verify a supporting index for (account_id, type, created_at, source) (or an equivalent partial index) to avoid full scans as transaction volume grows.

Also applies to: 531-557


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a0d48227-bc12-483b-b696-3d57350eaf9a

📥 Commits

Reviewing files that changed from the base of the PR and between 19dd037 and b64c958.

📒 Files selected for processing (4)
  • billing/credit/mocks/transaction_repository.go
  • billing/credit/service.go
  • billing/invoice/service.go
  • internal/store/postgres/billing_transactions_repository.go
✅ Files skipped from review due to trivial changes (1)
  • billing/credit/mocks/transaction_repository.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • billing/invoice/service.go

Before() is strict — when the previous invoice's start matches the
customer's created_at exactly, startRange was never advanced past
the previous invoice's end, preventing new invoices from being
created for subsequent periods.
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 57cb352f-5ea5-4cb4-b5a8-5128536835be

📥 Commits

Reviewing files that changed from the base of the PR and between b64c958 and 6a4e978.

📒 Files selected for processing (4)
  • billing/credit/mocks/transaction_repository.go
  • billing/credit/service.go
  • billing/invoice/service.go
  • internal/store/postgres/billing_transactions_repository.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • billing/invoice/service.go
  • billing/credit/mocks/transaction_repository.go

@rohilsurana rohilsurana merged commit 4e7b2be into main Apr 7, 2026
10 of 11 checks passed
@rohilsurana rohilsurana deleted the fix/credit-overdraft-start-range branch April 7, 2026 07:20
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