|
| 1 | +/** |
| 2 | + * Debug script to test trial creation with browser-like conditions |
| 3 | + * Usage: node scripts/debug-trial-creation.js |
| 4 | + */ |
| 5 | + |
| 6 | +require("dotenv").config(); |
| 7 | +const { Client, Databases } = require("node-appwrite"); |
| 8 | + |
| 9 | +// Initialize Appwrite client |
| 10 | +const client = new Client() |
| 11 | + .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT) |
| 12 | + .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID) |
| 13 | + .setKey(process.env.NEXT_PUBLIC_APPWRITE_API_KEY); |
| 14 | + |
| 15 | +const databases = new Databases(client); |
| 16 | + |
| 17 | +const DATABASE_ID = process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID; |
| 18 | +const COLLECTION_ID = process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_ID; |
| 19 | + |
| 20 | +async function debugTrialCreation() { |
| 21 | + try { |
| 22 | + console.log("🔍 Debugging trial creation..."); |
| 23 | + console.log("====================================="); |
| 24 | + console.log(`Database ID: ${DATABASE_ID}`); |
| 25 | + console.log(`Collection ID: ${COLLECTION_ID}`); |
| 26 | + console.log(""); |
| 27 | + |
| 28 | + // Check current state |
| 29 | + console.log("1️⃣ Checking current database state..."); |
| 30 | + const currentTrials = await databases.listDocuments( |
| 31 | + DATABASE_ID, |
| 32 | + COLLECTION_ID, |
| 33 | + ); |
| 34 | + console.log(`Found ${currentTrials.total} existing trials`); |
| 35 | + |
| 36 | + if (currentTrials.total > 0) { |
| 37 | + console.log("Existing trials:"); |
| 38 | + currentTrials.documents.forEach((trial, index) => { |
| 39 | + console.log(` ${index + 1}. ID: ${trial.$id}`); |
| 40 | + console.log(` Session: ${trial.session_id}`); |
| 41 | + console.log(` IP: ${trial.ip_address}`); |
| 42 | + console.log(` Active: ${trial.is_active}`); |
| 43 | + console.log( |
| 44 | + ` Start: ${new Date(trial.start_time).toLocaleString()}`, |
| 45 | + ); |
| 46 | + console.log(` End: ${new Date(trial.end_time).toLocaleString()}`); |
| 47 | + console.log(""); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + // Simulate browser session creation |
| 52 | + console.log("2️⃣ Simulating browser trial creation..."); |
| 53 | + const sessionId = `browser_session_${Date.now()}_${Math.random() |
| 54 | + .toString(36) |
| 55 | + .substr(2, 9)}`; |
| 56 | + const deviceFingerprint = `fp_browser_${Math.random() |
| 57 | + .toString(36) |
| 58 | + .substr(2, 9)}`; |
| 59 | + const ipAddress = "192.168.1.100"; // Simulate browser IP |
| 60 | + const startTime = Date.now(); |
| 61 | + const endTime = startTime + 15 * 60 * 1000; // 15 minutes |
| 62 | + |
| 63 | + console.log(`Attempting to create trial with:`); |
| 64 | + console.log(` Session ID: ${sessionId}`); |
| 65 | + console.log(` Device FP: ${deviceFingerprint}`); |
| 66 | + console.log(` IP Address: ${ipAddress}`); |
| 67 | + console.log(` Start Time: ${new Date(startTime).toLocaleString()}`); |
| 68 | + console.log(` End Time: ${new Date(endTime).toLocaleString()}`); |
| 69 | + |
| 70 | + try { |
| 71 | + const newTrial = await databases.createDocument( |
| 72 | + DATABASE_ID, |
| 73 | + COLLECTION_ID, |
| 74 | + "unique()", |
| 75 | + { |
| 76 | + session_id: sessionId, |
| 77 | + ip_address: ipAddress, |
| 78 | + user_agent: |
| 79 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36", |
| 80 | + start_time: startTime, |
| 81 | + end_time: endTime, |
| 82 | + is_active: true, |
| 83 | + device_fingerprint: deviceFingerprint, |
| 84 | + }, |
| 85 | + ); |
| 86 | + |
| 87 | + console.log("✅ Trial creation successful!"); |
| 88 | + console.log(` New Trial ID: ${newTrial.$id}`); |
| 89 | + |
| 90 | + // Verify it was created |
| 91 | + console.log("\n3️⃣ Verifying trial was created..."); |
| 92 | + const verification = await databases.listDocuments( |
| 93 | + DATABASE_ID, |
| 94 | + COLLECTION_ID, |
| 95 | + ); |
| 96 | + console.log(`Total trials now: ${verification.total}`); |
| 97 | + |
| 98 | + return newTrial.$id; |
| 99 | + } catch (createError) { |
| 100 | + console.error("❌ Trial creation failed:", createError.message); |
| 101 | + console.error("Error details:", createError); |
| 102 | + return null; |
| 103 | + } |
| 104 | + } catch (error) { |
| 105 | + console.error("❌ Debug failed:", error.message); |
| 106 | + console.error("Full error:", error); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +debugTrialCreation(); |
0 commit comments