googleapis#calendar_v3 TypeScript Examples

The following examples show how to use googleapis#calendar_v3. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: gcal.ts    From tams-club-cal with MIT License 5 votes vote down vote up
/**
 * Updates the event on the recurring calendar, lengthening or shortening the event if needed
 *
 * @param data New event data
 * @param id ID of the calendar event to update
 * @param isLonger True if event repeats for longer; will return list ids
 */
export async function updateRecurringCalendar(
    data: EventObject,
    id: string,
    isLonger: boolean = false
): Promise<calendar_v3.Schema$Event[]> {
    // Calculate start time
    const start = data.allDay
        ? { date: dayjs(data.start).format('YYYY-MM-DD'), timeZone: 'Etc/UTC' }
        : { dateTime: new Date(data.start).toISOString(), timeZone: 'Etc/UTC' };

    // Calculate end time
    const end = data.allDay
        ? { date: dayjs(data.start).format('YYYY-MM-DD'), timeZone: 'Etc/UTC' }
        : { dateTime: new Date(data.end).toISOString(), timeZone: 'Etc/UTC' };

    // Calculate repeating and end status
    const freq = data.repeats === RepeatingStatus.WEEKLY ? 'WEEKLY' : 'MONTHLY';
    const until = dayjs(data.repeatsUntil).startOf('day').subtract(1, 'second').format('YYYYMMDD');

    await google.calendar('v3').events.update({
        calendarId: process.env.CALENDAR_ID,
        eventId: id,
        requestBody: {
            start,
            end,
            summary: `${data.name} (${data.club})`,
            description: data.description,
            recurrence: [`RRULE:FREQ=${freq};UNTIL=${until}`],
        },
    });

    if (!isLonger) return null;

    // Get the list of all repeating events
    const res = await google.calendar('v3').events.instances({
        calendarId: process.env.CALENDAR_ID,
        eventId: id,
    });

    // Return those items lol
    return res.data.items;
}