const { Pool } = require('pg'); const fs = require('fs'); (async () => { const env = fs.readFileSync('/opt/ai-fresh-delivery/server/.env', 'utf8'); const m = env.match(/^DATABASE_URL=(.+)$/m); const pool = new Pool({ connectionString: m[1].trim().replace(/^["']|["']$/g, ''), }); await pool.query(`SELECT set_config('search_path', 'tenant_br002, public', false)`); // Pending inspections created recently const pending = await pool.query( `SELECT i.id, i.order_code, i.sku_code, i.item_name, i.supplier_code, i.prepare_qty, i.purchase_price, i.source_type, i.created_at, i.branch_code, o.order_status, o.customer_code, o.created_at AS order_created_at, sp.prepare_status, sp.prepare_time, sp.created_at AS prep_created_at, sp.purchase_price AS prep_price FROM inspections i LEFT JOIN orders o ON o.order_code = i.order_code LEFT JOIN supplier_preparations sp ON sp.order_code = i.order_code AND sp.sku_code IS NOT DISTINCT FROM i.sku_code AND COALESCE(sp.supplier_code, '') = COALESCE(i.supplier_code, '') WHERE i.inspection_time IS NULL AND COALESCE(i.inbound_batch_no, '') = '' AND COALESCE(i.compliance_status, 0) < 40 AND i.created_at >= NOW() - INTERVAL '36 hours' ORDER BY i.created_at DESC LIMIT 40` ); console.log('PENDING_RECENT_COUNT', pending.rows.length); for (const r of pending.rows) { const prepDone = r.prepare_time ? new Date(r.prepare_time).getTime() : null; const inspCreated = new Date(r.created_at).getTime(); const lagMin = prepDone != null ? Math.round((inspCreated - prepDone) / 60000) : null; console.log( JSON.stringify({ item: r.item_name, order: r.order_code, order_status: r.order_status, insp_created: r.created_at, prep_status: r.prepare_status, prep_time: r.prepare_time, prep_created: r.prep_created_at, lag_min_after_prep: lagMin, source: r.source_type, qty: r.prepare_qty, price: r.purchase_price, }) ); } // Bucket: created in last 2h vs older pending still open const buckets = await pool.query( `SELECT COUNT(*) FILTER (WHERE i.created_at >= NOW() - INTERVAL '2 hours')::int AS last_2h, COUNT(*) FILTER (WHERE i.created_at >= NOW() - INTERVAL '12 hours' AND i.created_at < NOW() - INTERVAL '2 hours')::int AS last_2_12h, COUNT(*) FILTER (WHERE i.created_at < NOW() - INTERVAL '12 hours' AND i.created_at >= NOW() - INTERVAL '36 hours')::int AS last_12_36h, COUNT(*) FILTER ( WHERE i.inspection_time IS NULL AND COALESCE(i.inbound_batch_no, '') = '' AND COALESCE(i.compliance_status, 0) < 40 )::int AS all_pending FROM inspections i WHERE i.created_at >= NOW() - INTERVAL '36 hours' OR ( i.inspection_time IS NULL AND COALESCE(i.inbound_batch_no, '') = '' AND COALESCE(i.compliance_status, 0) < 40 )` ); console.log('BUCKETS', buckets.rows[0]); // Cases where inspection created long after prep completed const delayed = await pool.query( `SELECT i.item_name, i.order_code, i.created_at AS insp_at, sp.prepare_time, ROUND(EXTRACT(EPOCH FROM (i.created_at - sp.prepare_time))/60.0)::int AS lag_min FROM inspections i JOIN supplier_preparations sp ON sp.order_code = i.order_code AND sp.sku_code IS NOT DISTINCT FROM i.sku_code AND COALESCE(sp.supplier_code, '') = COALESCE(i.supplier_code, '') WHERE i.inspection_time IS NULL AND COALESCE(i.inbound_batch_no, '') = '' AND sp.prepare_time IS NOT NULL AND i.created_at > sp.prepare_time + INTERVAL '30 minutes' AND i.created_at >= NOW() - INTERVAL '48 hours' ORDER BY lag_min DESC LIMIT 20` ); console.log('DELAYED_AFTER_PREP', delayed.rows); await pool.end(); })().catch((e) => { console.error(e); process.exit(1); });