|
1 | 1 | import fs from 'fs'; |
2 | | -import { resolve } from 'path'; |
3 | | -import dotenv from 'dotenv'; |
4 | | -import { google } from 'googleapis'; |
5 | 2 | import { getCssStringFromCommittee, generateSingleEvent } from './lib.mjs'; |
6 | 3 |
|
7 | | -// .env config |
8 | | -dotenv.config({ path: '.env.local' }); |
9 | | -const SPREADSHEET_ID = process.env.EVENTS_SPREADSHEET_ID; |
10 | | -const SERVICE_ACCOUNT = process.env.SERVICE_ACCOUNT ?? ''; |
11 | | - |
12 | | -// Week one MONDAY of the quarter (y, m (base 0), d) |
13 | | -const FIRST_DAY_OF_QUARTER = new Date(2023, 0, 9); |
14 | | -const DAYS_OF_WEEK = [ |
15 | | - 'monday', |
16 | | - 'tuesday', |
17 | | - 'wednesday', |
18 | | - 'thursday', |
19 | | - 'friday', |
20 | | - 'saturday', |
21 | | - 'sunday', |
| 4 | +// Mock data for events |
| 5 | +const mockEvents = [ |
| 6 | + { |
| 7 | + id: 1, |
| 8 | + title: 'Event 1', |
| 9 | + start: '2023-01-01T10:00:00Z', |
| 10 | + end: '2023-01-01T12:00:00Z', |
| 11 | + committee: 'Cloud', |
| 12 | + location: 'Zoom', |
| 13 | + description: 'Description for Event 1', |
| 14 | + links: [], |
| 15 | + banner: '/event1-banner.png', |
| 16 | + }, |
| 17 | + { |
| 18 | + id: 2, |
| 19 | + title: 'Event 2', |
| 20 | + start: '2023-01-02T14:00:00Z', |
| 21 | + end: '2023-01-02T16:00:00Z', |
| 22 | + committee: 'Cloud', |
| 23 | + location: 'Room 101', |
| 24 | + description: 'Description for Event 2', |
| 25 | + links: [], |
| 26 | + banner: '/event2-banner.png', |
| 27 | + }, |
22 | 28 | ]; |
23 | 29 |
|
24 | | -// Grab all single and recurring events of Week n |
25 | | -// and write to output.json |
26 | | -async function writeAllEventsOfWeek(n) { |
27 | | - // Get events |
28 | | - let events = (await getSingleEventsOfWeek(n)).concat( |
29 | | - await getRecurringEventsOfWeek(n), |
30 | | - ); |
31 | | - const cleaned = events.filter((item) => item); |
32 | | - writeToOutput(cleaned); |
33 | | -} |
34 | | - |
35 | | -// Get all single and recurring events of the quarter |
36 | | -// Return as a list of JSONs |
| 30 | +// Get all events (mock implementation) |
37 | 31 | async function getAllEvents() { |
38 | | - // Get all single events |
39 | | - let promises = []; |
40 | | - for (let i = 1; i <= 10; i++) { |
41 | | - promises = promises.concat(getSingleEventsOfWeek(i)); |
42 | | - } |
43 | | - let events = await Promise.all(promises); |
44 | | - events = [].concat(...events); |
45 | | - |
46 | | - // Get all recurring events |
47 | | - let recurring_rows = await getGoogleSheetData('RECURRING EVENTS!A:J'); |
48 | | - for (let i = 1; i <= 10; i++) { |
49 | | - events = events.concat(getRecurringEventsOfWeek(recurring_rows, i)); |
50 | | - } |
51 | | - return events; |
52 | | -} |
53 | | - |
54 | | -// Read single events of Week n |
55 | | -// Return as array of JSON objects |
56 | | -async function getSingleEventsOfWeek(n) { |
57 | | - const rows = await getGoogleSheetData('Week ' + n + '!A:I'); |
58 | | - |
59 | | - const events = []; |
60 | | - for (const row of rows) { |
61 | | - // Skip header rows and example event |
62 | | - if ( |
63 | | - row.length < 5 || |
64 | | - row[0] === 'Committee' || |
65 | | - row[0].includes('Example:') |
66 | | - ) { |
67 | | - continue; |
68 | | - } |
69 | | - try { |
70 | | - events.push( |
71 | | - generateSingleEvent({ |
72 | | - id: null, |
73 | | - title: row[1], |
74 | | - start: null, |
75 | | - end: null, |
76 | | - committee: getCssStringFromCommittee(row[0]), |
77 | | - location: row[5] ?? '', |
78 | | - description: row[6] ?? '', |
79 | | - links: null, |
80 | | - rawStart: row[3], |
81 | | - rawEnd: row[4], |
82 | | - date: row[2], |
83 | | - fblink: row[7], |
84 | | - banner: row[8], |
85 | | - }), |
86 | | - ); |
87 | | - } catch (err) { |
88 | | - // eslint-disable-next-line no-console |
89 | | - console.error(`Error ${err} on event ${row}`); |
90 | | - } |
91 | | - } |
92 | | - |
93 | | - return events; |
94 | | -} |
95 | | - |
96 | | -// Read recurring events of Week n |
97 | | -// Return as array of JSON objects |
98 | | -function getRecurringEventsOfWeek(rows, n) { |
99 | | - const events = []; |
100 | | - for (const row of rows) { |
101 | | - // Skip header rows and example event |
102 | | - if ( |
103 | | - row.length < 5 || |
104 | | - row[0] === 'Committee' || |
105 | | - row[0].includes('Example:') |
106 | | - ) { |
107 | | - continue; |
108 | | - } |
109 | | - |
110 | | - // Check the current week is within the event's range |
111 | | - if (parseInt(row[2]) <= n && parseInt(row[3]) >= n) { |
112 | | - try { |
113 | | - // Calculate date of event for this week |
114 | | - const d = (n - 1) * 7 + DAYS_OF_WEEK.indexOf(row[4].toLowerCase()); |
115 | | - const date = new Date(FIRST_DAY_OF_QUARTER); |
116 | | - date.setDate(date.getDate() + d); |
117 | | - |
118 | | - events.push( |
119 | | - generateSingleEvent({ |
120 | | - id: null, |
121 | | - title: row[1], |
122 | | - start: null, |
123 | | - end: null, |
124 | | - committee: getCssStringFromCommittee(row[0]), |
125 | | - location: row[7] ?? '', |
126 | | - description: row[8] ?? '', |
127 | | - links: null, |
128 | | - rawStart: row[5], |
129 | | - rawEnd: row[6], |
130 | | - date: date.toISOString().split('T')[0], |
131 | | - fblink: row[9], |
132 | | - banner: row[10], |
133 | | - }), |
134 | | - ); |
135 | | - } catch (err) { |
136 | | - // eslint-disable-next-line no-console |
137 | | - console.error(`Error ${err} on event ${row}`); |
138 | | - } |
139 | | - } |
140 | | - } |
141 | | - |
142 | | - return events; |
| 32 | + return mockEvents; |
143 | 33 | } |
144 | 34 |
|
145 | | -//////////////////////////////////////////////////////// |
146 | | -// Helper Functions |
147 | | -//////////////////////////////////////////////////////// |
148 | | - |
149 | | -// Read data from Google sheets |
150 | | -// using sheet range (eg: 'Week 1!A:H) |
151 | | -async function getGoogleSheetData(range) { |
152 | | - const sheets = google.sheets({ version: 'v4' }); |
153 | | - |
154 | | - // Get JWT Token to access sheet |
155 | | - const service_account = JSON.parse(SERVICE_ACCOUNT); |
156 | | - const jwtClient = new google.auth.JWT( |
157 | | - service_account.client_email, |
158 | | - '', |
159 | | - service_account.private_key, |
160 | | - ['https://www.googleapis.com/auth/spreadsheets'], |
161 | | - ); |
162 | | - jwtClient.authorize(function (err) { |
163 | | - if (err) { |
164 | | - throw err; |
165 | | - } |
166 | | - }); |
167 | | - |
168 | | - // Get data from Google spreadsheets |
169 | | - const res = await sheets.spreadsheets.values.get({ |
170 | | - auth: jwtClient, |
171 | | - spreadsheetId: SPREADSHEET_ID, |
172 | | - range: range, |
173 | | - }); |
174 | | - const rows = res?.data.values; |
175 | | - if (!rows || rows.length == 0) { |
176 | | - console.log('Error: no data found'); |
177 | | - return []; |
178 | | - } |
179 | | - |
180 | | - // // Replacing the new lines with <br/> (doesnt work tho) |
181 | | - // const formatRows = rows.map((row) => row.map( (r) => r.replace(/\n/g, '<br/>'))); |
182 | | - // return formatRows; |
183 | | - |
184 | | - return rows; |
185 | | -} |
186 | | - |
187 | | -// write events (list of event jsons) to output.json |
| 35 | +// Write events (list of event JSONs) to output.json |
188 | 36 | function writeToOutput(events) { |
189 | | - // Write to output.json |
190 | 37 | const out = JSON.stringify(events); |
191 | 38 | fs.writeFile('output.json', out, (err) => { |
192 | 39 | if (err) throw err; |
193 | | - // eslint-disable-next-line no-console |
194 | 40 | console.log('Output successfully saved to output.json'); |
195 | 41 | }); |
196 | 42 | } |
197 | 43 |
|
| 44 | +// Filter single events of a specific committee |
| 45 | +function getSingleEventsOfWeek(events) { |
| 46 | + return events.filter((event) => { |
| 47 | + if (!event.committee) return false; // Add null check for committee |
| 48 | + return event.committee.includes('Cloud'); // Example logic |
| 49 | + }); |
| 50 | +} |
| 51 | + |
198 | 52 | export default getAllEvents; |
0 commit comments