@@ -4,8 +4,6 @@ const process = require('process');
44const { authenticate } = require ( '@google-cloud/local-auth' ) ;
55const { google } = require ( 'googleapis' ) ;
66
7- console . log ( "I am working." ) ;
8-
97// If modifying these scopes, delete token.json.
108const SCOPES = [ 'https://www.googleapis.com/auth/calendar' ] ;
119// The file token.json stores the user's access and refresh tokens, and is
@@ -14,56 +12,76 @@ const SCOPES = ['https://www.googleapis.com/auth/calendar'];
1412const TOKEN_PATH = path . join ( process . cwd ( ) , 'token.json' ) ;
1513const CREDENTIALS_PATH = path . join ( process . cwd ( ) , 'credentials.json' ) ;
1614
17- /**
18- * Creates a new event on the user's primary calendar.
19- * @param {google.auth.OAuth2 } auth An authorized OAuth2 client.
20- * @param {Object } eventBody The event details.
21- */
22- async function createEvent ( auth , eventBody ) {
15+ async function createEvents ( auth , eventBodiesArray ) {
2316 const calendar = google . calendar ( { version : 'v3' , auth } ) ;
2417
25- try {
26- const event = await calendar . events . insert ( {
18+ const promises = eventBodiesArray . map ( eventBody => {
19+ return calendar . events . insert ( {
2720 calendarId : 'primary' ,
2821 resource : eventBody ,
22+ } ) . then ( event => {
23+ console . log ( `Event created: ${ event . data . htmlLink } ` ) ;
24+ return event ; // Return event for further processing if needed.
25+ } ) . catch ( error => {
26+ console . error ( 'Error creating event' , error ) ;
27+ return null ; // Return null or appropriate error handling.
2928 } ) ;
30- console . log ( `Event created: ${ event . data . htmlLink } ` ) ;
29+ } ) ;
30+
31+ try {
32+ const results = await Promise . all ( promises ) ;
33+ // Optional: Process results or perform further actions here.
3134 } catch ( error ) {
32- console . error ( 'Error creating event ' , error ) ;
35+ console . error ( 'An error occurred with Promise.all ' , error ) ;
3336 }
3437}
3538
36- /**
37- * Creates an object literal representing the event details.
38- * @param {string } summary The summary of the event.
39- * @param {string } location The location of the event.
40- * @param {string } description The description of the event.
41- * @param {Date } startDateTime The start date and time of the event.
42- * @param {Date } endDateTime The end date and time of the event.
43- * @returns {Object } The event object literal.
44- */
39+ function createEventObjects ( optionsArray ) {
40+ // Initialize an array to hold all event objects
41+ let events = [ ] ;
42+
43+ // Iterate over each set of options
44+ optionsArray . forEach ( options => {
45+ let event = {
46+ 'summary' : options . summary ,
47+ 'location' : options . location ,
48+ 'description' : options . description ,
49+ // Initialize start and end events without dateTime or date
50+ 'start' : { } ,
51+ 'end' : { }
52+ } ;
53+
54+ // Check if it's an all day event
55+ if ( options . allDay ) {
56+ event . start . date = options . start ;
57+ event . end . date = options . end ;
58+ event . start . timeZone = 'GMT+2' ;
59+ event . end . timeZone = 'GMT+2' ;
60+ } else {
61+ event . start . dateTime = options . start ;
62+ event . end . dateTime = options . end ;
63+ event . start . timeZone = options . startTimeZone || 'Etc/GMT+2' ;
64+ event . end . timeZone = options . endTimeZone || 'Etc/GMT+2' ;
65+ }
66+
67+ // Add recurrence if provided
68+ if ( options . recurrence ) {
69+ event . recurrence = options . recurrence ;
70+ }
71+
72+ // Add reminders if provided
73+ if ( options . reminders ) {
74+ event . reminders = {
75+ 'useDefault' : false ,
76+ 'overrides' : options . reminders
77+ } ;
78+ }
79+
80+ // Add the constructed event to the events array
81+ events . push ( event ) ;
82+ } ) ;
4583
46- function createEventObject ( summary , location , description , startDateTime , endDateTime ) {
47- return {
48- 'summary' : summary ,
49- 'location' : location ,
50- 'description' : description ,
51- 'start' : {
52- 'dateTime' : startDateTime . toISOString ( ) ,
53- 'timeZone' : 'America/Los_Angeles' , // Adjust to the desired timezone
54- } ,
55- 'end' : {
56- 'dateTime' : endDateTime . toISOString ( ) ,
57- 'timeZone' : 'America/Los_Angeles' , // Adjust to the desired timezone
58- } ,
59- 'reminders' : {
60- 'useDefault' : false ,
61- 'overrides' : [
62- { 'method' : 'email' , 'minutes' : 24 * 60 } ,
63- { 'method' : 'popup' , 'minutes' : 10 } ,
64- ] ,
65- } ,
66- } ;
84+ return events ; // Returns an array of event objects
6785}
6886
6987/**
@@ -128,46 +146,23 @@ async function authorize() {
128146 return client ;
129147}
130148
131- /**
132- * Lists the next 10 events on the user's primary calendar.
133- * @param {google.auth.OAuth2 } auth An authorized OAuth2 client.
134- */
135- async function listEvents ( auth ) {
136- const calendar = google . calendar ( { version : 'v3' , auth } ) ;
137- const res = await calendar . events . list ( {
138- calendarId : 'primary' ,
139- timeMin : new Date ( ) . toISOString ( ) ,
140- maxResults : 5 ,
141- singleEvents : true ,
142- orderBy : 'startTime' ,
143- } ) ;
144- const events = res . data . items ;
145- if ( ! events || events . length === 0 ) {
146- console . log ( 'No upcoming events found.' ) ;
147- return ;
148- }
149- console . log ( 'Upcoming 10 events:' ) ;
150- events . map ( ( event , i ) => {
151- const start = event . start . dateTime || event . start . date ;
152- console . log ( `${ start } - ${ event . summary } ` ) ;
153- } ) ;
154- }
155-
156- authorize ( ) . then ( listEvents ) . catch ( console . error ) ;
157-
158- // Example usage
159149authorize ( ) . then ( auth => {
150+ // Preparing the date format for an all-day event
151+ const startDate = new Date ( '2024-05-15' ) . toISOString ( ) . substring ( 0 , 10 ) ; // Converting to 'YYYY-MM-DD' format
152+ const endDate = new Date ( '2024-05-16' ) . toISOString ( ) . substring ( 0 , 10 ) ; // All-day event end date should be the next day in 'YYYY-MM-DD' format
160153
161154 // Creating an event object
162- const newEvent = createEventObject (
163- 'Team Meeting' , // Summary
164- 'Online' , // Location
165- 'Discuss project progress' , // Description
166- new Date ( '2024-05-15T09:00:00' ) , // Start date and time
167- new Date ( '2024-05-15T10:00:00' ) // End date and time
168- ) ;
169-
170- // Creating the event on Google Calendar
171- createEvent ( auth , newEvent ) . catch ( console . error ) ;
155+ const newEvent = createEventObjects ( [ {
156+ summary : 'Team Meeting - All Day' , // Summary
157+ location : 'Online' , // Location
158+ description : 'Discuss project progress' , // Description
159+ start : startDate , // Start date for all-day event
160+ end : endDate , // End date for all-day event
161+ allDay : true , // Marking this event as an all-day event
162+ // Time zone is irrelevant for all-day events but set for consistency if needed
163+ } ] ) ;
164+
165+ // Assuming createEvent is a function that creates an event on Google Calendar using the provided authentication and event object
166+ createEvents ( auth , newEvent ) . catch ( console . error ) ;
172167
173168} ) . catch ( console . error ) ;
0 commit comments