NeahStable/app/api/missions/[missionId]/route.ts

69 lines
1.7 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from "@/app/api/auth/options";
import { prisma } from '@/lib/prisma';
// GET endpoint to get mission details including creator and missionUsers
export async function GET(
request: Request,
{ params }: { params: Promise<{ missionId: string }> }
) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { missionId } = await params;
const userId = session.user.id;
// Find mission and check access
const mission = await prisma.mission.findFirst({
where: {
id: missionId,
OR: [
{ creatorId: userId },
{ missionUsers: { some: { userId } } }
]
},
select: {
id: true,
name: true,
creatorId: true,
isClosed: true,
creator: {
select: {
id: true,
email: true
}
},
missionUsers: {
select: {
id: true,
role: true,
userId: true,
user: {
select: {
id: true,
email: true
}
}
}
}
}
});
if (!mission) {
return NextResponse.json({ error: 'Mission not found or access denied' }, { status: 404 });
}
return NextResponse.json(mission);
} catch (error: any) {
console.error('Error fetching mission:', error);
return NextResponse.json(
{ error: 'Failed to fetch mission', details: error.message },
{ status: 500 }
);
}
}