115 lines
4.1 KiB
JavaScript
115 lines
4.1 KiB
JavaScript
import mongoose from 'mongoose';
|
|
import axios from 'axios';
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
|
|
|
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/turbotrades';
|
|
const API_URL = 'http://localhost:3000';
|
|
|
|
async function testTransactions() {
|
|
try {
|
|
console.log('🔌 Connecting to MongoDB...');
|
|
await mongoose.connect(MONGODB_URI);
|
|
console.log('✅ Connected to MongoDB\n');
|
|
|
|
// Import models
|
|
const User = (await import('./models/User.js')).default;
|
|
const Transaction = (await import('./models/Transaction.js')).default;
|
|
const Session = (await import('./models/Session.js')).default;
|
|
|
|
// Find the user
|
|
console.log('👤 Finding user...');
|
|
const user = await User.findOne().sort({ createdAt: -1 });
|
|
|
|
if (!user) {
|
|
console.error('❌ No user found!');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`✅ Found user: ${user.username}`);
|
|
console.log(` User ID: ${user._id}`);
|
|
console.log(` Steam ID: ${user.steamId}\n`);
|
|
|
|
// Check transactions in DB
|
|
console.log('💰 Checking transactions in database...');
|
|
const dbTransactions = await Transaction.find({ userId: user._id }).sort({ createdAt: -1 });
|
|
console.log(`✅ Found ${dbTransactions.length} transactions in DB`);
|
|
|
|
if (dbTransactions.length > 0) {
|
|
console.log('\n📋 Sample transactions:');
|
|
dbTransactions.slice(0, 5).forEach((t, i) => {
|
|
console.log(` ${i + 1}. ${t.type.padEnd(12)} $${t.amount.toFixed(2).padStart(8)} - Session: ${t.sessionIdShort || 'NONE'}`);
|
|
});
|
|
} else {
|
|
console.log('⚠️ No transactions found for this user!');
|
|
console.log('💡 Run: node seed-transactions.js');
|
|
}
|
|
|
|
// Check sessions
|
|
console.log('\n🔐 Checking sessions...');
|
|
const sessions = await Session.find({ userId: user._id, isActive: true });
|
|
console.log(`✅ Found ${sessions.length} active sessions`);
|
|
sessions.forEach((s, i) => {
|
|
console.log(` ${i + 1}. ${s.browser || 'Unknown'} on ${s.os || 'Unknown'} (...${s._id.toString().slice(-6)})`);
|
|
});
|
|
|
|
// Check if we can get a valid session token
|
|
console.log('\n🔑 Testing API authentication...');
|
|
if (sessions.length > 0) {
|
|
const session = sessions[0];
|
|
console.log(` Using session: ${session._id}`);
|
|
console.log(` Token: ${session.token.substring(0, 20)}...`);
|
|
|
|
try {
|
|
// Test the transactions endpoint
|
|
console.log('\n📡 Testing /api/user/transactions endpoint...');
|
|
const response = await axios.get(`${API_URL}/api/user/transactions`, {
|
|
headers: {
|
|
'Cookie': `token=${session.token}`
|
|
},
|
|
withCredentials: true
|
|
});
|
|
|
|
if (response.data.success) {
|
|
console.log('✅ API request successful!');
|
|
console.log(` Returned ${response.data.transactions.length} transactions`);
|
|
console.log(` Stats:`, response.data.stats);
|
|
|
|
if (response.data.transactions.length > 0) {
|
|
console.log('\n📊 Sample API transactions:');
|
|
response.data.transactions.slice(0, 3).forEach((t, i) => {
|
|
console.log(` ${i + 1}. ${t.type.padEnd(12)} $${t.amount.toFixed(2).padStart(8)} - Session: ${t.sessionIdShort || 'NONE'}`);
|
|
});
|
|
}
|
|
} else {
|
|
console.log('⚠️ API returned success: false');
|
|
}
|
|
} catch (apiError) {
|
|
console.error('❌ API request failed:', apiError.message);
|
|
if (apiError.response) {
|
|
console.error(' Status:', apiError.response.status);
|
|
console.error(' Data:', apiError.response.data);
|
|
}
|
|
}
|
|
} else {
|
|
console.log('⚠️ No active sessions found. Cannot test API.');
|
|
console.log('💡 Log in via Steam to create a session');
|
|
}
|
|
|
|
console.log('\n' + '='.repeat(60));
|
|
console.log('✅ Test complete!');
|
|
console.log('='.repeat(60));
|
|
|
|
} catch (error) {
|
|
console.error('\n❌ Error during test:', error);
|
|
process.exit(1);
|
|
} finally {
|
|
await mongoose.disconnect();
|
|
console.log('\n🔌 Disconnected from MongoDB');
|
|
process.exit(0);
|
|
}
|
|
}
|
|
|
|
testTransactions();
|