Added settings for user data deletion
This commit is contained in:
parent
59e15cfde8
commit
f49490042a
35 changed files with 2758 additions and 499 deletions
26
backend/dist/index.js
vendored
26
backend/dist/index.js
vendored
|
|
@ -9,12 +9,13 @@ const cors_1 = __importDefault(require("cors"));
|
|||
const http_1 = require("http");
|
||||
const socket_io_1 = require("socket.io");
|
||||
const client_1 = require("@prisma/client");
|
||||
const auth_js_1 = __importDefault(require("./routes/auth.js"));
|
||||
const games_js_1 = __importDefault(require("./routes/games.js"));
|
||||
const teams_js_1 = __importDefault(require("./routes/teams.js"));
|
||||
const legs_js_1 = __importDefault(require("./routes/legs.js"));
|
||||
const upload_js_1 = __importDefault(require("./routes/upload.js"));
|
||||
const index_js_1 = __importDefault(require("./socket/index.js"));
|
||||
const auth_1 = __importDefault(require("./routes/auth"));
|
||||
const games_1 = __importDefault(require("./routes/games"));
|
||||
const teams_1 = __importDefault(require("./routes/teams"));
|
||||
const routes_1 = __importDefault(require("./routes/routes"));
|
||||
const users_1 = __importDefault(require("./routes/users"));
|
||||
const upload_1 = __importDefault(require("./routes/upload"));
|
||||
const index_1 = __importDefault(require("./socket/index"));
|
||||
const app = (0, express_1.default)();
|
||||
const httpServer = (0, http_1.createServer)(app);
|
||||
const io = new socket_io_1.Server(httpServer, {
|
||||
|
|
@ -27,15 +28,16 @@ exports.prisma = new client_1.PrismaClient();
|
|||
app.use((0, cors_1.default)());
|
||||
app.use(express_1.default.json());
|
||||
app.use('/uploads', express_1.default.static('uploads'));
|
||||
app.use('/api/auth', auth_js_1.default);
|
||||
app.use('/api/games', games_js_1.default);
|
||||
app.use('/api/teams', teams_js_1.default);
|
||||
app.use('/api/legs', legs_js_1.default);
|
||||
app.use('/api/upload', upload_js_1.default);
|
||||
app.use('/api/auth', auth_1.default);
|
||||
app.use('/api/games', games_1.default);
|
||||
app.use('/api/teams', teams_1.default);
|
||||
app.use('/api/routes', routes_1.default);
|
||||
app.use('/api/users', users_1.default);
|
||||
app.use('/api/upload', upload_1.default);
|
||||
app.get('/api/health', (req, res) => {
|
||||
res.json({ status: 'ok' });
|
||||
});
|
||||
(0, index_js_1.default)(io);
|
||||
(0, index_1.default)(io);
|
||||
const PORT = process.env.PORT || 3001;
|
||||
httpServer.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
|
|
|
|||
6
backend/dist/middleware/auth.js
vendored
6
backend/dist/middleware/auth.js
vendored
|
|
@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.optionalAuth = exports.authenticate = void 0;
|
||||
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
||||
const index_js_1 = require("../index.js");
|
||||
const index_1 = require("../index");
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'treasure-trails-secret-key';
|
||||
const authenticate = async (req, res, next) => {
|
||||
try {
|
||||
|
|
@ -15,7 +15,7 @@ const authenticate = async (req, res, next) => {
|
|||
}
|
||||
const token = authHeader.split(' ')[1];
|
||||
const decoded = jsonwebtoken_1.default.verify(token, JWT_SECRET);
|
||||
const user = await index_js_1.prisma.user.findUnique({
|
||||
const user = await index_1.prisma.user.findUnique({
|
||||
where: { id: decoded.userId },
|
||||
select: { id: true, email: true, name: true }
|
||||
});
|
||||
|
|
@ -38,7 +38,7 @@ const optionalAuth = async (req, res, next) => {
|
|||
}
|
||||
const token = authHeader.split(' ')[1];
|
||||
const decoded = jsonwebtoken_1.default.verify(token, JWT_SECRET);
|
||||
const user = await index_js_1.prisma.user.findUnique({
|
||||
const user = await index_1.prisma.user.findUnique({
|
||||
where: { id: decoded.userId },
|
||||
select: { id: true, email: true, name: true }
|
||||
});
|
||||
|
|
|
|||
10
backend/dist/routes/auth.js
vendored
10
backend/dist/routes/auth.js
vendored
|
|
@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||
const express_1 = require("express");
|
||||
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
||||
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
||||
const index_js_1 = require("../index.js");
|
||||
const index_1 = require("../index");
|
||||
const router = (0, express_1.Router)();
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'treasure-trails-secret-key';
|
||||
router.post('/register', async (req, res) => {
|
||||
|
|
@ -15,12 +15,12 @@ router.post('/register', async (req, res) => {
|
|||
if (!email || !password || !name) {
|
||||
return res.status(400).json({ error: 'Email, password, and name are required' });
|
||||
}
|
||||
const existingUser = await index_js_1.prisma.user.findUnique({ where: { email } });
|
||||
const existingUser = await index_1.prisma.user.findUnique({ where: { email } });
|
||||
if (existingUser) {
|
||||
return res.status(400).json({ error: 'Email already registered' });
|
||||
}
|
||||
const passwordHash = await bcryptjs_1.default.hash(password, 10);
|
||||
const user = await index_js_1.prisma.user.create({
|
||||
const user = await index_1.prisma.user.create({
|
||||
data: { email, passwordHash, name }
|
||||
});
|
||||
const token = jsonwebtoken_1.default.sign({ userId: user.id }, JWT_SECRET, { expiresIn: '7d' });
|
||||
|
|
@ -40,7 +40,7 @@ router.post('/login', async (req, res) => {
|
|||
if (!email || !password) {
|
||||
return res.status(400).json({ error: 'Email and password are required' });
|
||||
}
|
||||
const user = await index_js_1.prisma.user.findUnique({ where: { email } });
|
||||
const user = await index_1.prisma.user.findUnique({ where: { email } });
|
||||
if (!user) {
|
||||
return res.status(401).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
|
|
@ -67,7 +67,7 @@ router.get('/me', async (req, res) => {
|
|||
}
|
||||
const token = authHeader.split(' ')[1];
|
||||
const decoded = jsonwebtoken_1.default.verify(token, JWT_SECRET);
|
||||
const user = await index_js_1.prisma.user.findUnique({
|
||||
const user = await index_1.prisma.user.findUnique({
|
||||
where: { id: decoded.userId },
|
||||
select: { id: true, email: true, name: true, createdAt: true }
|
||||
});
|
||||
|
|
|
|||
171
backend/dist/routes/games.js
vendored
171
backend/dist/routes/games.js
vendored
|
|
@ -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 } } }
|
||||
});
|
||||
|
|
|
|||
2
backend/dist/routes/routes.d.ts
vendored
Normal file
2
backend/dist/routes/routes.d.ts
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
declare const router: import("express-serve-static-core").Router;
|
||||
export default router;
|
||||
338
backend/dist/routes/routes.js
vendored
Normal file
338
backend/dist/routes/routes.js
vendored
Normal file
|
|
@ -0,0 +1,338 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const express_1 = require("express");
|
||||
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 routes = await index_1.prisma.route.findMany({
|
||||
where: { gameId: gameId },
|
||||
include: {
|
||||
routeLegs: {
|
||||
orderBy: { sequenceNumber: 'asc' }
|
||||
},
|
||||
_count: { select: { teamRoutes: true } }
|
||||
},
|
||||
orderBy: { createdAt: 'asc' }
|
||||
});
|
||||
res.json(routes);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('List routes error:', error);
|
||||
res.status(500).json({ error: 'Failed to list routes' });
|
||||
}
|
||||
});
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const route = await index_1.prisma.route.findUnique({
|
||||
where: { id: id },
|
||||
include: {
|
||||
routeLegs: {
|
||||
orderBy: { sequenceNumber: 'asc' }
|
||||
},
|
||||
teamRoutes: {
|
||||
include: {
|
||||
team: {
|
||||
include: {
|
||||
members: { include: { user: { select: { id: true, name: true } } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!route) {
|
||||
return res.status(404).json({ error: 'Route not found' });
|
||||
}
|
||||
res.json(route);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Get route error:', error);
|
||||
res.status(500).json({ error: 'Failed to get route' });
|
||||
}
|
||||
});
|
||||
router.post('/', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { gameId, name, description, color } = req.body;
|
||||
if (!gameId || !name) {
|
||||
return res.status(400).json({ error: 'Game ID and name are required' });
|
||||
}
|
||||
const game = await index_1.prisma.game.findUnique({ where: { id: gameId } });
|
||||
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 route = await index_1.prisma.route.create({
|
||||
data: {
|
||||
gameId,
|
||||
name,
|
||||
description,
|
||||
color: color || '#3498db'
|
||||
},
|
||||
include: {
|
||||
routeLegs: true
|
||||
}
|
||||
});
|
||||
res.json(route);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Create route error:', error);
|
||||
res.status(500).json({ error: 'Failed to create route' });
|
||||
}
|
||||
});
|
||||
router.put('/:id', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { name, description, color } = req.body;
|
||||
const route = await index_1.prisma.route.findUnique({
|
||||
where: { id: id },
|
||||
include: { game: true }
|
||||
});
|
||||
if (!route) {
|
||||
return res.status(404).json({ error: 'Route not found' });
|
||||
}
|
||||
if (route.game.gameMasterId !== req.user.id) {
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
}
|
||||
const updated = await index_1.prisma.route.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
name: name || route.name,
|
||||
description: description !== undefined ? description : route.description,
|
||||
color: color || route.color
|
||||
},
|
||||
include: {
|
||||
routeLegs: {
|
||||
orderBy: { sequenceNumber: 'asc' }
|
||||
}
|
||||
}
|
||||
});
|
||||
res.json(updated);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Update route error:', error);
|
||||
res.status(500).json({ error: 'Failed to update route' });
|
||||
}
|
||||
});
|
||||
router.delete('/:id', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const route = await index_1.prisma.route.findUnique({
|
||||
where: { id: id },
|
||||
include: { game: true }
|
||||
});
|
||||
if (!route) {
|
||||
return res.status(404).json({ error: 'Route not found' });
|
||||
}
|
||||
if (route.game.gameMasterId !== req.user.id) {
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
}
|
||||
await index_1.prisma.route.delete({ where: { id: id } });
|
||||
res.json({ message: 'Route deleted' });
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Delete route error:', error);
|
||||
res.status(500).json({ error: 'Failed to delete route' });
|
||||
}
|
||||
});
|
||||
router.post('/:id/legs', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { description, conditionType, conditionDetails, locationLat, locationLng, timeLimit } = req.body;
|
||||
const route = await index_1.prisma.route.findUnique({
|
||||
where: { id: id },
|
||||
include: {
|
||||
game: true,
|
||||
routeLegs: true
|
||||
}
|
||||
});
|
||||
if (!route) {
|
||||
return res.status(404).json({ error: 'Route not found' });
|
||||
}
|
||||
if (route.game.gameMasterId !== req.user.id) {
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
}
|
||||
const maxSequence = route.routeLegs.length > 0
|
||||
? Math.max(...route.routeLegs.map(l => l.sequenceNumber))
|
||||
: 0;
|
||||
const routeLeg = await index_1.prisma.routeLeg.create({
|
||||
data: {
|
||||
routeId: id,
|
||||
sequenceNumber: maxSequence + 1,
|
||||
description: description || '',
|
||||
conditionType: conditionType || 'photo',
|
||||
conditionDetails,
|
||||
locationLat,
|
||||
locationLng,
|
||||
timeLimit
|
||||
}
|
||||
});
|
||||
res.json(routeLeg);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Add route leg error:', error);
|
||||
res.status(500).json({ error: 'Failed to add leg to route' });
|
||||
}
|
||||
});
|
||||
router.put('/:id/legs/:legId', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id, legId } = req.params;
|
||||
const { description, conditionType, conditionDetails, locationLat, locationLng, timeLimit, sequenceNumber } = req.body;
|
||||
const route = await index_1.prisma.route.findUnique({
|
||||
where: { id: id },
|
||||
include: { game: true }
|
||||
});
|
||||
if (!route) {
|
||||
return res.status(404).json({ error: 'Route not found' });
|
||||
}
|
||||
if (route.game.gameMasterId !== req.user.id) {
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
}
|
||||
const routeLeg = await index_1.prisma.routeLeg.update({
|
||||
where: { id: legId },
|
||||
data: {
|
||||
description: description !== undefined ? description : undefined,
|
||||
conditionType: conditionType !== undefined ? conditionType : undefined,
|
||||
conditionDetails: conditionDetails !== undefined ? conditionDetails : undefined,
|
||||
locationLat: locationLat !== undefined ? locationLat : undefined,
|
||||
locationLng: locationLng !== undefined ? locationLng : undefined,
|
||||
timeLimit: timeLimit !== undefined ? timeLimit : undefined,
|
||||
sequenceNumber: sequenceNumber !== undefined ? sequenceNumber : undefined
|
||||
}
|
||||
});
|
||||
res.json(routeLeg);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Update route leg error:', error);
|
||||
res.status(500).json({ error: 'Failed to update route leg' });
|
||||
}
|
||||
});
|
||||
router.delete('/:id/legs/:legId', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id, legId } = req.params;
|
||||
const route = await index_1.prisma.route.findUnique({
|
||||
where: { id: id },
|
||||
include: { game: true }
|
||||
});
|
||||
if (!route) {
|
||||
return res.status(404).json({ error: 'Route not found' });
|
||||
}
|
||||
if (route.game.gameMasterId !== req.user.id) {
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
}
|
||||
await index_1.prisma.routeLeg.delete({ where: { id: legId } });
|
||||
const remainingLegs = await index_1.prisma.routeLeg.findMany({
|
||||
where: { routeId: id },
|
||||
orderBy: { sequenceNumber: 'asc' }
|
||||
});
|
||||
for (let i = 0; i < remainingLegs.length; i++) {
|
||||
if (remainingLegs[i].sequenceNumber !== i + 1) {
|
||||
await index_1.prisma.routeLeg.update({
|
||||
where: { id: remainingLegs[i].id },
|
||||
data: { sequenceNumber: i + 1 }
|
||||
});
|
||||
}
|
||||
}
|
||||
res.json({ message: 'Leg deleted' });
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Delete route leg error:', error);
|
||||
res.status(500).json({ error: 'Failed to delete route leg' });
|
||||
}
|
||||
});
|
||||
router.post('/:id/legs/:legId/photo', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id, legId } = req.params;
|
||||
const { teamId, photoUrl } = req.body;
|
||||
const route = await index_1.prisma.route.findUnique({
|
||||
where: { id: id },
|
||||
include: { game: true }
|
||||
});
|
||||
if (!route) {
|
||||
return res.status(404).json({ error: 'Route not found' });
|
||||
}
|
||||
const routeLeg = await index_1.prisma.routeLeg.findFirst({
|
||||
where: { id: legId, routeId: id }
|
||||
});
|
||||
if (!routeLeg) {
|
||||
return res.status(404).json({ error: 'Route leg not found' });
|
||||
}
|
||||
const member = await index_1.prisma.teamMember.findFirst({
|
||||
where: { teamId, userId: req.user.id }
|
||||
});
|
||||
const team = await index_1.prisma.team.findUnique({ where: { id: teamId } });
|
||||
if (!member && team?.captainId !== req.user.id) {
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
}
|
||||
const submission = await index_1.prisma.photoSubmission.create({
|
||||
data: {
|
||||
teamId,
|
||||
routeId: id,
|
||||
routeLegId: legId,
|
||||
photoUrl
|
||||
}
|
||||
});
|
||||
res.json(submission);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Photo submission error:', error);
|
||||
res.status(500).json({ error: 'Failed to submit photo' });
|
||||
}
|
||||
});
|
||||
router.post('/:id/copy', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const originalRoute = await index_1.prisma.route.findUnique({
|
||||
where: { id: id },
|
||||
include: {
|
||||
game: true,
|
||||
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
|
||||
}
|
||||
});
|
||||
if (!originalRoute) {
|
||||
return res.status(404).json({ error: 'Route not found' });
|
||||
}
|
||||
if (originalRoute.game.gameMasterId !== req.user.id) {
|
||||
return res.status(403).json({ error: 'Not authorized' });
|
||||
}
|
||||
const newRoute = await index_1.prisma.route.create({
|
||||
data: {
|
||||
gameId: originalRoute.gameId,
|
||||
name: `${originalRoute.name} (Copy)`,
|
||||
description: originalRoute.description,
|
||||
color: originalRoute.color
|
||||
}
|
||||
});
|
||||
for (const leg of originalRoute.routeLegs) {
|
||||
await index_1.prisma.routeLeg.create({
|
||||
data: {
|
||||
routeId: newRoute.id,
|
||||
sequenceNumber: leg.sequenceNumber,
|
||||
description: leg.description,
|
||||
conditionType: leg.conditionType,
|
||||
conditionDetails: leg.conditionDetails,
|
||||
locationLat: leg.locationLat,
|
||||
locationLng: leg.locationLng,
|
||||
timeLimit: leg.timeLimit
|
||||
}
|
||||
});
|
||||
}
|
||||
const fullRoute = await index_1.prisma.route.findUnique({
|
||||
where: { id: newRoute.id },
|
||||
include: {
|
||||
routeLegs: { orderBy: { sequenceNumber: 'asc' } }
|
||||
}
|
||||
});
|
||||
res.json(fullRoute);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Copy route error:', error);
|
||||
res.status(500).json({ error: 'Failed to copy route' });
|
||||
}
|
||||
});
|
||||
exports.default = router;
|
||||
186
backend/dist/routes/teams.js
vendored
186
backend/dist/routes/teams.js
vendored
|
|
@ -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) {
|
||||
|
|
|
|||
4
backend/dist/routes/upload.js
vendored
4
backend/dist/routes/upload.js
vendored
|
|
@ -7,7 +7,7 @@ const express_1 = require("express");
|
|||
const multer_1 = __importDefault(require("multer"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const uuid_1 = require("uuid");
|
||||
const auth_js_1 = require("../middleware/auth.js");
|
||||
const auth_1 = require("../middleware/auth");
|
||||
const router = (0, express_1.Router)();
|
||||
const storage = multer_1.default.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
|
|
@ -31,7 +31,7 @@ const upload = (0, multer_1.default)({
|
|||
cb(new Error('Only image files are allowed'));
|
||||
}
|
||||
});
|
||||
router.post('/upload', auth_js_1.authenticate, upload.single('photo'), (req, res) => {
|
||||
router.post('/upload', auth_1.authenticate, upload.single('photo'), (req, res) => {
|
||||
try {
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ error: 'No file uploaded' });
|
||||
|
|
|
|||
2
backend/dist/routes/users.d.ts
vendored
Normal file
2
backend/dist/routes/users.d.ts
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
declare const router: import("express-serve-static-core").Router;
|
||||
export default router;
|
||||
213
backend/dist/routes/users.js
vendored
Normal file
213
backend/dist/routes/users.js
vendored
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const express_1 = require("express");
|
||||
const index_1 = require("../index");
|
||||
const auth_1 = require("../middleware/auth");
|
||||
const router = (0, express_1.Router)();
|
||||
router.get('/me', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const user = await index_1.prisma.user.findUnique({
|
||||
where: { id: req.user.id },
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
screenName: true,
|
||||
avatarUrl: true,
|
||||
createdAt: true
|
||||
}
|
||||
});
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: 'User not found' });
|
||||
}
|
||||
res.json(user);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Get user error:', error);
|
||||
res.status(500).json({ error: 'Failed to get user' });
|
||||
}
|
||||
});
|
||||
router.put('/me', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const { name, screenName, avatarUrl } = req.body;
|
||||
const updated = await index_1.prisma.user.update({
|
||||
where: { id: req.user.id },
|
||||
data: {
|
||||
name: name || undefined,
|
||||
screenName: screenName !== undefined ? screenName || null : undefined,
|
||||
avatarUrl: avatarUrl !== undefined ? avatarUrl || null : undefined
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
screenName: true,
|
||||
avatarUrl: true,
|
||||
createdAt: true
|
||||
}
|
||||
});
|
||||
res.json(updated);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Update user error:', error);
|
||||
res.status(500).json({ error: 'Failed to update user' });
|
||||
}
|
||||
});
|
||||
router.get('/me/location-history', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const locations = await index_1.prisma.locationHistory.findMany({
|
||||
where: { userId: req.user.id },
|
||||
include: {
|
||||
game: {
|
||||
select: { id: true, name: true }
|
||||
}
|
||||
},
|
||||
orderBy: { recordedAt: 'desc' }
|
||||
});
|
||||
const games = await index_1.prisma.game.findMany({
|
||||
where: {
|
||||
teams: {
|
||||
some: {
|
||||
members: {
|
||||
some: { userId: req.user.id }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
select: { id: true, name: true }
|
||||
});
|
||||
const locationByGame = games.map(game => {
|
||||
const gameLocations = locations.filter(l => l.gameId === game.id);
|
||||
return {
|
||||
game: game,
|
||||
locations: gameLocations,
|
||||
locationCount: gameLocations.length
|
||||
};
|
||||
}).filter(g => g.locationCount > 0);
|
||||
res.json({
|
||||
totalLocations: locations.length,
|
||||
byGame: locationByGame
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Get location history error:', error);
|
||||
res.status(500).json({ error: 'Failed to get location history' });
|
||||
}
|
||||
});
|
||||
router.get('/me/games', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
const memberships = await index_1.prisma.teamMember.findMany({
|
||||
where: { userId: req.user.id },
|
||||
include: {
|
||||
team: {
|
||||
include: {
|
||||
game: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
status: true,
|
||||
startDate: true,
|
||||
locationLat: true,
|
||||
locationLng: true,
|
||||
gameMasterId: true,
|
||||
gameMaster: { select: { name: true } }
|
||||
}
|
||||
},
|
||||
teamRoutes: {
|
||||
include: {
|
||||
route: {
|
||||
include: {
|
||||
routeLegs: {
|
||||
orderBy: { sequenceNumber: 'asc' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
photoSubmissions: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
const gamesWithDetails = memberships.map(m => {
|
||||
const team = m.team;
|
||||
const game = team.game;
|
||||
const teamRoute = team.teamRoutes[0];
|
||||
const route = teamRoute?.route;
|
||||
const photoSubmissions = team.photoSubmissions;
|
||||
const routeLegs = route?.routeLegs || [];
|
||||
const proofLocations = routeLegs.filter(leg => photoSubmissions.some(p => p.routeLegId === leg.id));
|
||||
let totalDistance = 0;
|
||||
if (game.locationLat && game.locationLng) {
|
||||
let prevLat = game.locationLat;
|
||||
let prevLng = game.locationLng;
|
||||
for (const leg of routeLegs) {
|
||||
if (leg.locationLat && leg.locationLng) {
|
||||
const R = 6371;
|
||||
const dLat = (leg.locationLat - prevLat) * Math.PI / 180;
|
||||
const dLng = (leg.locationLng - prevLng) * Math.PI / 180;
|
||||
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos(prevLat * Math.PI / 180) * Math.cos(leg.locationLat * Math.PI / 180) *
|
||||
Math.sin(dLng / 2) * Math.sin(dLng / 2);
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
totalDistance += R * c;
|
||||
prevLat = leg.locationLat;
|
||||
prevLng = leg.locationLng;
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
gameId: game.id,
|
||||
gameName: game.name,
|
||||
gameStatus: game.status,
|
||||
gameMaster: game.gameMaster.name,
|
||||
startDate: game.startDate,
|
||||
teamId: team.id,
|
||||
teamName: team.name,
|
||||
teamStatus: team.status,
|
||||
routeId: route?.id || null,
|
||||
routeName: route?.name || null,
|
||||
routeColor: route?.color || null,
|
||||
totalLegs: routeLegs.length,
|
||||
totalDistance: Math.round(totalDistance * 100) / 100,
|
||||
proofLocations: proofLocations.map(leg => ({
|
||||
legNumber: leg.sequenceNumber,
|
||||
description: leg.description,
|
||||
locationLat: leg.locationLat,
|
||||
locationLng: leg.locationLng,
|
||||
hasPhotoProof: photoSubmissions.some(p => p.routeLegId === leg.id)
|
||||
}))
|
||||
};
|
||||
});
|
||||
res.json(gamesWithDetails);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Get user games error:', error);
|
||||
res.status(500).json({ error: 'Failed to get user games' });
|
||||
}
|
||||
});
|
||||
router.delete('/me/location-data', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
await index_1.prisma.locationHistory.deleteMany({
|
||||
where: { userId: req.user.id }
|
||||
});
|
||||
res.json({ message: 'Location data deleted' });
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Delete location data error:', error);
|
||||
res.status(500).json({ error: 'Failed to delete location data' });
|
||||
}
|
||||
});
|
||||
router.delete('/me/account', auth_1.authenticate, async (req, res) => {
|
||||
try {
|
||||
await index_1.prisma.user.delete({
|
||||
where: { id: req.user.id }
|
||||
});
|
||||
res.json({ message: 'Account deleted' });
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Delete account error:', error);
|
||||
res.status(500).json({ error: 'Failed to delete account' });
|
||||
}
|
||||
});
|
||||
exports.default = router;
|
||||
6
backend/dist/socket/index.js
vendored
6
backend/dist/socket/index.js
vendored
|
|
@ -1,7 +1,7 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = setupSocket;
|
||||
const index_js_1 = require("../index.js");
|
||||
const index_1 = require("../index");
|
||||
function setupSocket(io) {
|
||||
io.on('connection', (socket) => {
|
||||
console.log('Client connected:', socket.id);
|
||||
|
|
@ -13,7 +13,7 @@ function setupSocket(io) {
|
|||
socket.leave(`game:${gameId}`);
|
||||
});
|
||||
socket.on('team-location', async (data) => {
|
||||
await index_js_1.prisma.team.update({
|
||||
await index_1.prisma.team.update({
|
||||
where: { id: data.teamId },
|
||||
data: { lat: data.lat, lng: data.lng }
|
||||
});
|
||||
|
|
@ -24,7 +24,7 @@ function setupSocket(io) {
|
|||
});
|
||||
});
|
||||
socket.on('chat-message', async (data) => {
|
||||
const chatMessage = await index_js_1.prisma.chatMessage.create({
|
||||
const chatMessage = await index_1.prisma.chatMessage.create({
|
||||
data: {
|
||||
gameId: data.gameId,
|
||||
teamId: data.teamId,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue