the-tip-top-backend/scripts/create-test-pending-ticket.js
2025-11-17 23:47:54 +01:00

91 lines
3.2 KiB
JavaScript

import { pool } from '../db.js';
const createTestPendingTicket = async () => {
try {
console.log('🎫 Création d\'un ticket test en attente de validation...\n');
// Trouver un utilisateur client
const clientResult = await pool.query(`
SELECT id, first_name, last_name, email
FROM users
WHERE role = 'CLIENT'
LIMIT 1
`);
if (clientResult.rows.length === 0) {
console.log('❌ Aucun client trouvé dans la base. Création d\'un client test...\n');
// Créer un client test
const newClient = await pool.query(`
INSERT INTO users (email, password, first_name, last_name, phone, role, is_verified)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, first_name, last_name, email
`, [
'test.client@example.com',
'$2a$10$abcdefghijklmnopqrstuvwxyz', // dummy hash
'Jean',
'Dupont',
'0612345678',
'CLIENT',
true
]);
const client = newClient.rows[0];
console.log(`✅ Client créé: ${client.first_name} ${client.last_name} (${client.email})\n`);
}
// Re-sélectionner le client
const client = clientResult.rows.length > 0
? clientResult.rows[0]
: (await pool.query('SELECT id, first_name, last_name, email FROM users WHERE role = $1 LIMIT 1', ['CLIENT'])).rows[0];
// Trouver un ticket disponible (status NULL)
const ticketResult = await pool.query(`
SELECT t.id, t.code, p.name as prize_name, p.type as prize_type
FROM tickets t
JOIN prizes p ON t.prize_id = p.id
WHERE t.user_id IS NULL AND t.status IS NULL
LIMIT 1
`);
if (ticketResult.rows.length === 0) {
console.log('❌ Aucun ticket disponible trouvé');
await pool.end();
process.exit(1);
}
const ticket = ticketResult.rows[0];
// Simuler qu'un client a joué ce ticket
await pool.query(`
UPDATE tickets
SET user_id = $1,
status = 'PENDING',
played_at = NOW()
WHERE id = $2
`, [client.id, ticket.id]);
console.log('✅ Ticket test créé avec succès!\n');
console.log('═══════════════════════════════════════════════════');
console.log('📋 DÉTAILS DU TICKET TEST:');
console.log('═══════════════════════════════════════════════════');
console.log(`🎫 Code ticket: ${ticket.code}`);
console.log(`👤 Client: ${client.first_name} ${client.last_name}`);
console.log(`📧 Email: ${client.email}`);
console.log(`🎁 Lot gagné: ${ticket.prize_name}`);
console.log(`📊 Statut: PENDING (en attente de validation)`);
console.log('═══════════════════════════════════════════════════\n');
console.log('💡 Ce ticket devrait maintenant apparaître dans l\'interface employé\n');
await pool.end();
process.exit(0);
} catch (error) {
console.error('❌ Erreur:', error.message);
await pool.end();
process.exit(1);
}
};
createTestPendingTicket();