Fix: Remove mission from Calendar include to avoid Prisma error
This commit is contained in:
parent
4378381303
commit
2d35863f02
@ -45,6 +45,7 @@ export default async function CalendarPage() {
|
|||||||
const userId = session.user.username || session.user.email || '';
|
const userId = session.user.username || session.user.email || '';
|
||||||
|
|
||||||
// Get all calendars for the user
|
// Get all calendars for the user
|
||||||
|
// First, get calendars without mission relation (in case Prisma Client not regenerated)
|
||||||
let calendars = await prisma.calendar.findMany({
|
let calendars = await prisma.calendar.findMany({
|
||||||
where: {
|
where: {
|
||||||
userId: session?.user?.id || '',
|
userId: session?.user?.id || '',
|
||||||
@ -54,15 +55,68 @@ export default async function CalendarPage() {
|
|||||||
orderBy: {
|
orderBy: {
|
||||||
start: 'asc'
|
start: 'asc'
|
||||||
}
|
}
|
||||||
},
|
|
||||||
mission: {
|
|
||||||
include: {
|
|
||||||
missionUsers: true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Then, fetch mission data for calendars that have missionId
|
||||||
|
// This works whether or not the Prisma relation is available
|
||||||
|
const calendarsWithMission = await Promise.all(
|
||||||
|
calendars.map(async (cal) => {
|
||||||
|
// Try to get missionId from the calendar (using raw query if needed)
|
||||||
|
let missionId: string | null = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Try to access missionId directly (will work after Prisma Client regeneration)
|
||||||
|
missionId = (cal as any).missionId;
|
||||||
|
} catch (e) {
|
||||||
|
// If missionId doesn't exist, check if calendar name indicates a mission
|
||||||
|
if (cal.name.startsWith('Mission:')) {
|
||||||
|
const missionName = cal.name.replace('Mission: ', '');
|
||||||
|
const mission = await prisma.mission.findFirst({
|
||||||
|
where: {
|
||||||
|
name: missionName,
|
||||||
|
OR: [
|
||||||
|
{ creatorId: session.user.id },
|
||||||
|
{ missionUsers: { some: { userId: session.user.id } } }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
select: { id: true }
|
||||||
|
});
|
||||||
|
if (mission) {
|
||||||
|
missionId = mission.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have a missionId, fetch the full mission data
|
||||||
|
if (missionId) {
|
||||||
|
try {
|
||||||
|
const mission = await prisma.mission.findUnique({
|
||||||
|
where: { id: missionId },
|
||||||
|
include: {
|
||||||
|
missionUsers: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (mission) {
|
||||||
|
return {
|
||||||
|
...cal,
|
||||||
|
missionId: mission.id,
|
||||||
|
mission: mission
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Error fetching mission data:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cal;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
calendars = calendarsWithMission as any;
|
||||||
|
|
||||||
// If no calendars exist, create default ones
|
// If no calendars exist, create default ones
|
||||||
if (calendars.length === 0) {
|
if (calendars.length === 0) {
|
||||||
const defaultCalendars = [
|
const defaultCalendars = [
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user