|
const { checkAndRefreshAccessToken } = require('./refreshToken'); |
|
const { google } = require('googleapis'); |
|
const { oAuth2Client } = require('../config/googleOAuth'); |
|
const { DateTime } = require('luxon'); |
|
const ics = require('ics'); |
|
const { v4: uuid } = require('uuid'); |
|
const fs = require('fs'); |
|
const path = require('path'); |
|
|
|
async function convertToUTC(date, time, timezone) { |
|
|
|
const dateTimeString = `${DateTime.fromISO(date).toFormat('yyyy-MM-dd')}T${time}`; |
|
console.log(dateTimeString); |
|
|
|
|
|
const dateTimeInTimezone = DateTime.fromISO(dateTimeString, { zone: timezone }); |
|
console.log(dateTimeInTimezone); |
|
|
|
|
|
const dateTimeInUTC = dateTimeInTimezone.toUTC(); |
|
console.log(dateTimeInUTC); |
|
|
|
|
|
return dateTimeInUTC.toISO(); |
|
} |
|
|
|
async function isoToArray(isoString) { |
|
const dt = DateTime.fromISO(isoString, { zone: 'utc' }); |
|
return [dt.year, dt.month, dt.day, dt.hour, dt.minute]; |
|
} |
|
|
|
|
|
|
|
async function createGoogleCalendarEvent(name, email, demoDate, selectedSlot, product, timezone) { |
|
|
|
const [startTime, endTime] = selectedSlot.split(' (')[0].split(' - '); |
|
|
|
const startDatetime = await convertToUTC(demoDate, startTime, timezone); |
|
const endDatetime = await convertToUTC(demoDate, endTime, timezone); |
|
|
|
console.log(`Times: ${startDatetime}, ${endDatetime}`); |
|
|
|
|
|
await checkAndRefreshAccessToken(); |
|
|
|
const calendar = google.calendar({ version: 'v3', auth: oAuth2Client }); |
|
|
|
const event = { |
|
summary: `Demo for ${product}`, |
|
description: `Demo for ${product}`, |
|
start: { |
|
dateTime: startDatetime, |
|
timeZone: 'UTC', |
|
}, |
|
end: { |
|
dateTime: endDatetime, |
|
timeZone: 'UTC', |
|
}, |
|
colorId: 1, |
|
conferenceData: { |
|
createRequest: { |
|
requestId: uuid(), |
|
} |
|
}, |
|
attendees: [ |
|
{email: email}, |
|
] |
|
}; |
|
|
|
try { |
|
const response = await calendar.events.insert({ |
|
calendarId: 'primary', |
|
conferenceDataVersion: 1, |
|
resource: event, |
|
sendUpdates: 'all' |
|
}); |
|
console.log('Google Calendar Event Created:', response.data); |
|
|
|
const dateArray = await isoToArray(startDatetime); |
|
const icsevent = { |
|
start: dateArray, |
|
startInputType: 'utc', |
|
duration: { minutes: 30 }, |
|
title: `Demo for ${product}`, |
|
description: `Product demonstration for ${product}`, |
|
url: response.data.hangoutLink, |
|
categories: ['Product Demo'], |
|
status: 'CONFIRMED', |
|
busyStatus: 'BUSY', |
|
attendees: [ |
|
{ name: name, email: email, rsvp: true, partstat: 'ACCEPTED', role: 'REQ-PARTICIPANT' }, |
|
] |
|
} |
|
|
|
const icsFilePath = path.join(__dirname, '../demo-event.ics'); |
|
const icsContent = await new Promise((resolve, reject) => { |
|
ics.createEvent(icsevent, (error, value) => { |
|
if (error) { |
|
reject(error); |
|
} else { |
|
resolve(value); |
|
} |
|
}); |
|
}); |
|
return [response.data, icsContent]; |
|
} catch (error) { |
|
console.error('Error creating Google Calendar event:', error); |
|
throw error; |
|
} |
|
} |
|
|
|
module.exports = {createGoogleCalendarEvent} |