|
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | +import { transformGoogleEventsToSchedule, GoogleCalendarEvent } from '@/lib/hackathons/schedule-strategy'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Google Calendar API route |
| 6 | + * |
| 7 | + * Fetches events from a PUBLIC Google Calendar using an API Key. |
| 8 | + * Requires GOOGLE_CALENDAR_API_KEY environment variable. |
| 9 | + * |
| 10 | + * Query parameters: |
| 11 | + * - calendarId: The Google Calendar ID (required) |
| 12 | + * - maxResults: Maximum number of events to fetch (default: 100) |
| 13 | + * - timeMin: Only fetch events after this ISO date |
| 14 | + * - timeMax: Only fetch events before this ISO date |
| 15 | + */ |
| 16 | + |
| 17 | +export async function GET(request: NextRequest) { |
| 18 | + try { |
| 19 | + const apiKey = process.env.NEXT_PUBLIC_GOOGLE_CALENDAR_API_KEY; |
| 20 | + |
| 21 | + if (!apiKey) { |
| 22 | + return NextResponse.json( |
| 23 | + { error: 'NEXT_PUBLIC_GOOGLE_CALENDAR_API_KEY not configured' }, |
| 24 | + { status: 500 } |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + const { searchParams } = new URL(request.url); |
| 29 | + |
| 30 | + const calendarId = searchParams.get('calendarId'); |
| 31 | + const maxResults = searchParams.get('maxResults') || '100'; |
| 32 | + const timeMin = searchParams.get('timeMin'); |
| 33 | + const timeMax = searchParams.get('timeMax'); |
| 34 | + |
| 35 | + if (!calendarId) { |
| 36 | + return NextResponse.json( |
| 37 | + { error: 'calendarId is required' }, |
| 38 | + { status: 400 } |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // Fetch all events using pagination |
| 43 | + const allEvents: GoogleCalendarEvent[] = []; |
| 44 | + let pageToken: string | undefined = undefined; |
| 45 | + |
| 46 | + do { |
| 47 | + // Build Google Calendar API URL |
| 48 | + const apiParams = new URLSearchParams({ |
| 49 | + key: apiKey, |
| 50 | + maxResults: '250', // Max allowed per request |
| 51 | + singleEvents: 'true', |
| 52 | + orderBy: 'startTime', |
| 53 | + // Include conference data (Google Meet links, etc.) |
| 54 | + conferenceDataVersion: '1', |
| 55 | + }); |
| 56 | + |
| 57 | + if (timeMin) { |
| 58 | + apiParams.set('timeMin', timeMin); |
| 59 | + } |
| 60 | + if (timeMax) { |
| 61 | + apiParams.set('timeMax', timeMax); |
| 62 | + } |
| 63 | + if (pageToken) { |
| 64 | + apiParams.set('pageToken', pageToken); |
| 65 | + } |
| 66 | + |
| 67 | + const calendarUrl = `https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(calendarId)}/events?${apiParams.toString()}`; |
| 68 | + |
| 69 | + const response = await fetch(calendarUrl, { |
| 70 | + next: { revalidate: 60 }, // Cache for 1 minute (reduced for faster updates) |
| 71 | + }); |
| 72 | + |
| 73 | + if (!response.ok) { |
| 74 | + const error = await response.text(); |
| 75 | + console.error('Google Calendar API error:', error); |
| 76 | + |
| 77 | + if (response.status === 404) { |
| 78 | + return NextResponse.json( |
| 79 | + { error: 'Calendar not found. Make sure the calendar is public and the ID is correct.' }, |
| 80 | + { status: 404 } |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + return NextResponse.json( |
| 85 | + { error: `Google Calendar API error: ${response.status}` }, |
| 86 | + { status: response.status } |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + const data = await response.json(); |
| 91 | + const events = (data.items || []) as GoogleCalendarEvent[]; |
| 92 | + allEvents.push(...events); |
| 93 | + |
| 94 | + // Get next page token for pagination |
| 95 | + pageToken = data.nextPageToken; |
| 96 | + } while (pageToken); |
| 97 | + |
| 98 | + const events = allEvents; |
| 99 | + |
| 100 | + // Transform to ScheduleActivity format |
| 101 | + const schedule = transformGoogleEventsToSchedule(events); |
| 102 | + |
| 103 | + // Get calendar timezone from first event or calendar metadata |
| 104 | + const calendarTimeZone = events[0]?.start?.timeZone || ''; |
| 105 | + |
| 106 | + return NextResponse.json({ |
| 107 | + schedule, |
| 108 | + totalEvents: events.length, |
| 109 | + calendarId, |
| 110 | + timeZone: calendarTimeZone, |
| 111 | + }); |
| 112 | + } catch (error) { |
| 113 | + console.error('Google Calendar API error:', error); |
| 114 | + |
| 115 | + return NextResponse.json( |
| 116 | + { error: error instanceof Error ? error.message : 'Failed to fetch calendar events' }, |
| 117 | + { status: 500 } |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments