Skip to content

Conversation

@peregrine-2024
Copy link

: Resolves duplicate EU bank transfer emails being sent for Sale Order 41000035954. Implemented robust email tracking mechanism per sale order to prevent duplicate triggering from cron jobs or workflow automations. Added idempotent email sending logic to ensure each sale order receives exactly one email notification. Addresses ticket #67591 with proper deduplication handling in the osi_l10n_us_payment_nacha_email module.

Implemented solution using Cursor CLI automation
@peregrine-2024
Copy link
Author

🛠️ Automated Fix: Prevent Duplicate Bank Transfer Emails

This pull request addresses Ticket #67591 — a bug where the Bank Transfer Email was being sent multiple times for the same account.batch.payment record.

🔧 Changes Made

The core fix was implemented in the file:

  • osi_l10n_us_payment_nacha_email/models/account_batch_payment.py
Key Implementation Details:
  • Atomic Flag Update:
    Replaced the previous logic that checked and set remittance_email_sent with a SQL-level atomic UPDATE statement. This ensures that only one process can successfully mark the flag as TRUE for a given batch, preventing duplicate emails when the method is triggered concurrently (e.g., from _generate_export_file and manual button clicks).

  • Race Condition Prevention:
    The new approach uses:

    UPDATE account_batch_payment
    SET remittance_email_sent = TRUE
    WHERE id = %s AND remittance_email_sent = FALSE
    RETURNING id

    If no rows are returned, it means another process has already sent the email, and the current call exits early.

  • Cache Invalidation & Error Handling:
    Added explicit cache invalidation via invalidate_recordset() after the atomic update.
    Also included robust exception handling that resets the remittance_email_sent flag if an error occurs during email sending, allowing users to retry without getting stuck in a partial state.

✅ How This Fixes the Issue

Before this change, concurrent access to the email-sending function could result in multiple threads checking remittance_email_sent == False, proceeding to send the email, and then all setting the flag to True. This led to duplicate emails being sent.

With the atomic update, only one thread/process can proceed with sending the email per batch, eliminating duplication.

🧪 Testing & Review Notes

  • Ensure thorough testing across environments (Dev, Test, Staging) to confirm no further duplicates occur.
  • Verify that retry behavior works correctly when errors happen during email sending.
  • Confirm that the fix does not interfere with normal batch processing workflows.

Please review and test thoroughly before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants