Skip to content

Commit

Permalink
Fixed formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmouris committed May 4, 2024
1 parent 5d1b71f commit c0e24d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default defineNuxtConfig({
},
googleCalendarAPI: {
googleCalendarId: process.env.GOOGLE_CALENDAR_ID,
serviceAccountCredentialsJSON: process.env.GOOGLE_SERVICE_ACCOUNT_CREDENTIALS_JSON
}
serviceAccountCredentialsJSON: process.env.GOOGLE_SERVICE_ACCOUNT_CREDENTIALS_JSON,
},
},

app: {
Expand Down
39 changes: 22 additions & 17 deletions server/api/events.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { google } from 'googleapis';
import { JWT } from 'google-auth-library';
import { google } from 'googleapis'
import { JWT } from 'google-auth-library'

/*
Reference for folks who maybe curious.
- The google calendar id of the user you want to follow the events.
- Refer to here https://it.umn.edu/services-technologies/how-tos/google-calendar-find-your-google#:~:text=Click%20on%20the%20three%20vertical,will%20find%20your%20Calendar%20ID.
- A service account
- A service account
- Refer to here https://dev.to/pedrohase/create-google-calender-events-using-the-google-api-and-service-accounts-in-nodejs-22m8
- The google calendar user needs to give the "Make Changes to Events" permissions to the service account
- The google calendar user needs to give the "Make Changes to Events" permissions to the service account
- Refer to here https://dev.to/pedrohase/create-google-calender-events-using-the-google-api-and-service-accounts-in-nodejs-22m8
*/

Expand All @@ -25,15 +25,16 @@ const getEvents = async ({
googleCalendarId,
serviceAccountCredentials,
startDate,
limitEvents
limitEvents,
}) => {
if(!startDate) {
if (!startDate) {
startDate = new Date().toISOString()
} else {
}
else {
startDate = new Date(startDate).toISOString()
}

if(!limitEvents) {
if (!limitEvents) {
limitEvents = 10
}

Expand All @@ -45,7 +46,7 @@ const getEvents = async ({
'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' })
Expand All @@ -57,7 +58,7 @@ const getEvents = async ({
maxResults: limitEvents,
singleEvents: true,
orderBy: 'startTime',
});
})

// return the events.
return resposnse.data.items
Expand All @@ -72,21 +73,24 @@ const getEvents = async ({
* // GET /api/events?startDate=2024-05-01&limitEvents=25
* // GET /api/events
*/
// eslint-disable-next-line
export default defineEventHandler(async (event) => {
// get the query params
// eslint-disable-next-line
const { startDate, limitEvents } = getQuery(event)

// get the credentials from the service account
const { googleCalendarId, serviceAccountCredentialsJSON } = useRuntimeConfig(event).googleCalendarAPI
let serviceAccountCredentials;

let serviceAccountCredentials
try {
serviceAccountCredentials = JSON.parse(serviceAccountCredentialsJSON)
} catch (error) {
}
catch {
return {
statusCode: 400,
message: 'No Service Account Credentials Provided',
error: "Service Account Credentials are not provided",
error: 'Service Account Credentials are not provided',
}
}

Expand All @@ -104,11 +108,12 @@ export default defineEventHandler(async (event) => {
message: 'Success',
events,
}
} catch (error) {
}
catch (error) {
return {
statusCode: 400,
message: 'Bad Request',
error: error.message,
}
}
})
})

0 comments on commit c0e24d7

Please sign in to comment.