2025-11-12 21:56:11 +01:00
|
|
|
const express = require('express');
|
|
|
|
|
const { prisma } = require('../utils/db');
|
2025-11-13 20:16:58 +01:00
|
|
|
const { authenticate } = require('../middleware/auth');
|
2025-11-12 21:56:11 +01:00
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
// GET /api/events - List all events
|
2025-11-13 21:18:15 +01:00
|
|
|
router.get('/', authenticate, async (req, res, next) => {
|
2025-11-12 21:56:11 +01:00
|
|
|
try {
|
2025-11-13 21:18:15 +01:00
|
|
|
const userId = req.user.id;
|
|
|
|
|
|
|
|
|
|
// Fetch all events with participation info
|
2025-11-12 21:56:11 +01:00
|
|
|
const events = await prisma.event.findMany({
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
location: true,
|
|
|
|
|
startDate: true,
|
|
|
|
|
endDate: true,
|
|
|
|
|
worldsdcId: true,
|
|
|
|
|
participantsCount: true,
|
|
|
|
|
description: true,
|
|
|
|
|
createdAt: true,
|
2025-11-13 21:18:15 +01:00
|
|
|
participants: {
|
|
|
|
|
where: {
|
|
|
|
|
userId: userId,
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
joinedAt: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-11-12 21:56:11 +01:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-13 21:18:15 +01:00
|
|
|
// Transform data and add isJoined flag
|
|
|
|
|
const eventsWithJoinedStatus = events.map(event => ({
|
|
|
|
|
id: event.id,
|
|
|
|
|
name: event.name,
|
|
|
|
|
location: event.location,
|
|
|
|
|
startDate: event.startDate,
|
|
|
|
|
endDate: event.endDate,
|
|
|
|
|
worldsdcId: event.worldsdcId,
|
|
|
|
|
participantsCount: event.participantsCount,
|
|
|
|
|
description: event.description,
|
|
|
|
|
createdAt: event.createdAt,
|
|
|
|
|
isJoined: event.participants.length > 0,
|
|
|
|
|
joinedAt: event.participants[0]?.joinedAt || null,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Sort: joined events first, then by start date
|
|
|
|
|
eventsWithJoinedStatus.sort((a, b) => {
|
|
|
|
|
// First, sort by joined status (joined events first)
|
|
|
|
|
if (a.isJoined && !b.isJoined) return -1;
|
|
|
|
|
if (!a.isJoined && b.isJoined) return 1;
|
|
|
|
|
|
|
|
|
|
// Then sort by start date
|
|
|
|
|
return new Date(a.startDate) - new Date(b.startDate);
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-12 21:56:11 +01:00
|
|
|
res.json({
|
|
|
|
|
success: true,
|
2025-11-13 21:18:15 +01:00
|
|
|
count: eventsWithJoinedStatus.length,
|
|
|
|
|
data: eventsWithJoinedStatus,
|
2025-11-12 21:56:11 +01:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// GET /api/events/:id - Get event by ID
|
|
|
|
|
router.get('/:id', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
|
|
const event = await prisma.event.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: parseInt(id),
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
chatRooms: true,
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
matches: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!event) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
|
|
|
|
error: 'Event not found',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: event,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-13 20:16:58 +01:00
|
|
|
// GET /api/events/:eventId/messages - Get event chat messages with pagination
|
|
|
|
|
router.get('/:eventId/messages', authenticate, async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const { eventId } = req.params;
|
|
|
|
|
const { before, limit = 20 } = req.query;
|
|
|
|
|
|
|
|
|
|
// Find event chat room
|
|
|
|
|
const chatRoom = await prisma.chatRoom.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
eventId: parseInt(eventId),
|
|
|
|
|
type: 'event',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!chatRoom) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
|
|
|
|
error: 'Chat room not found',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build query with pagination
|
|
|
|
|
const where = { roomId: chatRoom.id };
|
|
|
|
|
if (before) {
|
|
|
|
|
where.id = { lt: parseInt(before) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const messages = await prisma.message.findMany({
|
|
|
|
|
where,
|
|
|
|
|
include: {
|
|
|
|
|
user: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
username: true,
|
|
|
|
|
avatar: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
orderBy: { createdAt: 'desc' },
|
|
|
|
|
take: parseInt(limit),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Return in chronological order (oldest first)
|
|
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: messages.reverse().map(msg => ({
|
|
|
|
|
id: msg.id,
|
|
|
|
|
roomId: msg.roomId,
|
|
|
|
|
userId: msg.user.id,
|
|
|
|
|
username: msg.user.username,
|
|
|
|
|
avatar: msg.user.avatar,
|
|
|
|
|
content: msg.content,
|
|
|
|
|
type: msg.type,
|
|
|
|
|
createdAt: msg.createdAt,
|
|
|
|
|
})),
|
|
|
|
|
hasMore: messages.length === parseInt(limit),
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-12 21:56:11 +01:00
|
|
|
module.exports = router;
|