Repository navigation
[FIX] thankyou_letters: forget deleted donation lines in pending thank you - #2224
Merged
Merged
Conversation
…k you When a donation line was deleted (e.g. the bank statement line it was booked on is reconciled again) while its pending thank you held other lines, the thank you was refreshed but kept the deleted id in object_ids: rendering it again failed with "MissingError: Record does not exist (account.move.line(...))". - unlink() removes the deleted lines from object_ids before the refresh, in every communication not sent yet that holds them: the one the entry points to, and the ones holding them without that pointer (merged at creation, cancelled by a merge, thank you wizard, ambassador receipts). - generate_thank_you() no longer carries forward a deleted line of a pending thank you it merges into. Assisted-by: claude-opus-5-5, medium, claude-code
[Medium impact] The PR appears safe to merge; the earlier non-blocking concern about scanning unrelated letters remains. Findings
Reviews (3) · Last reviewed commit: "[FIX] thankyou_letters: queued thank you..." · Reviewed by Greptile |
…hank you Review of #2224: - the communications holding deleted lines are searched among all those not sent yet, whoever they are sent to: ambassador receipts (crm_compassion) are sent to the ambassador, not to the donor; - generate_thank_you() cancels a pending thank you of another config whose lines were all deleted, instead of writing it an empty object_ids. Assisted-by: claude-opus-5-5, medium, claude-code
Comment on lines
+68
to
+73
| jobs |= self.env["partner.communication.job"].search( | ||
| [ | ||
| ("config_id.model_id.model", "=", self._name), | ||
| ("state", "in", ("pending", "call", "failure")), | ||
| ] | ||
| ) |
There was a problem hiding this comment.
Deletions scan unrelated letters
Every account.move.line.unlink() now loads all pending, call, and failed communications for account lines, then parses every job's object_ids in Python—even when deleting an unrelated accounting line. This adds work that grows with the entire backlog rather than the affected donations.
Narrow the search by the deleted line IDs before loading and parsing jobs, while still allowing recipients other than the donor.
The thank you of donations booked on a journal entry runs in a queue job on the lines. When the bank statement line is reconciled again before the job runs, these lines are deleted and recreated: the job failed with MissingError, although the new lines have their own job. The job now calls generate_thank_you_job(), which skips the deleted lines. Assisted-by: claude-opus-5-5, medium, claude-code
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.
What and why
A thank-you communication lists its donation lines in
object_ids. When one of these lines is deleted while the thank you also holds other lines (typically: a bank statement line reconciled again, its entry lines deleted and recreated),account.move.line.unlink()refreshed the thank you but kept the deleted id. Rendering it again then failed:Seen in production on a thank you grouping two donations booked from two bank statement lines.
Changes in
thankyou_letters:unlink()removes the deleted lines fromobject_idsbefore the refresh (the thank you is still deleted when no line is left, as before)._get_unsent_communications), whoever it is sent to, not only the onemove_id.communication_idpoints to: thank yous merged at creation (partner.communication.job.create) or kept by a merge that cancelled others, the thank you wizard, ambassador receipts (crm_compassion, sent to the ambassador). It reads the communications not sent yet of theaccount.move.lineconfigs (about 550 on the Swiss database) and keeps those holding the deleted lines.generate_thank_you()drops deleted lines of the pending thank you it merges into, so a thank you already holding a dead id is healed by the next donation instead of copying the id forward. A pending thank you of another config whose lines were all deleted is cancelled instead of getting an emptyobject_ids.Sent communications (processing, done) are not touched: their text is kept as sent.
Same scenario, queue side: the thank you of donations booked on a journal entry runs in a queue job on the lines. When the bank statement line is reconciled again before the job runs, the lines are deleted and recreated and the job failed with
MissingError(seen in production), although the new lines have their own job. The job now callsgenerate_thank_you_job(), which skips the deleted lines and thanks those that remain.Deployment notes
No upgrade needed (Python only). Thank-you jobs queued before the deployment still call
generate_thank_youand fail the same way when their lines are gone: they are obsolete (the new lines have their own job) and can be set to done. Communications already holding deleted ids are repaired by a one-off script (thankyou_deleted_lines.py, private ops scripts, dry run unlessAPPLY=1): it can run before this PR is deployed, and once more after to catch the cases created in between.Test evidence
Run on an empty database (
-i thankyou_letters, then--test-tags /thankyou_letters:TestThankYouDeletedLine): 6 tests, 0 failed.test_queued_thank_you_skips_lines_deleted_since, run with the former job entry point (generate_thank_you) instead ofgenerate_thank_you_job: ERROR,MissingError, as in production.On the first commit of this PR (ba612d6), the two tests added after the Greptile review fail:
test_deleted_line_of_thank_you_without_entry_pointer(receipt sent to an ambassador who is not the donor): FAIL,'59,60' != '60'test_new_donation_cancels_thank_you_of_deleted_lines_only: FAIL,'pending' != 'cancel'On
origin/18.0(model file of the base, first four tests):test_deleted_line_is_removed_from_thank_you(two entries sharing one thank you, one line deleted): FAIL,'27,25' != '25'test_deleted_line_of_thank_you_without_entry_pointer: FAIL,'29,30' != '30'test_new_donation_drops_deleted_line_of_pending_thank_you: ERROR,MissingErrortest_thank_you_of_deleted_lines_is_removed: passes on both (characterization of the existing behaviour).The one-off script was run in dry run on a copy of production data (922 communications with deleted ids, nearly all old sent letters) and on two communications set back to pending in the transaction: one refreshed without error with the remaining lines, one cancelled (no line left); its own check reported 0 errors.
Sibling paths
Invariant: a thank-you communication not sent yet never lists a deleted
account.move.line.account.move.line.unlinkis the choke point;account.move.writewithCommand.delete,account.move.unlinkandaccount_reconcile_oca(_reconcile_bank_line_edit) all go through it. Covered by the tests above (driven throughaccount.move.write); the reconciliation flow itself is not driven (other repository).account.move.generate_thank_you,cancel_thankyou_letter) now queuegenerate_thank_you_job; testtest_queued_thank_you_skips_....object_idswithout entry pointer (create merge, wizard, ambassador receipts, cancelled-by-merge): covered by the search in_get_unsent_communications, which does not filter on the recipient; test..._without_entry_pointeruses a receipt sent to another partner than the donor.refresh_text/ rendering get only live ids on these paths;generate_thank_youreads with.exists()(testtest_new_donation_drops_...) and cancels a thank you left without line (testtest_new_donation_cancels_...).Rules review
Run with the
code-rulesreviewer on the first version of this change:move_id.communication_id, missing the create merge, wizard and ambassador paths: fixed, search by lines (first by partner, widened after the Greptile review below).generate_thank_youcopied dead ids forward: fixed with.exists(), new test.partner.communication.job, so this branch may raise anAccessErrorfor them. Unchanged by this PR, not covered by a test as a non-admin user.Greptile review
object_ids. Fixed in cbe196d: it is cancelled; test added.🤖 Generated with Claude Code