49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { auth } from '@/lib/auth';
|
|
import { getUserTeams, createTeam } from '@/services/teams';
|
|
import type { CreateTeamInput } from '@/lib/types';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const session = await auth();
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
|
}
|
|
|
|
const teams = await getUserTeams(session.user.id);
|
|
|
|
return NextResponse.json(teams);
|
|
} catch (error) {
|
|
console.error('Error fetching teams:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Erreur lors de la récupération des équipes' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const session = await auth();
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
|
}
|
|
|
|
const body: CreateTeamInput = await request.json();
|
|
const { name, description } = body;
|
|
|
|
if (!name) {
|
|
return NextResponse.json({ error: "Le nom de l'équipe est requis" }, { status: 400 });
|
|
}
|
|
|
|
const team = await createTeam(name, description || null, session.user.id);
|
|
|
|
return NextResponse.json(team, { status: 201 });
|
|
} catch (error) {
|
|
console.error('Error creating team:', error);
|
|
return NextResponse.json({ error: "Erreur lors de la création de l'équipe" }, { status: 500 });
|
|
}
|
|
}
|