Skip to content

Commit

Permalink
Merge pull request #455 from dgmouris/events_importer_394
Browse files Browse the repository at this point in the history
Auto Event importing from Meetup Sources through single server endpoint.
  • Loading branch information
dgmouris authored Jan 30, 2025
2 parents 69f6d13 + ac1c3fa commit 6d8d394
Show file tree
Hide file tree
Showing 5 changed files with 478 additions and 88 deletions.
153 changes: 130 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"google-auth-library": "^9.14.2",
"googleapis": "^144.0.0",
"happy-dom": "^15.7.4",
"ical.js": "^2.1.0",
"node-fetch": "^3.3.2",
"nuxt": "^3.13.2",
"swiper": "^11.1.4",
"tailwind-merge": "^2.5.4",
Expand Down
66 changes: 1 addition & 65 deletions server/api/events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { google } from 'googleapis'
import { JWT } from 'google-auth-library'
import { getEvents } from '../utils/calendar'

/*
Reference for folks who maybe curious.
Expand All @@ -11,69 +10,6 @@ Reference for folks who maybe curious.
- Refer to here https://dev.to/pedrohase/create-google-calender-events-using-the-google-api-and-service-accounts-in-nodejs-22m8
*/

/**
* Retrieves events from Google Calendar.
* @async
* @param {Object} options - The options object.
* @param {string} options.googleCalendarId - The ID of the Google Calendar should be an email.
* @param {Object} options.serviceAccountCredentials - The service account credentials for google authentication.
* @param {Date} options.startDate - The start date from which to retrieve events.
* @param {number} options.limitEvents - The maximum number of events to retrieve.
* @returns {Promise<Array>} An array of events.
*/
const getEvents = async ({
googleCalendarId,
serviceAccountCredentials,
startDate,
limitEvents,
}) => {
if (!startDate) {
startDate = new Date().toISOString()
}
else {
startDate = new Date(startDate).toISOString()
}

if (!limitEvents) {
limitEvents = 10
}

// get the credentials from the service account
const client = new JWT({
email: serviceAccountCredentials.client_email,
key: serviceAccountCredentials.private_key,
scopes: [ // set the right scope
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.events',
],
})

// get the calendar api using google
const calendar = google.calendar({ version: 'v3' })
// get the events from the calendar
const resposnse = await calendar.events.list({
auth: client,
calendarId: googleCalendarId,
timeMin: startDate,
maxResults: limitEvents,
singleEvents: true,
orderBy: 'startTime',
})

// return the events.
return resposnse.data.items
}
/**
* API endpoint for fetching events for DES.
* @param {Object} event - The event object.
* @param {string} startDate - Start date of the events you want to fetch (query param).
* @param {number} limitEvents - Limit the number of events to fetch (query param)
* @example
* // Example usage:
* // GET /api/events?startDate=2024-05-01&limitEvents=25
* // GET /api/events
*/

export default defineEventHandler(async (event) => {
// get the query params

Expand Down
Loading

0 comments on commit 6d8d394

Please sign in to comment.