Files
spotlightcam/backend/src/routes/events.js

183 lines
4.2 KiB
JavaScript
Raw Normal View History

const express = require('express');
const { prisma } = require('../utils/db');
const { authenticate } = require('../middleware/auth');
const router = express.Router();
// GET /api/events - List all events
router.get('/', authenticate, async (req, res, next) => {
try {
const userId = req.user.id;
// Fetch all events with participation info
const events = await prisma.event.findMany({
select: {
id: true,
slug: true,
name: true,
location: true,
startDate: true,
endDate: true,
worldsdcId: true,
participantsCount: true,
description: true,
createdAt: true,
participants: {
where: {
userId: userId,
},
select: {
joinedAt: true,
},
},
},
});
// Transform data and add isJoined flag
const eventsWithJoinedStatus = events.map(event => ({
id: event.id,
slug: event.slug,
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);
});
res.json({
success: true,
count: eventsWithJoinedStatus.length,
data: eventsWithJoinedStatus,
});
} catch (error) {
next(error);
}
});
// GET /api/events/:slug - Get event by slug
router.get('/:slug', async (req, res, next) => {
try {
const { slug } = req.params;
const event = await prisma.event.findUnique({
where: {
slug: slug,
},
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);
}
});
// GET /api/events/:slug/messages - Get event chat messages with pagination
router.get('/:slug/messages', authenticate, async (req, res, next) => {
try {
const { slug } = req.params;
const { before, limit = 20 } = req.query;
// Find event by slug
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 event chat room
const chatRoom = await prisma.chatRoom.findFirst({
where: {
eventId: event.id,
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);
}
});
module.exports = router;