feat: add competition heats system backend

- Add 3 new database tables: divisions, competition_types, event_user_heats
- Add seed data for 6 divisions (NEW, NOV, INT, ADV, ALL, CHA) and 2 competition types (J&J, STR)
- Add API endpoints for divisions and competition types
- Add heats management endpoints in events route (POST/GET/DELETE)
- Implement unique constraint: cannot have same role in same division+competition type
- Add participant verification before allowing heats management
- Support heat numbers 1-9 with optional Leader/Follower role
This commit is contained in:
Radosław Gierwiało
2025-11-14 15:32:40 +01:00
parent 0e5dc34cbf
commit 02d3d7ac42
7 changed files with 584 additions and 8 deletions

View File

@@ -0,0 +1,27 @@
const express = require('express');
const { prisma } = require('../utils/db');
const router = express.Router();
// GET /api/competition-types - List all competition types (public)
router.get('/', async (req, res, next) => {
try {
const competitionTypes = await prisma.competitionType.findMany({
select: {
id: true,
name: true,
abbreviation: true,
},
});
res.json({
success: true,
count: competitionTypes.length,
data: competitionTypes,
});
} catch (error) {
next(error);
}
});
module.exports = router;

View File

@@ -0,0 +1,31 @@
const express = require('express');
const { prisma } = require('../utils/db');
const router = express.Router();
// GET /api/divisions - List all divisions (public)
router.get('/', async (req, res, next) => {
try {
const divisions = await prisma.division.findMany({
orderBy: {
displayOrder: 'asc',
},
select: {
id: true,
name: true,
abbreviation: true,
displayOrder: true,
},
});
res.json({
success: true,
count: divisions.length,
data: divisions,
});
} catch (error) {
next(error);
}
});
module.exports = router;

View File

@@ -448,4 +448,336 @@ router.delete('/:slug/leave', authenticate, async (req, res, next) => {
}
});
// POST /api/events/:slug/heats - Add/update user's heats for event
router.post('/:slug/heats', authenticate, async (req, res, next) => {
try {
const { slug } = req.params;
const userId = req.user.id;
const { heats } = req.body;
// Validation
if (!Array.isArray(heats) || heats.length === 0) {
return res.status(400).json({
success: false,
error: 'Heats must be a non-empty array',
});
}
// Validate heat structure
for (const heat of heats) {
if (!heat.divisionId || !heat.competitionTypeId || !heat.heatNumber) {
return res.status(400).json({
success: false,
error: 'Each heat must have divisionId, competitionTypeId, and heatNumber',
});
}
if (heat.heatNumber < 1 || heat.heatNumber > 9) {
return res.status(400).json({
success: false,
error: 'Heat number must be between 1 and 9',
});
}
if (heat.role && !['Leader', 'Follower'].includes(heat.role)) {
return res.status(400).json({
success: false,
error: 'Role must be either Leader or Follower',
});
}
}
// Find event
const event = await prisma.event.findUnique({
where: { slug },
select: { id: true },
});
if (!event) {
return res.status(404).json({
success: false,
error: 'Event not found',
});
}
// Check if user is participant
const participant = await prisma.eventParticipant.findUnique({
where: {
userId_eventId: {
userId,
eventId: event.id,
},
},
});
if (!participant) {
return res.status(403).json({
success: false,
error: 'You must be a participant of this event',
});
}
// Delete existing heats and create new ones (transaction)
const result = await prisma.$transaction(async (tx) => {
// Delete existing heats for this user and event
await tx.eventUserHeat.deleteMany({
where: {
userId,
eventId: event.id,
},
});
// Create new heats
const created = await tx.eventUserHeat.createMany({
data: heats.map((heat) => ({
userId,
eventId: event.id,
divisionId: heat.divisionId,
competitionTypeId: heat.competitionTypeId,
heatNumber: heat.heatNumber,
role: heat.role || null,
})),
});
// Fetch created heats with relations
const userHeats = await tx.eventUserHeat.findMany({
where: {
userId,
eventId: event.id,
},
include: {
division: {
select: {
id: true,
name: true,
abbreviation: true,
},
},
competitionType: {
select: {
id: true,
name: true,
abbreviation: true,
},
},
},
});
return userHeats;
});
res.json({
success: true,
count: result.length,
data: result,
});
} catch (error) {
// Handle unique constraint violation
if (error.code === 'P2002') {
return res.status(400).json({
success: false,
error: 'Cannot have duplicate heats with same role in same division and competition type',
});
}
next(error);
}
});
// GET /api/events/:slug/heats/me - Get current user's heats
router.get('/:slug/heats/me', authenticate, async (req, res, next) => {
try {
const { slug } = req.params;
const userId = req.user.id;
// Find event
const event = await prisma.event.findUnique({
where: { slug },
select: { id: true },
});
if (!event) {
return res.status(404).json({
success: false,
error: 'Event not found',
});
}
// Get user's heats
const heats = await prisma.eventUserHeat.findMany({
where: {
userId,
eventId: event.id,
},
include: {
division: {
select: {
id: true,
name: true,
abbreviation: true,
},
},
competitionType: {
select: {
id: true,
name: true,
abbreviation: true,
},
},
},
orderBy: [
{ competitionTypeId: 'asc' },
{ divisionId: 'asc' },
{ heatNumber: 'asc' },
],
});
res.json({
success: true,
count: heats.length,
data: heats,
});
} catch (error) {
next(error);
}
});
// GET /api/events/:slug/heats/all - Get all users' heats for event
router.get('/:slug/heats/all', authenticate, async (req, res, next) => {
try {
const { slug } = req.params;
// Find event
const event = await prisma.event.findUnique({
where: { slug },
select: { id: true },
});
if (!event) {
return res.status(404).json({
success: false,
error: 'Event not found',
});
}
// Get all heats with user info
const heats = await prisma.eventUserHeat.findMany({
where: {
eventId: event.id,
},
include: {
user: {
select: {
id: true,
username: true,
avatar: true,
},
},
division: {
select: {
id: true,
name: true,
abbreviation: true,
},
},
competitionType: {
select: {
id: true,
name: true,
abbreviation: true,
},
},
},
});
// Group by user
const userHeatsMap = new Map();
for (const heat of heats) {
const userId = heat.user.id;
if (!userHeatsMap.has(userId)) {
userHeatsMap.set(userId, {
userId: heat.user.id,
username: heat.user.username,
avatar: heat.user.avatar,
heats: [],
});
}
userHeatsMap.get(userId).heats.push({
id: heat.id,
divisionId: heat.divisionId,
division: heat.division,
competitionTypeId: heat.competitionTypeId,
competitionType: heat.competitionType,
heatNumber: heat.heatNumber,
role: heat.role,
});
}
const result = Array.from(userHeatsMap.values());
res.json({
success: true,
count: result.length,
data: result,
});
} catch (error) {
next(error);
}
});
// DELETE /api/events/:slug/heats/:id - Delete specific heat
router.delete('/:slug/heats/:id', authenticate, async (req, res, next) => {
try {
const { slug, id } = req.params;
const userId = req.user.id;
// Find event
const event = await prisma.event.findUnique({
where: { slug },
select: { id: true },
});
if (!event) {
return res.status(404).json({
success: false,
error: 'Event not found',
});
}
// Find heat
const heat = await prisma.eventUserHeat.findUnique({
where: { id: parseInt(id) },
});
if (!heat) {
return res.status(404).json({
success: false,
error: 'Heat not found',
});
}
// Check ownership
if (heat.userId !== userId) {
return res.status(403).json({
success: false,
error: 'You can only delete your own heats',
});
}
// Delete heat
await prisma.eventUserHeat.delete({
where: { id: parseInt(id) },
});
res.json({
success: true,
message: 'Heat deleted successfully',
});
} catch (error) {
next(error);
}
});
module.exports = router;