83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
// src/app/api/team/request-join/route.ts
|
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
import { prisma } from '@/app/lib/prisma'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/app/lib/auth'
|
|
import { sendServerSSEMessage } from '@/app/lib/sse-server-client'
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
/* ---- Session prüfen ------------------------------------------ */
|
|
const session = await getServerSession(authOptions(req))
|
|
if (!session?.user?.steamId) {
|
|
return NextResponse.json({ message: 'Nicht eingeloggt' }, { status: 401 })
|
|
}
|
|
const requesterSteamId = session.user.steamId
|
|
|
|
/* ---- Body validieren ----------------------------------------- */
|
|
const { teamId } = await req.json()
|
|
if (!teamId) {
|
|
return NextResponse.json({ message: 'teamId fehlt' }, { status: 400 })
|
|
}
|
|
|
|
/* ---- Team holen ---------------------------------------------- */
|
|
const team = await prisma.team.findUnique({ where: { id: teamId } })
|
|
if (!team) {
|
|
return NextResponse.json({ message: 'Team nicht gefunden' }, { status: 404 })
|
|
}
|
|
|
|
/* ---- Bereits Mitglied? --------------------------------------- */
|
|
if (
|
|
requesterSteamId === team.leaderId ||
|
|
team.activePlayers.includes(requesterSteamId) ||
|
|
team.inactivePlayers.includes(requesterSteamId)
|
|
) {
|
|
return NextResponse.json({ message: 'Du bist bereits Mitglied' }, { status: 400 })
|
|
}
|
|
|
|
/* ---- Doppelte Anfrage vermeiden ------------------------------ */
|
|
const existingInvite = await prisma.teamInvite.findFirst({
|
|
where: { steamId: requesterSteamId, teamId },
|
|
})
|
|
if (existingInvite) {
|
|
return NextResponse.json({ message: 'Anfrage läuft bereits' }, { status: 200 })
|
|
}
|
|
|
|
/* ---- Invitation anlegen -------------------------------------- */
|
|
await prisma.teamInvite.create({
|
|
data: {
|
|
steamId: requesterSteamId, // User.steamId
|
|
teamId,
|
|
type: 'team-join-request',
|
|
},
|
|
})
|
|
|
|
/* ---- Leader benachrichtigen ---------------------------------- */
|
|
const notification = await prisma.notification.create({
|
|
data: {
|
|
steamId: team.leaderId!,
|
|
title: 'Beitrittsanfrage',
|
|
message: `${session.user.name ?? 'Ein Spieler'} möchte deinem Team beitreten.`,
|
|
actionType: 'team-join-request',
|
|
actionData: teamId,
|
|
},
|
|
})
|
|
|
|
/* ---- SSE Event (optional) ------------------------------ */
|
|
await sendServerSSEMessage({
|
|
type: notification.actionType ?? 'notification',
|
|
targetUserIds: [team.leaderId],
|
|
message: notification.message,
|
|
id: notification.id,
|
|
actionType: notification.actionType ?? undefined,
|
|
actionData: notification.actionData ?? undefined,
|
|
createdAt: notification.createdAt.toISOString(),
|
|
})
|
|
|
|
return NextResponse.json({ message: 'Anfrage gesendet' }, { status: 200 })
|
|
} catch (err) {
|
|
console.error('POST /api/team/request-join', err)
|
|
return NextResponse.json({ message: 'Interner Serverfehler' }, { status: 500 })
|
|
}
|
|
}
|