NeahStable/app/api/missions/user/route.ts
2026-01-16 14:03:26 +01:00

57 lines
1.5 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 list missions for the current user
export async function GET(request: Request) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const userId = session.user.id;
// Find all missions where the user is either the creator or a member
const missions = await prisma.mission.findMany({
where: {
OR: [
{ creatorId: userId },
{ missionUsers: { some: { userId } } }
]
},
select: {
id: true,
name: true,
logo: true,
logoUrl: true,
createdAt: true,
updatedAt: true,
isClosed: true,
missionUsers: {
where: { userId },
select: {
role: true
}
}
},
orderBy: { updatedAt: 'desc' }
});
// Transform missions to include logo URL
const missionsWithUrls = missions.map(mission => ({
...mission,
logoUrl: mission.logo ? `/api/missions/image/${mission.logo}` : null
}));
return NextResponse.json({ missions: missionsWithUrls });
} catch (error) {
console.error('Error fetching user missions:', error);
return NextResponse.json(
{ error: 'Failed to fetch missions' },
{ status: 500 }
);
}
}