Repository navigation
Keep fee quotes tied to the filing they priced #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
18 changes: 18 additions & 0 deletions
18
efile_app/efile/migrations/0026_filingdraft_quoted_fee_fingerprint.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Generated by Django 5.2.5 on 2026-09-28 23:37 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('efile', '0025_pending_activation'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='filingdraft', | ||
| name='quoted_fee_fingerprint', | ||
| field=models.CharField(blank=True, max_length=64), | ||
| ), | ||
| ] |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| """Whether the fee quote on a draft still describes the filing it would price. | ||
|
|
||
| A quote is asked of the EFSP on Payment and shown again on Review. Anything | ||
| the filer changes afterwards -- the court, the case type, a document's filing | ||
| type, an optional service -- can change what the court charges, and it can be | ||
| changed from any of half a dozen screens. Rather than have each of them | ||
| remember to clear the quote, the quote carries a fingerprint of everything it | ||
| was priced on, and is only ever treated as current while the draft still | ||
| matches it. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import hashlib | ||
| import json | ||
| from decimal import Decimal, InvalidOperation | ||
| from typing import Any | ||
|
|
||
| from efile.models import FilingDocument, FilingDraft, FilingParty | ||
|
|
||
| WAIVER_ACCOUNT_TYPE = "WV" | ||
|
|
||
|
|
||
| class FeeQuoteState: | ||
| CURRENT = "current" | ||
| STALE = "stale" | ||
| MISSING = "missing" | ||
| WAIVED = "waived" | ||
|
|
||
|
|
||
| def fee_inputs(draft: FilingDraft, *, payment_account_id: str | None = None) -> dict[str, Any]: | ||
| """Everything on the draft that the EFSP's fee calculation reads. | ||
|
|
||
| Deliberately generous: a field that turns out not to affect the fee costs | ||
| one extra fee request after it changes, while one left out lets a stale | ||
| total through. `payment_account_id` is the account the quote was asked | ||
| for, which on Payment is chosen before it is saved to the draft. | ||
| """ | ||
|
|
||
| documents = [ | ||
| { | ||
| "role": document.role, | ||
| "filing_type": document.filing_type_code, | ||
| "document_type": document.document_type_code, | ||
| "component": document.filing_component_code, | ||
| "optional_services": sorted( | ||
| json.dumps(service, sort_keys=True, default=str) | ||
| for service in (document.requested_optional_services or []) | ||
| ), | ||
| "amount_in_controversy_required": document.filing_requires_amount_in_controversy, | ||
| } | ||
| for document in FilingDocument.objects.filter(draft=draft).order_by("role", "sort_order", "pk") | ||
| ] | ||
| parties = sorted( | ||
| (party.role, party.party_type, bool(party.is_filing_party), bool(party.organization_name)) | ||
| for party in FilingParty.objects.filter(draft=draft) | ||
| ) | ||
| return { | ||
| "jurisdiction": draft.jurisdiction, | ||
| "court": draft.court_code, | ||
| "case_category": draft.case_category_code, | ||
| "case_type": draft.case_type_code, | ||
| "existing_case": draft.existing_case, | ||
| "previous_case_id": draft.previous_case_id, | ||
| "amount_in_controversy": draft.amount_in_controversy, | ||
| "optional_services": json.dumps(draft.optional_services or [], sort_keys=True, default=str), | ||
| "payment_account": draft.selected_payment_account_id if payment_account_id is None else payment_account_id, | ||
| "documents": documents, | ||
| "parties": parties, | ||
| } | ||
|
|
||
|
|
||
| def fee_fingerprint(draft: FilingDraft, *, payment_account_id: str | None = None) -> str: | ||
| encoded = json.dumps(fee_inputs(draft, payment_account_id=payment_account_id), sort_keys=True, default=str) | ||
| return hashlib.sha256(encoded.encode("utf-8")).hexdigest() | ||
|
|
||
|
|
||
| def fee_quote_state(draft: FilingDraft) -> str: | ||
| if draft.selected_payment_account_type == WAIVER_ACCOUNT_TYPE: | ||
| return FeeQuoteState.WAIVED | ||
| if not draft.quoted_fee_total: | ||
| return FeeQuoteState.MISSING | ||
| # A quote saved before fingerprints existed says nothing about what it | ||
| # priced, so it is treated like one that no longer matches. | ||
| if not draft.quoted_fee_fingerprint or draft.quoted_fee_fingerprint != fee_fingerprint(draft): | ||
| return FeeQuoteState.STALE | ||
| return FeeQuoteState.CURRENT | ||
|
|
||
|
|
||
| def fee_quote_is_usable(draft: FilingDraft) -> bool: | ||
| """Whether the filing may be submitted against the quote the filer saw.""" | ||
|
|
||
| return fee_quote_state(draft) in {FeeQuoteState.CURRENT, FeeQuoteState.WAIVED} | ||
|
|
||
|
|
||
| def invalidate_fee_quote(draft: FilingDraft, *, save: bool = True) -> None: | ||
| """Forget the quote outright, for changes that make it meaningless.""" | ||
|
|
||
| draft.quoted_fee_total = "" | ||
| draft.quoted_fee_breakdown = [] | ||
| draft.quoted_fee_fingerprint = "" | ||
| if save: | ||
| draft.save(update_fields=["quoted_fee_total", "quoted_fee_breakdown", "quoted_fee_fingerprint", "updated_at"]) | ||
|
|
||
|
|
||
| def quote_from_efsp_response(response: dict[str, Any]) -> tuple[str, list[dict[str, str]]] | None: | ||
| """The total and itemized charges from an EFSP fee response, or None. | ||
|
|
||
| None when the response names no total: a quote that cannot be read is not | ||
| one to record, and Review then says the fee could not be determined. | ||
| """ | ||
|
|
||
| if not isinstance(response, dict): | ||
| return None | ||
| total = (response.get("feesCalculationAmount") or {}).get("value") | ||
| if total in (None, ""): | ||
| return None | ||
| try: | ||
| total = str(Decimal(str(total)).quantize(Decimal("0.01"))) | ||
| except (InvalidOperation, ValueError): | ||
| return None | ||
| breakdown = [] | ||
| for fee in response.get("allowanceCharge") or []: | ||
| if not isinstance(fee, dict) or not (fee.get("chargeIndicator") or {}).get("value"): | ||
| continue | ||
| breakdown.append( | ||
| { | ||
| "label": str((fee.get("allowanceChargeReason") or {}).get("value") or "Court fee"), | ||
| "amount": str((fee.get("amount") or {}).get("value") or "0.00"), | ||
| } | ||
| ) | ||
| return total, breakdown | ||
|
|
||
|
|
||
| def record_fee_quote( | ||
| draft: FilingDraft, | ||
| total: str, | ||
| breakdown: list[dict[str, str]], | ||
| *, | ||
| payment_account_id: str | None = None, | ||
| ) -> None: | ||
| draft.quoted_fee_total = total | ||
| draft.quoted_fee_breakdown = breakdown | ||
| draft.quoted_fee_fingerprint = fee_fingerprint(draft, payment_account_id=payment_account_id) | ||
| draft.save(update_fields=["quoted_fee_total", "quoted_fee_breakdown", "quoted_fee_fingerprint", "updated_at"]) | ||
|
|
||
|
|
||
| def fee_quote_summary(draft: FilingDraft) -> dict[str, Any]: | ||
| """What Review and the fee API tell the page about the quote.""" | ||
|
|
||
| state = fee_quote_state(draft) | ||
| current = state == FeeQuoteState.CURRENT | ||
| return { | ||
| "state": state, | ||
| "total": draft.quoted_fee_total if current else "", | ||
| "breakdown": list(draft.quoted_fee_breakdown or []) if current else [], | ||
| } |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The EFSP response here priced the
efile_datacaptured before the network call, butrecord_fee_quote()computes its fingerprint from the draft after that call returns. If another tab changes a fee-affecting field while the (up to 60s) fee request is in flight, the old total can be stored with the new draft鈥檚 fingerprint andfee_quote_state()will report CURRENT鈥攄efeating the stale-quote/submission guard this PR adds. Could you snapshot the expected fingerprint before sending the fee request and only record the response if the current draft still matches it (or otherwise bind the stored fingerprint to the exact inputs/payload that was priced)? A test that mutates the draft inside the mockedrequests.postwould catch this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in e342a77. This binds the quote to the draft state the payload was built from, not just to the state at the start of the request. Payment and Review now render a token of the draft's fee inputs (the fingerprint without the payment account, which is bound separately). The page sends that token with the fee request. The quote is recorded only if the draft still matches the token before the EFSP call and again, under a row lock, when it answers. If it doesn't match, the response has
quote_superseded: true, nothing is stored, and the page asks the filer to reload. New tests: the draft changes inside the mockedrequests.post, the request comes from an older page, and the request carries no token. The optional-services regression now takes its token from the Review page itself.