const { Pool } = require('pg'); // Database configuration - using the same connection as the main app const pool = new Pool({ connectionString: process.env.SUPABASE_URL ? process.env.SUPABASE_URL.replace('postgresql://', 'postgresql://postgres.ghurdhqdcrxeugyuxxqa:') : 'postgresql://postgres.ghurdhqdcrxeugyuxxqa:Ze7KGPXLa6CGDN0gsYfgBEP2N4Y-8YGUB_H6xyxggu8@aws-0-us-east-1.pooler.supabase.com:6543/postgres', ssl: { rejectUnauthorized: false } }); async function resetStuckDocument(documentId) { try { console.log(`šŸ”„ Resetting stuck document: ${documentId}`); // First, check the current status const checkQuery = ` SELECT id, original_file_name, status, error_message, created_at, processing_completed_at FROM documents WHERE id = $1 `; const checkResult = await pool.query(checkQuery, [documentId]); if (checkResult.rows.length === 0) { console.log('āŒ Document not found'); return; } const document = checkResult.rows[0]; console.log('\nšŸ“„ Current Document Status:'); console.log(` ID: ${document.id}`); console.log(` Name: ${document.original_file_name}`); console.log(` Status: ${document.status}`); console.log(` Created: ${document.created_at}`); console.log(` Completed: ${document.processing_completed_at || 'Not completed'}`); console.log(` Error: ${document.error_message || 'None'}`); // Check if document is actually stuck const processingTime = new Date() - new Date(document.created_at); const hoursSinceCreation = processingTime / (1000 * 60 * 60); console.log(`\nā±ļø Processing Time Analysis:`); console.log(` Time since creation: ${hoursSinceCreation.toFixed(2)} hours`); if (hoursSinceCreation < 0.5) { console.log('āš ļø Document has been processing for less than 30 minutes - may not be stuck'); console.log('šŸ’” Consider waiting a bit longer before resetting'); return; } // Reset the document status const resetQuery = ` UPDATE documents SET status = 'uploaded', error_message = NULL, processing_completed_at = NULL, analysis_data = NULL, generated_summary = NULL, updated_at = CURRENT_TIMESTAMP WHERE id = $1 `; const resetResult = await pool.query(resetQuery, [documentId]); if (resetResult.rowCount > 0) { console.log('\nāœ… Document successfully reset!'); console.log(' Status changed to: uploaded'); console.log(' Error message cleared'); console.log(' Analysis data cleared'); console.log(' Ready for reprocessing'); // Also clear any stuck processing jobs const clearJobsQuery = ` UPDATE processing_jobs SET status = 'failed', error_message = 'Document reset by admin', completed_at = CURRENT_TIMESTAMP WHERE document_id = $1 AND status IN ('pending', 'processing') `; const clearJobsResult = await pool.query(clearJobsQuery, [documentId]); console.log(` Cleared ${clearJobsResult.rowCount} stuck processing jobs`); } else { console.log('āŒ Failed to reset document'); } } catch (error) { console.error('āŒ Error resetting document:', error); } finally { await pool.end(); } } // Get document ID from command line argument const documentId = process.argv[2]; if (!documentId) { console.log('Usage: node reset-stuck-document.js '); console.log('Example: node reset-stuck-document.js f5509048-d282-4316-9b65-cb89bf8ac09d'); console.log('\nāš ļø WARNING: This will reset the document and clear all processing data!'); console.log(' The document will need to be reprocessed from the beginning.'); process.exit(1); } resetStuckDocument(documentId);