Skip to content

Commit

Permalink
Merge pull request #4344 from kishanprmr/google-calendar
Browse files Browse the repository at this point in the history
fix(google-calendar): fix update event action
  • Loading branch information
abuaboud committed Apr 4, 2024
2 parents 735c4a8 + 8724f00 commit 5c8425a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/pieces/community/google-calendar/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-google-calendar",
"version": "0.5.2"
"version": "0.5.3"
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,121 @@
import { Property, createAction } from '@activepieces/pieces-framework';
import { google } from 'googleapis';
import { google, calendar_v3 } from 'googleapis';
import { OAuth2Client } from 'googleapis-common';
import { googleCalendarAuth } from '../../index';
import { googleCalendarCommon } from '../common';
import dayjs from 'dayjs';

export const updateEventAction = createAction({
displayName: 'Update Event',
auth: googleCalendarAuth,
name: 'update_event',
description: 'Updates an event in Google Calendar.',
props: {
calendar_id: googleCalendarCommon.calendarDropdown('writer'),
eventId: Property.ShortText({
displayName: 'Event ID',
required: true,
}),
title: Property.ShortText({
displayName: 'Title of the event',
required: false,
}),
start_date_time: Property.DateTime({
displayName: 'Start date time of the event',
required: false,
}),
end_date_time: Property.DateTime({
displayName: 'End date time of the event',
required: false,
}),
},
async run(context) {
const authClient = new OAuth2Client();
authClient.setCredentials(context.auth);
displayName: 'Update Event',
auth: googleCalendarAuth,
name: 'update_event',
description: 'Updates an event in Google Calendar.',
props: {
calendar_id: googleCalendarCommon.calendarDropdown('writer'),
eventId: Property.ShortText({
displayName: 'Event ID',
required: true,
}),
title: Property.ShortText({
displayName: 'Title of the event',
required: false,
}),
start_date_time: Property.DateTime({
displayName: 'Start date time of the event',
required: false,
}),
end_date_time: Property.DateTime({
displayName: 'End date time of the event',
required: false,
}),
location: Property.ShortText({
displayName: 'Location',
required: false,
}),
description: Property.LongText({
displayName: 'Description',
description: 'Description of the event. You can use HTML tags here.',
required: false,
}),
attendees: Property.Array({
displayName: 'Attendees',
description: 'Emails of the attendees (guests)',
required: false,
}),
guests_can_modify: Property.Checkbox({
displayName: 'Guests can modify',
defaultValue: false,
required: false,
}),
guests_can_invite_others: Property.Checkbox({
displayName: 'Guests can invite others',
defaultValue: false,
required: false,
}),
guests_can_see_other_guests: Property.Checkbox({
displayName: 'Guests can see other guests',
defaultValue: false,
required: false,
}),
},
async run(context) {
const {
calendar_id,
eventId,
title,
start_date_time,
end_date_time,
location,
description,
guests_can_invite_others,
guests_can_modify,
guests_can_see_other_guests,
} = context.propsValue;

const calendarId = context.propsValue.calendar_id;
const eventId = context.propsValue.eventId;
const summary = context.propsValue.title;
const start = context.propsValue.start_date_time;
const end = context.propsValue.end_date_time;
const attendees = context.propsValue.attendees as string[];

const calendar = google.calendar({ version: 'v3', auth: authClient });
const authClient = new OAuth2Client();
authClient.setCredentials(context.auth);
const calendar = google.calendar({ version: 'v3', auth: authClient });

const response = await calendar.events.update({
calendarId,
eventId,
requestBody: {
summary,
start: {
dateTime: start,
},
end: {
dateTime: end,
},
},
});
// Note that each patch request consumes three quota units;
// prefer using a get followed by an update
const currentEvent = await calendar.events.get({
calendarId: calendar_id,
eventId: eventId,
});

return response.data;
},
let attendeeFormatedList: calendar_v3.Schema$EventAttendee[] = [];
if (Array.isArray(attendees) && attendees.length > 0) {
attendeeFormatedList = attendees.map((email) => ({ email }));
} else if (currentEvent.data.attendees && Array.isArray(currentEvent.data.attendees)) {
attendeeFormatedList = currentEvent.data.attendees;
}

const response = await calendar.events.update({
calendarId: calendar_id,
eventId: eventId,
requestBody: {
summary: title ?? currentEvent.data.summary,
attendees: attendeeFormatedList,
description: description ?? currentEvent.data.description,
location: location ?? currentEvent.data.location,
start: start_date_time
? {
dateTime: dayjs(start_date_time).format('YYYY-MM-DDTHH:mm:ss.sssZ'),
}
: currentEvent.data.start,
end: end_date_time
? {
dateTime: dayjs(end_date_time).format('YYYY-MM-DDTHH:mm:ss.sssZ'),
}
: currentEvent.data.end,
guestsCanInviteOthers: guests_can_invite_others,
guestsCanModify: guests_can_modify,
guestsCanSeeOtherGuests: guests_can_see_other_guests,
},
});

return response.data;
},
});

0 comments on commit 5c8425a

Please sign in to comment.