Skip to content

Implemented the Unsubmitted Forms Queue Fix#20

Open
kayuse wants to merge 1 commit into
vula-africa:mainfrom
kayuse:kayode/feature/unsubmitted_forms
Open

Implemented the Unsubmitted Forms Queue Fix#20
kayuse wants to merge 1 commit into
vula-africa:mainfrom
kayuse:kayode/feature/unsubmitted_forms

Conversation

@kayuse
Copy link
Copy Markdown

@kayuse kayuse commented Jan 8, 2026

A Read me was also included to provide details into the Unsubmitted Forms Queue Fix.

The three issues raised are

  1. Incorrect Date Range Logic
  2. Incorrect Time Calculation
  3. Missing Relationship Handling During Deletion

Kindly find loom video https://www.loom.com/share/f18cb6ed545e493ab5763fca93f5c0c3

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Fixed date range filtering to correctly include records beyond the previous 7–8 day window.
    • Corrected time calculations for accurate 7-day interval comparisons.
    • Improved cleanup process to properly remove orphaned records when no related records exist.
  • Documentation

    • Updated documentation reflecting corrections to date filtering, time calculations, and cleanup behavior.

✏️ Tip: You can customize this high-level summary in your review settings.

A Read me was also included
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 8, 2026

Walkthrough

The 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

Cohort / File(s) Summary
Documentation
tests/unsubmitted_forms/ReadMe.MD
Updated documentation describing three consolidated fixes: corrected date range logic, fixed 7-day time calculation, and improved deletion flow for orphaned records.
Cleanup Logic
tests/unsubmitted_forms/cleanup_unsubmitted_forms.ts
Replaced 7-day window calculation with explicit cutoffDate filtering; reorganized deletion logic into staged, transaction-based operations with separate flows for related vs. unrelated records; added error handling with try/catch per transaction to continue processing on failures; adjusted corpus deletion to use token.entityId directly.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 Hoppy hops through tokens old,
Seven days now rightly told,
Orphans swept without a trace,
Transactions keep the cleanup pace!
Logic mended, dates made clear—
Cleaner databases this year! 🗑️✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Implemented the Unsubmitted Forms Queue Fix' accurately reflects the main changes in the pull request, which consolidates three critical fixes to the unsubmitted forms cleanup process.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 deleteEntityAndToken is 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

📥 Commits

Reviewing files that changed from the base of the PR and between 1dd21f7 and 1d3d1e7.

📒 Files selected for processing (2)
  • tests/unsubmitted_forms/ReadMe.MD
  • tests/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 * 1000 correctly computes 7 days in milliseconds, fixing the original bug where the * 1000 multiplier 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.

Comment thread tests/unsubmitted_forms/cleanup_unsubmitted_forms.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant