fixed a bit

This commit is contained in:
bobbert committed 2025-03-11 17:46:24 +00:00
1 parent 55e93b54c4
commit 524533ce35
12 files changed
+11174 -19

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
import { queryOne } from '../../../../lib/db';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '../../auth/[...nextauth]/route';
export async function GET(request, { params }) {
const session = await getServerSession(authOptions);
if (!session) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
try {
const { teamId } = params;
// Ensure the user is requesting their own team or is an admin
if (session.user.teamId != teamId && session.user.role !== 'admin') {
return Response.json({ error: 'Not authorized to view this team' }, { status: 403 });
}
const team = await queryOne('SELECT * FROM teams WHERE id = ?', [teamId]);
if (!team) {
return Response.json({ error: 'Team not found' }, { status: 404 });
}
return Response.json(team);
} catch (error) {
console.error('Team fetch error:', error);
return Response.json({ error: 'Failed to fetch team details' }, { status: 500 });
}
}
+51
View File
@@ -0,0 +1,51 @@
import { query, queryOne, run } from '../../../../lib/db';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '../../auth/[...nextauth]/route';
import { nanoid } from 'nanoid';
export async function POST(request) {
const session = await getServerSession(authOptions);
if (!session) {
return Response.json({ success: false, error: 'Unauthorized' }, { status: 401 });
}
try {
const { name, userId } = await request.json();
if (!name || name.trim() === '') {
return Response.json({ success: false, error: 'Team name is required' });
}
// Generate a unique team code (6 characters)
const teamCode = nanoid(6).toUpperCase();
// Insert the new team
const result = await run(
'INSERT INTO teams (name, code, created_by) VALUES (?, ?, ?)',
[name, teamCode, userId]
);
if (!result || !result.lastID) {
return Response.json({ success: false, error: 'Failed to create team' });
}
// Update the user's team_id
await run(
'UPDATE users SET team_id = ? WHERE id = ?',
[result.lastID, userId]
);
return Response.json({
success: true,
teamId: result.lastID,
teamCode
});
} catch (error) {
console.error('Create team error:', error);
return Response.json({
success: false,
error: 'Failed to create team: ' + error.message
});
}
}