This code creates a function that simplifies the process of adding new events to a Google Calendar, handling calendar ID validation and API calls.
npm run import -- "create new calendar event"
var importer = require('../Core');
var {google} = require('googleapis');
var util = require('util');
var {
correctCalendarId
} = importer.import("lookup calendar name",
"import google calendar api");
function createNewEvent(summary, description, options) {
const now = new Date();
options = options || {};
const event = {
summary: summary,
description: description,
start: {dateTime: new Date(now.getTime())},
end: {dateTime: new Date(now.getTime() + 30 * 60 * 1000)}
}
if(typeof summary === 'object') {
Object.assign(event, summary);
options = description;
}
return correctCalendarId(options)
.then(() => util.promisify(
calendar.events.insert.bind(calendar.events, {
calendarId: options.calendarId,
auth: options.auth,
resource: event
})))
}
module.exports = createNewEvent;
// Import required modules
const { google } = require('googleapis');
const { correctCalendarId } = require('../Core');
const console = require('console');
/**
* Creates a new event on the specified Google Calendar.
*
* @param {string|object} summary - The summary of the event.
* @param {string} description - A description of the event.
* @param {object} options - Options for the event.
* @param {string} options.calendarId - The ID of the calendar to add the event to.
* @param {object} options.auth - The authentication credentials.
* @returns {Promise} A promise that resolves when the event is created.
*/
async function createNewEvent(summary, description, options = {}) {
// Get the current date and time
const now = new Date();
// Create a new event object
const event = {
summary: summary.toString(), // Ensure summary is a string
description,
start: { dateTime: new Date(now.getTime()) }, // Use camelCase property names
end: { dateTime: new Date(now.getTime() + 30 * 60 * 1000) }
};
// If summary is an object, merge it with the event object
if (typeof summary === 'object') {
Object.assign(event, summary);
// Update description to be the last argument (if multiple)
const args = Array.prototype.slice.call(arguments);
args.shift();
args.shift();
description = args.pop();
}
// Get the correct calendar ID
const calendarId = await correctCalendarId(options);
// Create the event on the specified calendar
const calendar = google.calendar('v3');
const response = await calendar.events.insert({
auth: options.auth,
calendarId,
resource: event
});
// Log the event ID
console.log(`Event created: ${response.data.id}`);
return response.data.id;
}
module.exports = createNewEvent;
This code snippet defines a function createNewEvent
that creates a new event in a Google Calendar.
Here's a breakdown:
Imports:
createNewEvent
Function:
summary
, description
, and optional options
as arguments.summary
is an object, it assumes it contains event details and assigns them to the event object, using description
as the options
argument.correctCalendarId
to ensure the calendar ID is valid.util.promisify
to create a promise-based function for inserting the event into the specified calendar using the Google Calendar API.Purpose:
This function provides a convenient way to create new events in a Google Calendar, handling calendar ID lookup and API interaction.