Added settings for user data deletion

This commit is contained in:
Brian McGonagill 2026-03-21 12:23:20 -05:00
parent 59e15cfde8
commit f49490042a
35 changed files with 2758 additions and 499 deletions

View file

@ -1,8 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const index_js_1 = require("../index.js");
const auth_js_1 = require("../middleware/auth.js");
const index_1 = require("../index");
const auth_1 = require("../middleware/auth");
const uuid_1 = require("uuid");
const router = (0, express_1.Router)();
router.get('/', async (req, res) => {
@ -17,11 +17,11 @@ router.get('/', async (req, res) => {
if (search) {
where.name = { contains: search, mode: 'insensitive' };
}
const games = await index_js_1.prisma.game.findMany({
const games = await index_1.prisma.game.findMany({
where,
include: {
gameMaster: { select: { id: true, name: true } },
_count: { select: { teams: true, legs: true } }
_count: { select: { teams: true, routes: true } }
},
orderBy: { createdAt: 'desc' }
});
@ -32,12 +32,12 @@ router.get('/', async (req, res) => {
res.status(500).json({ error: 'Failed to list games' });
}
});
router.get('/my-games', auth_js_1.authenticate, async (req, res) => {
router.get('/my-games', auth_1.authenticate, async (req, res) => {
try {
const games = await index_js_1.prisma.game.findMany({
const games = await index_1.prisma.game.findMany({
where: { gameMasterId: req.user.id },
include: {
_count: { select: { teams: true, legs: true } }
_count: { select: { teams: true, routes: true } }
},
orderBy: { createdAt: 'desc' }
});
@ -51,15 +51,27 @@ router.get('/my-games', auth_js_1.authenticate, async (req, res) => {
router.get('/:id', async (req, res) => {
try {
const { id } = req.params;
const game = await index_js_1.prisma.game.findUnique({
where: { id },
const game = await index_1.prisma.game.findUnique({
where: { id: id },
include: {
gameMaster: { select: { id: true, name: true } },
legs: { orderBy: { sequenceNumber: 'asc' } },
routes: {
include: {
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
}
},
teams: {
include: {
members: { include: { user: { select: { id: true, name: true, email: true } } } },
currentLeg: true
teamRoutes: {
include: {
route: {
include: {
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
}
}
}
}
}
}
}
@ -71,16 +83,20 @@ router.get('/:id', async (req, res) => {
if (game.visibility === 'PRIVATE' && !isOwner) {
return res.status(403).json({ error: 'Access denied' });
}
res.json(game);
const result = {
...game,
rules: game.rules ? JSON.parse(game.rules) : []
};
res.json(result);
}
catch (error) {
console.error('Get game error:', error);
res.status(500).json({ error: 'Failed to get game' });
}
});
router.post('/', auth_js_1.authenticate, async (req, res) => {
router.post('/', auth_1.authenticate, async (req, res) => {
try {
const { name, description, prizeDetails, visibility, startDate, locationLat, locationLng, searchRadius, timeLimitPerLeg, timeDeductionPenalty } = req.body;
const { name, description, prizeDetails, visibility, startDate, locationLat, locationLng, searchRadius, rules, randomizeRoutes } = req.body;
if (!name) {
return res.status(400).json({ error: 'Name is required' });
}
@ -88,7 +104,7 @@ router.post('/', auth_js_1.authenticate, async (req, res) => {
return res.status(400).json({ error: 'Start date must be in the future' });
}
const inviteCode = (0, uuid_1.v4)().slice(0, 8);
const game = await index_js_1.prisma.game.create({
const game = await index_1.prisma.game.create({
data: {
name,
description,
@ -98,8 +114,8 @@ router.post('/', auth_js_1.authenticate, async (req, res) => {
locationLat,
locationLng,
searchRadius,
timeLimitPerLeg,
timeDeductionPenalty,
rules: rules ? JSON.stringify(rules) : null,
randomizeRoutes: randomizeRoutes || false,
gameMasterId: req.user.id,
inviteCode
}
@ -111,19 +127,19 @@ router.post('/', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to create game' });
}
});
router.put('/:id', auth_js_1.authenticate, async (req, res) => {
router.put('/:id', auth_1.authenticate, async (req, res) => {
try {
const { id } = req.params;
const { name, description, prizeDetails, visibility, startDate, locationLat, locationLng, searchRadius, timeLimitPerLeg, timeDeductionPenalty, status } = req.body;
const game = await index_js_1.prisma.game.findUnique({ where: { id } });
const id = req.params.id;
const { name, description, prizeDetails, visibility, startDate, locationLat, locationLng, searchRadius, rules, status, randomizeRoutes } = req.body;
const game = await index_1.prisma.game.findUnique({ where: { id } });
if (!game) {
return res.status(404).json({ error: 'Game not found' });
}
if (game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
const updated = await index_js_1.prisma.game.update({
where: { id },
const updated = await index_1.prisma.game.update({
where: { id: id },
data: {
name,
description,
@ -133,8 +149,8 @@ router.put('/:id', auth_js_1.authenticate, async (req, res) => {
locationLat,
locationLng,
searchRadius,
timeLimitPerLeg,
timeDeductionPenalty,
rules: rules ? JSON.stringify(rules) : undefined,
randomizeRoutes,
status
}
});
@ -145,17 +161,17 @@ router.put('/:id', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to update game' });
}
});
router.delete('/:id', auth_js_1.authenticate, async (req, res) => {
router.delete('/:id', auth_1.authenticate, async (req, res) => {
try {
const { id } = req.params;
const game = await index_js_1.prisma.game.findUnique({ where: { id } });
const id = req.params.id;
const game = await index_1.prisma.game.findUnique({ where: { id } });
if (!game) {
return res.status(404).json({ error: 'Game not found' });
}
if (game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
await index_js_1.prisma.game.delete({ where: { id } });
await index_1.prisma.game.delete({ where: { id } });
res.json({ message: 'Game deleted' });
}
catch (error) {
@ -163,12 +179,12 @@ router.delete('/:id', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to delete game' });
}
});
router.post('/:id/publish', auth_js_1.authenticate, async (req, res) => {
router.post('/:id/publish', auth_1.authenticate, async (req, res) => {
try {
const { id } = req.params;
const game = await index_js_1.prisma.game.findUnique({
where: { id },
include: { legs: true }
const game = await index_1.prisma.game.findUnique({
where: { id: id },
include: { routes: { include: { routeLegs: true } } }
});
if (!game) {
return res.status(404).json({ error: 'Game not found' });
@ -176,11 +192,34 @@ router.post('/:id/publish', auth_js_1.authenticate, async (req, res) => {
if (game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
if (game.legs.length === 0) {
return res.status(400).json({ error: 'Game must have at least one leg' });
if (!game.routes || game.routes.length === 0) {
return res.status(400).json({ error: 'Game must have at least one route' });
}
const updated = await index_js_1.prisma.game.update({
where: { id },
const hasValidRoute = game.routes.some(route => route.routeLegs.length > 0);
if (!hasValidRoute) {
return res.status(400).json({ error: 'At least one route must have legs' });
}
if (game.randomizeRoutes) {
const teams = await index_1.prisma.team.findMany({
where: { gameId: id }
});
const shuffledRoutes = [...game.routes].sort(() => Math.random() - 0.5);
for (let i = 0; i < teams.length; i++) {
const routeIndex = i % game.routes.length;
await index_1.prisma.teamRoute.upsert({
where: { teamId: teams[i].id },
create: {
teamId: teams[i].id,
routeId: shuffledRoutes[routeIndex].id
},
update: {
routeId: shuffledRoutes[routeIndex].id
}
});
}
}
const updated = await index_1.prisma.game.update({
where: { id: id },
data: { status: 'LIVE' }
});
res.json(updated);
@ -190,17 +229,17 @@ router.post('/:id/publish', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to publish game' });
}
});
router.post('/:id/end', auth_js_1.authenticate, async (req, res) => {
router.post('/:id/end', auth_1.authenticate, async (req, res) => {
try {
const { id } = req.params;
const game = await index_js_1.prisma.game.findUnique({ where: { id } });
const id = req.params.id;
const game = await index_1.prisma.game.findUnique({ where: { id } });
if (!game) {
return res.status(404).json({ error: 'Game not found' });
}
if (game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
const updated = await index_js_1.prisma.game.update({
const updated = await index_1.prisma.game.update({
where: { id },
data: { status: 'ENDED' }
});
@ -211,10 +250,52 @@ router.post('/:id/end', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to end game' });
}
});
router.post('/:id/archive', auth_1.authenticate, async (req, res) => {
try {
const id = req.params.id;
const game = await index_1.prisma.game.findUnique({ where: { id } });
if (!game) {
return res.status(404).json({ error: 'Game not found' });
}
if (game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
const updated = await index_1.prisma.game.update({
where: { id },
data: { status: 'ARCHIVED' }
});
res.json(updated);
}
catch (error) {
console.error('Archive game error:', error);
res.status(500).json({ error: 'Failed to archive game' });
}
});
router.post('/:id/unarchive', auth_1.authenticate, async (req, res) => {
try {
const id = req.params.id;
const game = await index_1.prisma.game.findUnique({ where: { id } });
if (!game) {
return res.status(404).json({ error: 'Game not found' });
}
if (game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
const updated = await index_1.prisma.game.update({
where: { id },
data: { status: 'ENDED' }
});
res.json(updated);
}
catch (error) {
console.error('Unarchive game error:', error);
res.status(500).json({ error: 'Failed to unarchive game' });
}
});
router.get('/:id/invite', async (req, res) => {
try {
const { id } = req.params;
const game = await index_js_1.prisma.game.findUnique({ where: { id } });
const id = req.params.id;
const game = await index_1.prisma.game.findUnique({ where: { id } });
if (!game) {
return res.status(404).json({ error: 'Game not found' });
}
@ -230,8 +311,8 @@ router.get('/:id/invite', async (req, res) => {
});
router.get('/invite/:code', async (req, res) => {
try {
const { code } = req.params;
const game = await index_js_1.prisma.game.findUnique({
const code = req.params.code;
const game = await index_1.prisma.game.findUnique({
where: { inviteCode: code },
include: { gameMaster: { select: { id: true, name: true } } }
});