diff --git a/app/agenda/page.tsx b/app/agenda/page.tsx index 38d6232..c464ca1 100644 --- a/app/agenda/page.tsx +++ b/app/agenda/page.tsx @@ -44,101 +44,24 @@ export default async function CalendarPage() { const userId = session.user.username || session.user.email || ''; - // Get all calendars for the user - // Try to use mission relation if available, otherwise fetch separately - let calendars: any[]; - - try { - // Try to use the mission relation directly (works after migration and Prisma Client regeneration) - calendars = await prisma.calendar.findMany({ - where: { - userId: session?.user?.id || '', + // Get all calendars for the user with mission relation + const calendars = await prisma.calendar.findMany({ + where: { + userId: session?.user?.id || '', + }, + include: { + events: { + orderBy: { + start: 'asc' + } }, - include: { - events: { - orderBy: { - start: 'asc' - } - }, - mission: { - include: { - missionUsers: true - } + 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 separately for calendars that have missionId - const calendarsWithMission = await Promise.all( - calendars.map(async (cal) => { - // Check if calendar has missionId (from database even if Prisma Client doesn't know about it) - const missionId = (cal as any).missionId; - - 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); - } - } else if (cal.name.startsWith('Mission:')) { - // Fallback: find mission by name if missionId not available - 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 } } } - ] - }, - include: { - missionUsers: true - } - }); - - if (mission) { - return { - ...cal, - missionId: mission.id, - mission: mission - }; - } - } - - return cal; - }) - ); - - calendars = calendarsWithMission; - } + } + }); // If no calendars exist, create default ones if (calendars.length === 0) { diff --git a/prisma/migrations/20260114125000_add_calendar_sync/migration.sql b/prisma/migrations/20260114125000_add_calendar_sync/migration.sql new file mode 100644 index 0000000..28a29c0 --- /dev/null +++ b/prisma/migrations/20260114125000_add_calendar_sync/migration.sql @@ -0,0 +1,60 @@ +-- AlterTable: Add missionId column if it doesn't exist +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'Calendar' AND column_name = 'missionId' + ) THEN + ALTER TABLE "Calendar" ADD COLUMN "missionId" TEXT; + END IF; +END $$; + +-- CreateTable +CREATE TABLE IF NOT EXISTS "CalendarSync" ( + "id" TEXT NOT NULL, + "calendarId" TEXT NOT NULL, + "mailCredentialId" TEXT, + "provider" TEXT NOT NULL, + "externalCalendarId" TEXT, + "externalCalendarUrl" TEXT, + "syncEnabled" BOOLEAN NOT NULL DEFAULT true, + "lastSyncAt" TIMESTAMP(3), + "syncFrequency" INTEGER NOT NULL DEFAULT 15, + "lastSyncError" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "CalendarSync_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX IF NOT EXISTS "CalendarSync_calendarId_key" ON "CalendarSync"("calendarId"); + +-- CreateIndex +CREATE INDEX IF NOT EXISTS "CalendarSync_calendarId_idx" ON "CalendarSync"("calendarId"); + +-- CreateIndex +CREATE INDEX IF NOT EXISTS "CalendarSync_mailCredentialId_idx" ON "CalendarSync"("mailCredentialId"); + +-- CreateIndex +CREATE INDEX IF NOT EXISTS "CalendarSync_provider_idx" ON "CalendarSync"("provider"); + +-- CreateIndex +CREATE INDEX IF NOT EXISTS "Calendar_missionId_idx" ON "Calendar"("missionId"); + +-- AddForeignKey: Add foreign key if it doesn't exist +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'Calendar_missionId_fkey' + ) THEN + ALTER TABLE "Calendar" ADD CONSTRAINT "Calendar_missionId_fkey" FOREIGN KEY ("missionId") REFERENCES "Mission"("id") ON DELETE SET NULL ON UPDATE CASCADE; + END IF; +END $$; + +-- AddForeignKey +ALTER TABLE "CalendarSync" ADD CONSTRAINT "CalendarSync_calendarId_fkey" FOREIGN KEY ("calendarId") REFERENCES "Calendar"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "CalendarSync" ADD CONSTRAINT "CalendarSync_mailCredentialId_fkey" FOREIGN KEY ("mailCredentialId") REFERENCES "MailCredentials"("id") ON DELETE SET NULL ON UPDATE CASCADE;