Fix cleanup_unsubmitted_forms date logic and safe entity cleanup#17
Fix cleanup_unsubmitted_forms date logic and safe entity cleanup#17BilenAssres wants to merge 3 commits into
Conversation
WalkthroughRefactored token cleanup logic in the unsubmitted forms cleanup utility. Replaced 7-day date calculations with a simpler cutoffDate approach, split bulk transaction handling into conditional logic based on entity ID presence, added guard clauses for missing entity IDs, and updated error handling with string literal status values. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
tests/unsubmitted_forms/cleanup_unsubmitted_forms.ts (2)
42-42: Good addition for observability.Consider also logging a summary at the end (e.g., how many tokens were successfully cleaned up vs. failed) to aid debugging in production.
44-86: Consider per-token error handling for resilience.If any single token transaction fails, the job marks as "failed" but tokens processed before the failure are already deleted (each transaction commits independently). This can leave the cleanup in an inconsistent state with no way to identify which tokens were processed.
Consider wrapping individual token processing in try-catch, collecting failures, and continuing with remaining tokens:
🔎 Proposed approach for resilient processing
+ const failures: string[] = []; + for (const token of expiredTokens) { + try { // Guard against missing entityId; delete only the token in that case if (!token.entityId) { // ... existing guard logic } await prisma.$transaction([ // ... existing transaction ]); + } catch (tokenError) { + console.error(`Failed to cleanup token ${token.token}:`, tokenError); + failures.push(token.token); + } } + if (failures.length > 0) { + console.error(`cleanup_unsubmitted_forms: ${failures.length} tokens failed to cleanup`); + // Optionally: still mark as 'completed' with warnings, or use 'partial_failure' status + } + await update_job_status(job.id, 'completed');
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge base: Disabled due to data retention organization setting
📒 Files selected for processing (1)
tests/unsubmitted_forms/cleanup_unsubmitted_forms.ts(1 hunks)
🔇 Additional comments (3)
tests/unsubmitted_forms/cleanup_unsubmitted_forms.ts (3)
32-40: LGTM!The cutoff date calculation and query logic are correct. Using
Date.now() - 7 * 24 * 60 * 60 * 1000correctly computes 7 days ago, and thelt: cutoffDatefilter properly finds tokens older than that threshold.
44-54: Good defensive programming.The guard clause correctly handles the edge case where
entityIdis missing, preventing potential errors downstream. The warning log helps with visibility into data quality issues.
56-79: Potential foreign key constraint violation when deleting entity.The transaction deletes only relationships with
status: "new"and matchingproduct_id, but then unconditionally deletes the entity. If the entity has relationships with other statuses or from other products, theentity.deletewill fail due to foreign key constraints. This could cause the cleanup job to fail mid-transaction, leaving some tokens partially deleted.Either:
- Delete all relationships for the entity (remove the
product_idandstatusfilters), or- Add error handling to skip entity deletion if other relationships exist, or
- Verify that entities only ever have relationships for a single product with "new" status.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.