google calendar | Update create merge delete event | | Search

This code creates a function that simplifies the process of adding new events to a Google Calendar, handling calendar ID validation and API calls.

Run example

npm run import -- "create new calendar event"

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;

What the code could have been:

// 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:

  1. Imports:

  2. createNewEvent Function:

Purpose:

This function provides a convenient way to create new events in a Google Calendar, handling calendar ID lookup and API interaction.