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) { // Combine date and time into one string (e.g., '2025-01-31T14:30') const dateTimeString = `${DateTime.fromISO(date).toFormat('yyyy-MM-dd')}T${time}`; console.log(dateTimeString); // Parse the combined date-time string in the given timezone const dateTimeInTimezone = DateTime.fromISO(dateTimeString, { zone: timezone }); console.log(dateTimeInTimezone); // Convert it to UTC const dateTimeInUTC = dateTimeInTimezone.toUTC(); console.log(dateTimeInUTC); // Return the UTC time as a string (ISO format) return dateTimeInUTC.toISO(); // Returns the date-time in ISO 8601 format (e.g., '2025-01-31T08:00:00.000Z') } async function isoToArray(isoString) { const dt = DateTime.fromISO(isoString, { zone: 'utc' }); // Ensure it's parsed as UTC return [dt.year, dt.month, dt.day, dt.hour, dt.minute]; } // Create Google Calendar Event 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}`); // Refresh access token if needed await checkAndRefreshAccessToken(); const calendar = google.calendar({ version: 'v3', auth: oAuth2Client }); const event = { summary: `Demo for ${product}`, description: `Demo for ${product}`, start: { dateTime: startDatetime, // Use the demo date here timeZone: 'UTC', }, end: { dateTime: endDatetime, // Assuming the demo lasts 30 minutes 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 .ics content as string } }); }); return [response.data, icsContent]; } catch (error) { console.error('Error creating Google Calendar event:', error); throw error; } } module.exports = {createGoogleCalendarEvent}