82 lines
2.3 KiB
JavaScript
Executable file
82 lines
2.3 KiB
JavaScript
Executable file
import express from 'express';
|
|
import { createTicket, getTicketStatus, getMatchSeats } from '../controllers/ticketController.js';
|
|
import multer from 'multer';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const router = express.Router();
|
|
|
|
// Configure multer for file uploads
|
|
const storage = multer.diskStorage({
|
|
destination: function (req, file, cb) {
|
|
cb(null, path.join(__dirname, '../uploads'));
|
|
},
|
|
filename: function (req, file, cb) {
|
|
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
|
|
cb(null, uniqueSuffix + path.extname(file.originalname));
|
|
}
|
|
});
|
|
|
|
const upload = multer({
|
|
storage: storage,
|
|
fileFilter: (req, file, cb) => {
|
|
if (file.mimetype === 'application/pdf') {
|
|
cb(null, true);
|
|
} else {
|
|
cb(new Error('Only PDF files are allowed'));
|
|
}
|
|
}
|
|
});
|
|
|
|
// Create a new ticket
|
|
router.post('/', upload.single('pdfFile'), async (req, res) => {
|
|
try {
|
|
const { matchId, name, email, phone, seats, deliveryMethod } = req.body;
|
|
const pdfFile = req.file;
|
|
|
|
if (!matchId || !name || !email || !phone || !seats || !deliveryMethod) {
|
|
return res.status(400).json({ message: 'Missing required fields' });
|
|
}
|
|
|
|
if (deliveryMethod === 'email' && !email) {
|
|
return res.status(400).json({ message: 'Email is required for email delivery' });
|
|
}
|
|
|
|
const ticket = await createTicket({
|
|
matchId,
|
|
name,
|
|
email,
|
|
phone,
|
|
seats: parseInt(seats),
|
|
pdfFile: pdfFile ? pdfFile.filename : null,
|
|
deliveryMethod
|
|
});
|
|
|
|
res.status(201).json(ticket);
|
|
} catch (error) {
|
|
console.error('Error creating ticket:', error);
|
|
res.status(500).json({ message: error.message });
|
|
}
|
|
});
|
|
|
|
// Get ticket status
|
|
router.get('/:ticketId', async (req, res) => {
|
|
try {
|
|
const ticket = await getTicketStatus(req.params.ticketId);
|
|
if (!ticket) {
|
|
return res.status(404).json({ message: 'Ticket not found' });
|
|
}
|
|
res.json(ticket);
|
|
} catch (error) {
|
|
console.error('Error getting ticket status:', error);
|
|
res.status(500).json({ message: error.message });
|
|
}
|
|
});
|
|
|
|
// Get seats for a match
|
|
router.get('/match/:matchId/seats', getMatchSeats);
|
|
|
|
export default router;
|