Convert backend to regular directory
This commit is contained in:
67
backend/src/scripts/generateTickets.js
Normal file
67
backend/src/scripts/generateTickets.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const prisma = require("../config/prisma");
|
||||
|
||||
function generateCode(length = 10) {
|
||||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
let code = "";
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
code += chars.charAt(Math.floor(Math.random() * chars.length));
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
async function generateTickets(total = 500000) {
|
||||
const lots = await prisma.lot.findMany({
|
||||
orderBy: { id: "asc" },
|
||||
});
|
||||
|
||||
if (lots.length !== 5) {
|
||||
throw new Error("Les 5 lots doivent être créés avant de générer les tickets.");
|
||||
}
|
||||
|
||||
await prisma.ticket.deleteMany();
|
||||
|
||||
const repartition = [
|
||||
{ lotId: lots[0].id, count: Math.floor(total * 0.6) },
|
||||
{ lotId: lots[1].id, count: Math.floor(total * 0.2) },
|
||||
{ lotId: lots[2].id, count: Math.floor(total * 0.1) },
|
||||
{ lotId: lots[3].id, count: Math.floor(total * 0.06) },
|
||||
{ lotId: lots[4].id, count: total - (Math.floor(total * 0.6) + Math.floor(total * 0.2) + Math.floor(total * 0.1) + Math.floor(total * 0.06)) },
|
||||
];
|
||||
|
||||
const usedCodes = new Set();
|
||||
const tickets = [];
|
||||
|
||||
for (const item of repartition) {
|
||||
for (let i = 0; i < item.count; i++) {
|
||||
let code;
|
||||
|
||||
do {
|
||||
code = generateCode();
|
||||
} while (usedCodes.has(code));
|
||||
|
||||
usedCodes.add(code);
|
||||
|
||||
tickets.push({
|
||||
code,
|
||||
lotId: item.lotId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.ticket.createMany({
|
||||
data: tickets,
|
||||
});
|
||||
|
||||
console.log(`---- ${tickets.length} tickets générés avec succès.----`);
|
||||
}
|
||||
|
||||
generateTickets()
|
||||
.catch((error) => {
|
||||
console.error("Erreur génération tickets :", error);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user