Agenda logic refactor
This commit is contained in:
parent
2d35863f02
commit
3b6d85a1cc
@ -45,33 +45,71 @@ 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)
|
// Try to use mission relation if available, otherwise fetch separately
|
||||||
let calendars = await prisma.calendar.findMany({
|
let calendars: any[];
|
||||||
where: {
|
|
||||||
userId: session?.user?.id || '',
|
try {
|
||||||
},
|
// Try to use the mission relation directly (works after migration and Prisma Client regeneration)
|
||||||
include: {
|
calendars = await prisma.calendar.findMany({
|
||||||
events: {
|
where: {
|
||||||
orderBy: {
|
userId: session?.user?.id || '',
|
||||||
start: 'asc'
|
},
|
||||||
|
include: {
|
||||||
|
events: {
|
||||||
|
orderBy: {
|
||||||
|
start: 'asc'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
mission: {
|
||||||
|
include: {
|
||||||
|
missionUsers: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
} catch (error: any) {
|
||||||
|
// If mission relation doesn't exist yet (Prisma Client not regenerated), fetch without it
|
||||||
|
console.log('Mission relation not available, fetching calendars without mission relation');
|
||||||
|
calendars = await prisma.calendar.findMany({
|
||||||
|
where: {
|
||||||
|
userId: session?.user?.id || '',
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
events: {
|
||||||
|
orderBy: {
|
||||||
|
start: 'asc'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Then, fetch mission data for calendars that have missionId
|
// Then fetch mission data separately for calendars that have missionId
|
||||||
// This works whether or not the Prisma relation is available
|
const calendarsWithMission = await Promise.all(
|
||||||
const calendarsWithMission = await Promise.all(
|
calendars.map(async (cal) => {
|
||||||
calendars.map(async (cal) => {
|
// Check if calendar has missionId (from database even if Prisma Client doesn't know about it)
|
||||||
// Try to get missionId from the calendar (using raw query if needed)
|
const missionId = (cal as any).missionId;
|
||||||
let missionId: string | null = null;
|
|
||||||
|
if (missionId) {
|
||||||
try {
|
try {
|
||||||
// Try to access missionId directly (will work after Prisma Client regeneration)
|
const mission = await prisma.mission.findUnique({
|
||||||
missionId = (cal as any).missionId;
|
where: { id: missionId },
|
||||||
} catch (e) {
|
include: {
|
||||||
// If missionId doesn't exist, check if calendar name indicates a mission
|
missionUsers: true
|
||||||
if (cal.name.startsWith('Mission:')) {
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (mission) {
|
||||||
|
return {
|
||||||
|
...cal,
|
||||||
|
missionId: mission.id,
|
||||||
|
mission: mission
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Error fetching mission data:', error);
|
||||||
|
}
|
||||||
|
} else if (cal.name.startsWith('Mission:')) {
|
||||||
|
// Fallback: find mission by name if missionId not available
|
||||||
const missionName = cal.name.replace('Mission: ', '');
|
const missionName = cal.name.replace('Mission: ', '');
|
||||||
const mission = await prisma.mission.findFirst({
|
const mission = await prisma.mission.findFirst({
|
||||||
where: {
|
where: {
|
||||||
@ -81,19 +119,6 @@ export default async function CalendarPage() {
|
|||||||
{ missionUsers: { some: { userId: 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: {
|
include: {
|
||||||
missionUsers: true
|
missionUsers: true
|
||||||
}
|
}
|
||||||
@ -106,16 +131,14 @@ export default async function CalendarPage() {
|
|||||||
mission: mission
|
mission: mission
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
console.log('Error fetching mission data:', error);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return cal;
|
||||||
return cal;
|
})
|
||||||
})
|
);
|
||||||
);
|
|
||||||
|
calendars = calendarsWithMission;
|
||||||
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) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user