You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(billing): invoice jobs see deleted invoices - #1982
The invoice list can now include soft deleted rows (IncludeDeleted on the filter, and a DeletedAt field on the invoice).
The monthly overdraft invoice job finds the last overdraft invoice in one place, and that lookup now counts deleted invoices too.
The invoice sync with the billing provider leaves a locally deleted invoice alone. Before, a deleted local row looked like a missing one and the sync would create it again.
Why
This is the second step of making billing rows soft deleted when an organization is deleted. Once invoices carry deleted_at, two jobs could do damage if they could not see them:
The overdraft job works out its billing window from the last overdraft invoice. If that invoice is soft deleted and the job cannot see it, the window falls back to an older invoice and the customer is billed again for a period that was already paid.
The sync would insert an invoice that still exists on the provider as a new live row.
Nothing writes deleted_at on invoices yet, so there is no behavior change in this PR.
Depends on
This branch is stacked on the migration PR for live-only provider ids.
Release note
Deploy this before the PR that makes the billing deletes soft. If the deletes run first and one fails half way, an overdraft customer can be billed twice.
Tests
Repository: the list returns deleted invoices only when asked.
Overdraft window: a deleted overdraft invoice still moves the window start, and a live one does as before.
Sync: a deleted local invoice is skipped and no new row appears. A live one is updated as before.
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
📝 Summary
Summary by CodeRabbit
Bug Fixes
Deleted invoices are no longer recreated or updated during provider sync.
Deleted overdraft invoices remain available as credit-window anchors, and invoices without credit items are skipped.
Invoice listings can include deleted records while preserving filtering, sorting, and pagination.
Invoice queries can now include soft-deleted records and expose their deletion timestamps. Provider synchronization skips matching deleted invoices, and overdraft generation can use deleted invoices as anchors.
Changes: Soft-deleted billing records
Layer / File(s)
Summary
Deleted invoice query support billing/invoice/invoice.go, internal/store/postgres/billing_invoice_repository.go, internal/store/postgres/billing_invoice_repository_pg_test.go
Invoice exposes DeletedAt, and Filter adds IncludeDeleted. The PostgreSQL repository returns deletion timestamps and includes deleted records when requested. Tests cover returned records and pagination counts.
Invoice processing with deleted records billing/invoice/service.go, billing/invoice/service_test.go
Provider sync skips matching deleted invoices. Overdraft generation selects an invoice with a credit item from all customer invoices and continues to the next customer after lookup errors. Tests cover deleted and live invoice cases.
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
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.
What changes
IncludeDeletedon the filter, and aDeletedAtfield on the invoice).Why
This is the second step of making billing rows soft deleted when an organization is deleted. Once invoices carry
deleted_at, two jobs could do damage if they could not see them:Nothing writes
deleted_aton invoices yet, so there is no behavior change in this PR.Depends on
This branch is stacked on the migration PR for live-only provider ids.
Release note
Deploy this before the PR that makes the billing deletes soft. If the deletes run first and one fails half way, an overdraft customer can be billed twice.
Tests