Implemented the Unsubmitted Forms Queue Fix#20
Conversation
A Read me was also included
WalkthroughThe changes consolidate fixes for three issues in the unsubmitted forms cleanup: correcting the date range logic to include older qualifying records, fixing the 7-day interval calculation from seconds to milliseconds, and updating the deletion flow to explicitly clean up orphaned records with proper transaction management. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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: 1
🤖 Fix all issues with AI agents
In @tests/unsubmitted_forms/cleanup_unsubmitted_forms.ts:
- Around line 36-38: The inline comment on the createdAt filter is stale; update
or remove the comment next to the createdAt: { lt: cutoffDate } expression in
cleanup_unsubmitted_forms to reflect the current logic (e.g., change to "//
older than cutoffDate (7 days ago)" or simply remove the misleading text "// but
less than 7 days ago + 1 day") so the comment matches the actual condition.
🧹 Nitpick comments (3)
tests/unsubmitted_forms/cleanup_unsubmitted_forms.ts (3)
51-76: Inconsistent indentation within transaction array.The array elements inside the transaction have inconsistent indentation (8 spaces) compared to the surrounding code structure. This affects readability.
Suggested formatting fix
if (relationship) { try { await prisma.$transaction([ - // Delete relationship first - prisma.relationship.delete({ - where: { id: relationship.id }, - }), - // Delete all corpus items associated with the entity - prisma.new_corpus.deleteMany({ - where: { - entity_id: token.entityId, - }, - }), - // Delete the entity - prisma.entity.delete({ - where: { id: token.entityId }, - }), - // Delete the token last - prisma.publicFormsTokens.delete({ - where: { token: token.token }, - }), + // Delete relationship first + prisma.relationship.delete({ + where: { id: relationship.id }, + }), + // Delete all corpus items associated with the entity + prisma.new_corpus.deleteMany({ + where: { + entity_id: token.entityId, + }, + }), + // Delete the entity + prisma.entity.delete({ + where: { id: token.entityId }, + }), + // Delete the token last + prisma.publicFormsTokens.delete({ + where: { token: token.token }, + }), ]);
50-100: Consider reducing duplication between branches.The corpus, entity, and token deletion operations are duplicated in both branches. You could refactor to build the transaction array dynamically or extract common deletions.
Optional refactor to reduce duplication
if (relationship) { try { + const operations = [ + prisma.relationship.delete({ + where: { id: relationship.id }, + }), + ]; + + await deleteEntityAndToken(operations, token); + } catch (transactionError) { + console.error(`Failed to delete expired token ${token.token}:`, transactionError); + continue; + } + } else { + try { + await deleteEntityAndToken([], token); + } catch (transactionError) { + console.error(`Failed to delete expired token ${token.token}:`, transactionError); + continue; + } + }Where
deleteEntityAndTokenis a helper function:async function deleteEntityAndToken( prefixOperations: Prisma.PrismaPromise<any>[], token: PublicFormsTokens ) { await prisma.$transaction([ ...prefixOperations, prisma.new_corpus.deleteMany({ where: { entity_id: token.entityId }, }), prisma.entity.delete({ where: { id: token.entityId }, }), prisma.publicFormsTokens.delete({ where: { token: token.token }, }), ]); }
42-104: Consider adding success logging for observability.The job logs errors but doesn't track successful deletions. For a scheduled cleanup job, logging the count of processed/deleted tokens would help with monitoring and debugging.
let deletedCount = 0; let failedCount = 0; for (const token of expiredTokens) { // ... existing logic ... // On success: deletedCount++; // On catch: failedCount++; } console.log(`Cleanup completed: ${deletedCount} deleted, ${failedCount} failed out of ${expiredTokens.length} expired tokens`);
📜 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 (2)
tests/unsubmitted_forms/ReadMe.MDtests/unsubmitted_forms/cleanup_unsubmitted_forms.ts
🧰 Additional context used
🪛 LanguageTool
tests/unsubmitted_forms/ReadMe.MD
[style] ~52-~52: Try moving the adverb to make the sentence clearer.
Context: ...### Resolution An else condition was added to explicitly delete the entity when no related records are found, ensu...
(SPLIT_INFINITIVE)
🔇 Additional comments (3)
tests/unsubmitted_forms/ReadMe.MD (1)
1-60: Clear and well-structured documentation.The README effectively documents the three issues fixed in this PR with clear problem statements and resolutions. This will be helpful for future maintainers understanding the context of these changes.
tests/unsubmitted_forms/cleanup_unsubmitted_forms.ts (2)
32-32: Correct millisecond-based calculation for 7-day cutoff.The calculation
7 * 24 * 60 * 60 * 1000correctly computes 7 days in milliseconds, fixing the original bug where the* 1000multiplier was missing.
77-100: Good addition of the else branch for orphaned record cleanup.This correctly addresses the case where no relationship exists, ensuring entities and tokens without relationships are still properly cleaned up instead of being left as orphaned records.
A Read me was also included to provide details into the Unsubmitted Forms Queue Fix.
The three issues raised are
Kindly find loom video https://www.loom.com/share/f18cb6ed545e493ab5763fca93f5c0c3
Summary by CodeRabbit
Release Notes
Bug Fixes
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.