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,18 +1,26 @@
"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 router = (0, express_1.Router)();
router.get('/game/:gameId', async (req, res) => {
try {
const { gameId } = req.params;
const teams = await index_js_1.prisma.team.findMany({
const teams = await index_1.prisma.team.findMany({
where: { gameId },
include: {
members: { include: { user: { select: { id: true, name: true, email: true } } } },
captain: { select: { id: true, name: true } },
currentLeg: true
teamRoutes: {
include: {
route: {
include: {
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
}
}
}
}
},
orderBy: { createdAt: 'asc' }
});
@ -23,11 +31,11 @@ router.get('/game/:gameId', async (req, res) => {
res.status(500).json({ error: 'Failed to get teams' });
}
});
router.post('/game/:gameId', auth_js_1.authenticate, async (req, res) => {
router.post('/game/:gameId', auth_1.authenticate, async (req, res) => {
try {
const { gameId } = req.params;
const { name } = req.body;
const game = await index_js_1.prisma.game.findUnique({
const game = await index_1.prisma.game.findUnique({
where: { id: gameId },
include: { teams: true }
});
@ -37,7 +45,7 @@ router.post('/game/:gameId', auth_js_1.authenticate, async (req, res) => {
if (game.status !== 'DRAFT' && game.status !== 'LIVE') {
return res.status(400).json({ error: 'Cannot join game at this time' });
}
const existingMember = await index_js_1.prisma.teamMember.findFirst({
const existingMember = await index_1.prisma.teamMember.findFirst({
where: {
userId: req.user.id,
team: { gameId }
@ -46,24 +54,33 @@ router.post('/game/:gameId', auth_js_1.authenticate, async (req, res) => {
if (existingMember) {
return res.status(400).json({ error: 'Already in a team for this game' });
}
const team = await index_js_1.prisma.team.create({
const team = await index_1.prisma.team.create({
data: {
gameId,
name,
captainId: req.user.id
}
});
await index_js_1.prisma.teamMember.create({
await index_1.prisma.teamMember.create({
data: {
teamId: team.id,
userId: req.user.id
}
});
const created = await index_js_1.prisma.team.findUnique({
const created = await index_1.prisma.team.findUnique({
where: { id: team.id },
include: {
members: { include: { user: { select: { id: true, name: true, email: true } } } },
captain: { select: { id: true, name: true } }
captain: { select: { id: true, name: true } },
teamRoutes: {
include: {
route: {
include: {
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
}
}
}
}
}
});
res.json(created);
@ -73,10 +90,10 @@ router.post('/game/:gameId', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to create team' });
}
});
router.post('/:teamId/join', auth_js_1.authenticate, async (req, res) => {
router.post('/:teamId/join', auth_1.authenticate, async (req, res) => {
try {
const { teamId } = req.params;
const team = await index_js_1.prisma.team.findUnique({
const team = await index_1.prisma.team.findUnique({
where: { id: teamId },
include: { game: true, members: true }
});
@ -86,7 +103,7 @@ router.post('/:teamId/join', auth_js_1.authenticate, async (req, res) => {
if (team.members.length >= 5) {
return res.status(400).json({ error: 'Team is full (max 5 members)' });
}
const existingMember = await index_js_1.prisma.teamMember.findFirst({
const existingMember = await index_1.prisma.teamMember.findFirst({
where: {
userId: req.user.id,
teamId
@ -95,7 +112,7 @@ router.post('/:teamId/join', auth_js_1.authenticate, async (req, res) => {
if (existingMember) {
return res.status(400).json({ error: 'Already in this team' });
}
const gameMember = await index_js_1.prisma.teamMember.findFirst({
const gameMember = await index_1.prisma.teamMember.findFirst({
where: {
userId: req.user.id,
team: { gameId: team.gameId }
@ -104,7 +121,7 @@ router.post('/:teamId/join', auth_js_1.authenticate, async (req, res) => {
if (gameMember) {
return res.status(400).json({ error: 'Already in another team for this game' });
}
await index_js_1.prisma.teamMember.create({
await index_1.prisma.teamMember.create({
data: {
teamId,
userId: req.user.id
@ -117,10 +134,10 @@ router.post('/:teamId/join', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to join team' });
}
});
router.post('/:teamId/leave', auth_js_1.authenticate, async (req, res) => {
router.post('/:teamId/leave', auth_1.authenticate, async (req, res) => {
try {
const { teamId } = req.params;
const team = await index_js_1.prisma.team.findUnique({
const team = await index_1.prisma.team.findUnique({
where: { id: teamId }
});
if (!team) {
@ -129,7 +146,7 @@ router.post('/:teamId/leave', auth_js_1.authenticate, async (req, res) => {
if (team.captainId === req.user.id) {
return res.status(400).json({ error: 'Captain cannot leave the team' });
}
await index_js_1.prisma.teamMember.deleteMany({
await index_1.prisma.teamMember.deleteMany({
where: {
teamId,
userId: req.user.id
@ -142,14 +159,65 @@ router.post('/:teamId/leave', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to leave team' });
}
});
router.post('/:teamId/advance', auth_js_1.authenticate, async (req, res) => {
router.post('/:teamId/assign-route', auth_1.authenticate, async (req, res) => {
try {
const { teamId } = req.params;
const team = await index_js_1.prisma.team.findUnique({
const { routeId } = req.body;
const team = await index_1.prisma.team.findUnique({
where: { id: teamId },
include: { game: true, teamRoutes: true }
});
if (!team) {
return res.status(404).json({ error: 'Team not found' });
}
if (team.game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
const route = await index_1.prisma.route.findUnique({
where: { id: routeId }
});
if (!route || route.gameId !== team.gameId) {
return res.status(400).json({ error: 'Invalid route for this game' });
}
await index_1.prisma.teamRoute.deleteMany({
where: { teamId }
});
const teamRoute = await index_1.prisma.teamRoute.create({
data: {
teamId,
routeId
},
include: {
route: {
include: {
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
}
}
}
});
res.json(teamRoute);
}
catch (error) {
console.error('Assign route error:', error);
res.status(500).json({ error: 'Failed to assign route' });
}
});
router.post('/:teamId/advance', auth_1.authenticate, async (req, res) => {
try {
const { teamId } = req.params;
const team = await index_1.prisma.team.findUnique({
where: { id: teamId },
include: {
game: { include: { legs: { orderBy: { sequenceNumber: 'asc' } } } },
currentLeg: true
game: true,
teamRoutes: {
include: {
route: {
include: {
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
}
}
}
}
}
});
if (!team) {
@ -158,27 +226,33 @@ router.post('/:teamId/advance', auth_js_1.authenticate, async (req, res) => {
if (team.game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
const legs = team.game.legs;
const currentLeg = team.currentLeg;
let nextLeg = null;
if (currentLeg) {
const currentIndex = legs.findIndex((l) => l.id === currentLeg.id);
if (currentIndex < legs.length - 1) {
nextLeg = legs[currentIndex + 1];
}
const teamRoute = team.teamRoutes[0];
if (!teamRoute) {
return res.status(400).json({ error: 'Team has no assigned route' });
}
else if (legs.length > 0) {
nextLeg = legs[0];
const legs = teamRoute.route.routeLegs;
const currentLegIndex = team.currentLegIndex;
let nextLegIndex = currentLegIndex;
if (currentLegIndex < legs.length - 1) {
nextLegIndex = currentLegIndex + 1;
}
const updated = await index_js_1.prisma.team.update({
const updated = await index_1.prisma.team.update({
where: { id: teamId },
data: {
currentLegId: nextLeg?.id || null,
status: nextLeg ? 'ACTIVE' : 'FINISHED'
currentLegIndex: nextLegIndex,
status: nextLegIndex >= legs.length - 1 ? 'FINISHED' : 'ACTIVE'
},
include: {
members: { include: { user: { select: { id: true, name: true } } } },
currentLeg: true
teamRoutes: {
include: {
route: {
include: {
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
}
}
}
}
}
});
res.json(updated);
@ -188,11 +262,11 @@ router.post('/:teamId/advance', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to advance team' });
}
});
router.post('/:teamId/deduct', auth_js_1.authenticate, async (req, res) => {
router.post('/:teamId/deduct', auth_1.authenticate, async (req, res) => {
try {
const { teamId } = req.params;
const { seconds } = req.body;
const team = await index_js_1.prisma.team.findUnique({
const team = await index_1.prisma.team.findUnique({
where: { id: teamId },
include: { game: true }
});
@ -202,8 +276,8 @@ router.post('/:teamId/deduct', auth_js_1.authenticate, async (req, res) => {
if (team.game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
const deduction = seconds || team.game.timeDeductionPenalty || 60;
const updated = await index_js_1.prisma.team.update({
const deduction = seconds || 60;
const updated = await index_1.prisma.team.update({
where: { id: teamId },
data: { totalTimeDeduction: { increment: deduction } }
});
@ -214,10 +288,10 @@ router.post('/:teamId/deduct', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to deduct time' });
}
});
router.post('/:teamId/disqualify', auth_js_1.authenticate, async (req, res) => {
router.post('/:teamId/disqualify', auth_1.authenticate, async (req, res) => {
try {
const { teamId } = req.params;
const team = await index_js_1.prisma.team.findUnique({
const team = await index_1.prisma.team.findUnique({
where: { id: teamId },
include: { game: true }
});
@ -227,7 +301,7 @@ router.post('/:teamId/disqualify', auth_js_1.authenticate, async (req, res) => {
if (team.game.gameMasterId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
const updated = await index_js_1.prisma.team.update({
const updated = await index_1.prisma.team.update({
where: { id: teamId },
data: { status: 'DISQUALIFIED' }
});
@ -238,23 +312,23 @@ router.post('/:teamId/disqualify', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to disqualify team' });
}
});
router.post('/:teamId/location', auth_js_1.authenticate, async (req, res) => {
router.post('/:teamId/location', auth_1.authenticate, async (req, res) => {
try {
const { teamId } = req.params;
const { lat, lng } = req.body;
const team = await index_js_1.prisma.team.findUnique({
const team = await index_1.prisma.team.findUnique({
where: { id: teamId }
});
if (!team) {
return res.status(404).json({ error: 'Team not found' });
}
const member = await index_js_1.prisma.teamMember.findFirst({
const member = await index_1.prisma.teamMember.findFirst({
where: { teamId, userId: req.user.id }
});
if (!member && team.captainId !== req.user.id) {
return res.status(403).json({ error: 'Not authorized' });
}
const updated = await index_js_1.prisma.team.update({
const updated = await index_1.prisma.team.update({
where: { id: teamId },
data: { lat, lng }
});
@ -265,16 +339,24 @@ router.post('/:teamId/location', auth_js_1.authenticate, async (req, res) => {
res.status(500).json({ error: 'Failed to update location' });
}
});
router.get('/:teamId', auth_js_1.authenticate, async (req, res) => {
router.get('/:teamId', auth_1.authenticate, async (req, res) => {
try {
const { teamId } = req.params;
const team = await index_js_1.prisma.team.findUnique({
const team = await index_1.prisma.team.findUnique({
where: { id: teamId },
include: {
members: { include: { user: { select: { id: true, name: true, email: true } } } },
captain: { select: { id: true, name: true } },
currentLeg: true,
game: { include: { legs: { orderBy: { sequenceNumber: 'asc' } } } }
game: { include: { routes: { include: { routeLegs: { orderBy: { sequenceNumber: 'asc' } } } } } },
teamRoutes: {
include: {
route: {
include: {
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
}
}
}
}
}
});
if (!team) {