|
3 | 3 | import { promises as fs } from "node:fs"; |
4 | 4 | import path from "node:path"; |
5 | 5 | import { endOfMonth } from "date-fns"; |
6 | | -import { JWT } from "google-auth-library"; |
7 | | -import { GoogleSpreadsheet } from "google-spreadsheet"; |
8 | | -import type { iconsMap } from "@/constants"; |
9 | | -import type { EventSubmissionFormValues } from "@/lib/forms-config"; |
10 | | -import type { IEvent, TEventColor } from "@/types"; |
11 | | - |
12 | | -const serviceAccountAuth = new JWT({ |
13 | | - email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, |
14 | | - key: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, "\n"), |
15 | | - scopes: ["https://www.googleapis.com/auth/spreadsheets"], |
16 | | -}); |
17 | | - |
18 | | -const doc = new GoogleSpreadsheet( |
19 | | - process.env.GOOGLE_SHEET_ID ?? "", |
20 | | - serviceAccountAuth, |
21 | | -); |
| 6 | +import type { IEvent } from "@/types"; |
22 | 7 |
|
23 | 8 | const localEventsPath = path.join(process.cwd(), "src/data/local-events.json"); |
| 9 | +const backendUrl = process.env.BACKEND_URL; |
24 | 10 |
|
25 | 11 | async function readLocalEvents(): Promise<IEvent[]> { |
26 | 12 | try { |
@@ -63,80 +49,25 @@ export async function getEvents( |
63 | 49 | return filterEventsByRange(localEvents, startDate, endDate); |
64 | 50 | } |
65 | 51 |
|
66 | | - if ( |
67 | | - !process.env.GOOGLE_SHEET_ID || |
68 | | - !process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL || |
69 | | - !process.env.GOOGLE_PRIVATE_KEY |
70 | | - ) { |
71 | | - console.warn("getEvents: Missing Google Sheets configuration."); |
| 52 | + if (!backendUrl) { |
| 53 | + console.warn("getEvents: Missing BACKEND_URL configuration."); |
72 | 54 | return []; |
73 | 55 | } |
74 | 56 |
|
75 | 57 | try { |
76 | | - await doc.loadInfo(); |
77 | | - const sheet = doc.sheetsByIndex[3]; |
78 | | - if (!sheet) throw new Error("Sheet not found"); |
79 | | - |
80 | | - const rows = await sheet.getRows(); |
81 | | - const res: IEvent[] = rows |
82 | | - .map((row): IEvent | null => { |
83 | | - const eventData = |
84 | | - row.toObject() as unknown as EventSubmissionFormValues & { |
85 | | - ID: string; |
86 | | - }; |
87 | | - const isDK24 = eventData.organizationName === "DK24"; |
88 | | - |
89 | | - const startDateTime = new Date(eventData.startDateTime); |
90 | | - const endDateTime = new Date(eventData.endDateTime); |
91 | | - |
92 | | - if ( |
93 | | - Number.isNaN(startDateTime.getTime()) || |
94 | | - Number.isNaN(endDateTime.getTime()) |
95 | | - ) { |
96 | | - return null; |
97 | | - } |
98 | | - |
99 | | - return { |
100 | | - id: eventData.ID, |
101 | | - title: eventData.eventName, |
102 | | - startDateTime: startDateTime.toISOString(), |
103 | | - endDateTime: endDateTime.toISOString(), |
104 | | - time: eventData.startDateTime.split("T")[1]?.substring(0, 5) || "", |
105 | | - location: eventData.eventLocation, |
106 | | - description: eventData.eventDescription, |
107 | | - registrationLink: eventData.registrationLink || "", |
108 | | - joinLink: eventData.eventWebsite || "", |
109 | | - icon: "calendar" as keyof typeof iconsMap, |
110 | | - highlight: isDK24, |
111 | | - youtubeLink: "", |
112 | | - color: (isDK24 ? "green" : "gray") as TEventColor, |
113 | | - organizationName: eventData.organizationName, |
114 | | - posterUrl: eventData.eventPosterUrl, |
115 | | - tags: (() => { |
116 | | - const raw = eventData.eventTags as unknown; |
117 | | - if (Array.isArray(raw)) return raw as string[]; |
118 | | - if (typeof raw === "string") { |
119 | | - let s = (raw as string).trim(); |
120 | | - if (s.startsWith("[") && s.endsWith("]")) { |
121 | | - s = s.slice(1, -1); |
122 | | - } |
123 | | - return s |
124 | | - .split(",") |
125 | | - .map((t: string) => t.replace(/^['"]|['"]$/g, "").trim()) |
126 | | - .filter(Boolean) as string[]; |
127 | | - } |
128 | | - return []; |
129 | | - })(), |
130 | | - }; |
131 | | - }) |
132 | | - .filter((event): event is IEvent => event !== null); |
| 58 | + const response = await fetch(`${backendUrl}/api/events`, { |
| 59 | + next: { revalidate: 300, tags: ["calendar"] }, |
| 60 | + }); |
| 61 | + |
| 62 | + if (!response.ok) { |
| 63 | + throw new Error(`Calendar API request failed with ${response.status}`); |
| 64 | + } |
133 | 65 |
|
134 | | - return filterEventsByRange(res, startDate, endDate); |
| 66 | + const data = (await response.json()) as { events?: IEvent[] }; |
| 67 | + const events = Array.isArray(data.events) ? data.events : []; |
| 68 | + return filterEventsByRange(events, startDate, endDate); |
135 | 69 | } catch (error) { |
136 | | - console.error( |
137 | | - "getEvents: Failed to load events from Google Sheets.", |
138 | | - error, |
139 | | - ); |
| 70 | + console.error("getEvents: Failed to load events from Calendar API.", error); |
140 | 71 | return []; |
141 | 72 | } |
142 | 73 | } |
|
0 commit comments