86 lines
2.8 KiB
JavaScript
86 lines
2.8 KiB
JavaScript
import { pool } from '../db.js';
|
|
|
|
const createTestTicket = async () => {
|
|
try {
|
|
console.log('🎫 Création d\'un ticket de test pour validation employé...\n');
|
|
|
|
// 1. Récupérer un client de test
|
|
const clientResult = await pool.query(`
|
|
SELECT id, email, first_name, last_name
|
|
FROM users
|
|
WHERE role = 'CLIENT'
|
|
LIMIT 1
|
|
`);
|
|
|
|
if (clientResult.rows.length === 0) {
|
|
console.log('❌ Aucun client trouvé.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const client = clientResult.rows[0];
|
|
console.log('👤 Client:', `${client.first_name} ${client.last_name} (${client.email})`);
|
|
|
|
// 2. Récupérer un lot disponible
|
|
const prizeResult = await pool.query(`
|
|
SELECT id, name, type, description
|
|
FROM prizes
|
|
ORDER BY RANDOM()
|
|
LIMIT 1
|
|
`);
|
|
|
|
if (prizeResult.rows.length === 0) {
|
|
console.log('❌ Aucun lot trouvé.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const prize = prizeResult.rows[0];
|
|
console.log('🎁 Lot:', prize.name);
|
|
|
|
// 3. Générer un code unique
|
|
const generateTicketCode = () => {
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
let code = '';
|
|
for (let i = 0; i < 10; i++) {
|
|
code += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
}
|
|
return code;
|
|
};
|
|
|
|
let ticketCode = generateTicketCode();
|
|
|
|
// 4. Créer le ticket en statut PENDING
|
|
const ticketResult = await pool.query(`
|
|
INSERT INTO tickets (code, user_id, prize_id, status, played_at)
|
|
VALUES ($1, $2, $3, $4, NOW())
|
|
RETURNING id, code, status, played_at
|
|
`, [
|
|
ticketCode,
|
|
client.id,
|
|
prize.id,
|
|
'PENDING'
|
|
]);
|
|
|
|
const ticket = ticketResult.rows[0];
|
|
|
|
console.log('\n✅ Ticket de test créé avec succès!\n');
|
|
console.log('═══════════════════════════════════════════════════');
|
|
console.log('📋 INFORMATIONS DU TICKET');
|
|
console.log('═══════════════════════════════════════════════════');
|
|
console.log('🔑 Code: ', ticket.code);
|
|
console.log('👤 Client: ', `${client.first_name} ${client.last_name}`);
|
|
console.log('📧 Email: ', client.email);
|
|
console.log('🎁 Lot gagné: ', prize.name);
|
|
console.log('📊 Statut: ', ticket.status);
|
|
console.log('═══════════════════════════════════════════════════');
|
|
console.log('\n💡 Utilisez ce code pour tester la validation employé:\n');
|
|
console.log(` ${ticket.code}\n`);
|
|
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error('❌ Erreur:', error.message);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
createTestTicket();
|