-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4344 from kishanprmr/google-calendar
fix(google-calendar): fix update event action
- Loading branch information
Showing
2 changed files
with
111 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
158 changes: 110 additions & 48 deletions
158
packages/pieces/community/google-calendar/src/lib/actions/update-event.ation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}); |