we're about to port the chrome-extension. everything else mostly works
This commit is contained in:
@@ -37,8 +37,9 @@ router.get('/active', (req, res) => {
|
||||
LIMIT 1
|
||||
`).get();
|
||||
|
||||
// Return null instead of 404 when no active session
|
||||
if (!session) {
|
||||
return res.status(404).json({ error: 'No active session found' });
|
||||
return res.json({ session: null, message: 'No active session' });
|
||||
}
|
||||
|
||||
res.json(session);
|
||||
@@ -114,6 +115,13 @@ router.post('/:id/close', authenticateToken, (req, res) => {
|
||||
return res.status(400).json({ error: 'Session is already closed' });
|
||||
}
|
||||
|
||||
// Set all 'playing' games to 'played' before closing
|
||||
db.prepare(`
|
||||
UPDATE session_games
|
||||
SET status = 'played'
|
||||
WHERE session_id = ? AND status = 'playing'
|
||||
`).run(req.params.id);
|
||||
|
||||
const stmt = db.prepare(`
|
||||
UPDATE sessions
|
||||
SET is_active = 0, closed_at = CURRENT_TIMESTAMP, notes = COALESCE(?, notes)
|
||||
@@ -129,6 +137,33 @@ router.post('/:id/close', authenticateToken, (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Delete session (admin only)
|
||||
router.delete('/:id', authenticateToken, (req, res) => {
|
||||
try {
|
||||
const session = db.prepare('SELECT * FROM sessions WHERE id = ?').get(req.params.id);
|
||||
|
||||
if (!session) {
|
||||
return res.status(404).json({ error: 'Session not found' });
|
||||
}
|
||||
|
||||
// Prevent deletion of active sessions
|
||||
if (session.is_active === 1) {
|
||||
return res.status(400).json({ error: 'Cannot delete an active session. Please close it first.' });
|
||||
}
|
||||
|
||||
// Delete related data first (cascade)
|
||||
db.prepare('DELETE FROM chat_logs WHERE session_id = ?').run(req.params.id);
|
||||
db.prepare('DELETE FROM session_games WHERE session_id = ?').run(req.params.id);
|
||||
|
||||
// Delete the session
|
||||
db.prepare('DELETE FROM sessions WHERE id = ?').run(req.params.id);
|
||||
|
||||
res.json({ message: 'Session deleted successfully', sessionId: parseInt(req.params.id) });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Get games played in a session
|
||||
router.get('/:id/games', (req, res) => {
|
||||
try {
|
||||
@@ -180,10 +215,20 @@ router.post('/:id/games', authenticateToken, (req, res) => {
|
||||
return res.status(404).json({ error: 'Game not found' });
|
||||
}
|
||||
|
||||
// Add game to session
|
||||
// Set all current 'playing' games to 'played' (except skipped ones)
|
||||
db.prepare(`
|
||||
UPDATE session_games
|
||||
SET status = CASE
|
||||
WHEN status = 'skipped' THEN 'skipped'
|
||||
ELSE 'played'
|
||||
END
|
||||
WHERE session_id = ? AND status = 'playing'
|
||||
`).run(req.params.id);
|
||||
|
||||
// Add game to session with 'playing' status
|
||||
const stmt = db.prepare(`
|
||||
INSERT INTO session_games (session_id, game_id, manually_added)
|
||||
VALUES (?, ?, ?)
|
||||
INSERT INTO session_games (session_id, game_id, manually_added, status)
|
||||
VALUES (?, ?, ?, 'playing')
|
||||
`);
|
||||
|
||||
const result = stmt.run(req.params.id, game_id, manually_added ? 1 : 0);
|
||||
@@ -336,5 +381,64 @@ router.post('/:id/chat-import', authenticateToken, (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Update session game status (admin only)
|
||||
router.patch('/:sessionId/games/:gameId/status', authenticateToken, (req, res) => {
|
||||
try {
|
||||
const { status } = req.body;
|
||||
const { sessionId, gameId } = req.params;
|
||||
|
||||
if (!status || !['playing', 'played', 'skipped'].includes(status)) {
|
||||
return res.status(400).json({ error: 'Invalid status. Must be playing, played, or skipped' });
|
||||
}
|
||||
|
||||
// If setting to 'playing', first set all other games in session to 'played' or keep as 'skipped'
|
||||
if (status === 'playing') {
|
||||
db.prepare(`
|
||||
UPDATE session_games
|
||||
SET status = CASE
|
||||
WHEN status = 'skipped' THEN 'skipped'
|
||||
ELSE 'played'
|
||||
END
|
||||
WHERE session_id = ? AND status = 'playing'
|
||||
`).run(sessionId);
|
||||
}
|
||||
|
||||
// Update the specific game
|
||||
const result = db.prepare(`
|
||||
UPDATE session_games
|
||||
SET status = ?
|
||||
WHERE session_id = ? AND id = ?
|
||||
`).run(status, sessionId, gameId);
|
||||
|
||||
if (result.changes === 0) {
|
||||
return res.status(404).json({ error: 'Session game not found' });
|
||||
}
|
||||
|
||||
res.json({ message: 'Status updated successfully', status });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Delete session game (admin only)
|
||||
router.delete('/:sessionId/games/:gameId', authenticateToken, (req, res) => {
|
||||
try {
|
||||
const { sessionId, gameId } = req.params;
|
||||
|
||||
const result = db.prepare(`
|
||||
DELETE FROM session_games
|
||||
WHERE session_id = ? AND id = ?
|
||||
`).run(sessionId, gameId);
|
||||
|
||||
if (result.changes === 0) {
|
||||
return res.status(404).json({ error: 'Session game not found' });
|
||||
}
|
||||
|
||||
res.json({ message: 'Game removed from session successfully' });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user