Agenda Sync refactor
This commit is contained in:
parent
b67143c2e8
commit
6b2573e35d
@ -44,101 +44,24 @@ 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 with mission relation
|
||||||
// Try to use mission relation if available, otherwise fetch separately
|
const 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: {
|
mission: {
|
||||||
events: {
|
include: {
|
||||||
orderBy: {
|
missionUsers: true
|
||||||
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 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 no calendars exist, create default ones
|
||||||
if (calendars.length === 0) {
|
if (calendars.length === 0) {
|
||||||
|
|||||||
@ -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;
|
||||||
Loading…
Reference in New Issue
Block a user