import { pool } from '../db.js'; async function testAdminAPIFilter() { try { console.log('\nđŸ§Ș SIMULATION APPEL API ADMIN TICKETS\n'); // Simuler exactement ce que fait l'API const page = 1; const limit = 20; const prizeType = 'THE_SIGNATURE'; const offset = (page - 1) * limit; console.log('ParamĂštres:'); console.log(` page: ${page}`); console.log(` limit: ${limit}`); console.log(` prizeType: ${prizeType}`); console.log(` offset: ${offset}\n`); // RequĂȘte exacte de l'API let query = ` SELECT t.id, t.code, t.status, t.played_at, t.claimed_at, t.validated_at, t.created_at, u.email as user_email, u.first_name || ' ' || u.last_name as user_name, p.name as prize_name, p.type as prize_type, p.value as prize_value, v.first_name || ' ' || v.last_name as validated_by_name FROM tickets t LEFT JOIN users u ON t.user_id = u.id LEFT JOIN prizes p ON t.prize_id = p.id LEFT JOIN users v ON t.validated_by = v.id `; const params = []; let paramCount = 1; const whereClauses = []; // Filtre par type de lot if (prizeType) { whereClauses.push(`p.type = $${paramCount++}`); params.push(prizeType); } if (whereClauses.length > 0) { query += ' WHERE ' + whereClauses.join(' AND '); } query += ` ORDER BY t.played_at DESC NULLS LAST, t.created_at DESC LIMIT $${paramCount++} OFFSET $${paramCount}`; params.push(limit, offset); console.log('RequĂȘte SQL:'); console.log(query); console.log('\nParamĂštres:', params); const result = await pool.query(query, params); console.log(`\n✅ RĂ©sultats: ${result.rows.length} tickets\n`); if (result.rows.length > 0) { console.log('Premiers tickets:'); result.rows.slice(0, 5).forEach((t, idx) => { console.log(` ${idx + 1}. ${t.code} - ${t.prize_name} (${t.prize_type})`); }); } else { console.log('❌ Aucun ticket retournĂ© !'); } // Compter le total let countQuery = 'SELECT COUNT(*) FROM tickets t LEFT JOIN prizes p ON t.prize_id = p.id'; const countParams = []; const countWhereClauses = []; let countParamCount = 1; if (prizeType) { countWhereClauses.push(`p.type = $${countParamCount++}`); countParams.push(prizeType); } if (countWhereClauses.length > 0) { countQuery += ' WHERE ' + countWhereClauses.join(' AND '); } const countResult = await pool.query(countQuery, countParams); const total = parseInt(countResult.rows[0].count); console.log(`\n📊 Total dans la base: ${total.toLocaleString('fr-FR')} tickets de type ${prizeType}`); await pool.end(); process.exit(0); } catch (error) { console.error('❌ Erreur:', error.message); console.error(error.stack); await pool.end(); process.exit(1); } } testAdminAPIFilter();