feat: add event creation script with random slugs

Add reusable script for creating events with secure random slugs.

- Create backend/scripts/create-event.js with CLI interface
- Add npm script 'event:create' to package.json
- Generate 8-character random hex slugs using crypto
- Include date validation and error handling
- Display event details and URL after creation
This commit is contained in:
Radosław Gierwiało
2025-12-06 17:50:57 +01:00
parent e905c78f52
commit 1ff70a9f7f
2 changed files with 72 additions and 1 deletions

View File

@@ -14,7 +14,8 @@
"prisma:seed:dev": "node prisma/seed.development.js",
"prisma:seed:prod": "node prisma/seed.production.js",
"prisma:studio": "prisma studio",
"cli": "node src/cli/index.js"
"cli": "node src/cli/index.js",
"event:create": "node scripts/create-event.js"
},
"keywords": [
"webrtc",

View File

@@ -0,0 +1,70 @@
#!/usr/bin/env node
const { PrismaClient } = require('@prisma/client');
const crypto = require('crypto');
const prisma = new PrismaClient();
/**
* Create a new event with a random slug
* Usage: node scripts/create-event.js <name> <location> <startDate> <endDate>
* Example: node scripts/create-event.js "Santa Swing" "Kraków, Poland" "2025-12-12" "2025-12-15"
*/
async function createEvent() {
try {
// Parse command-line arguments
const args = process.argv.slice(2);
if (args.length < 4) {
console.error('❌ Usage: node scripts/create-event.js <name> <location> <startDate> <endDate>');
console.error(' Example: node scripts/create-event.js "Santa Swing" "Kraków, Poland" "2025-12-12" "2025-12-15"');
process.exit(1);
}
const [name, location, startDate, endDate] = args;
// Validate dates
const start = new Date(startDate);
const end = new Date(endDate);
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
console.error('❌ Invalid date format. Use YYYY-MM-DD format.');
process.exit(1);
}
if (end < start) {
console.error('❌ End date must be after start date.');
process.exit(1);
}
// Generate random slug (8 characters alphanumeric)
const slug = crypto.randomBytes(4).toString('hex');
// Create event
const event = await prisma.event.create({
data: {
name,
slug,
location,
startDate: start,
endDate: end,
},
});
console.log('✅ Event created successfully:');
console.log(` ID: ${event.id}`);
console.log(` Name: ${event.name}`);
console.log(` Slug: ${event.slug}`);
console.log(` Location: ${event.location}`);
console.log(` Dates: ${event.startDate.toISOString().split('T')[0]}${event.endDate.toISOString().split('T')[0]}`);
console.log(`\n URL: ${process.env.FRONTEND_URL || 'http://localhost'}/events/${event.slug}`);
} catch (error) {
console.error('❌ Error creating event:', error.message);
process.exit(1);
} finally {
await prisma.$disconnect();
}
}
createEvent();