@pipedream/google_calendar 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -42,7 +42,7 @@ export default {
42
42
  recurrence: {
43
43
  label: "Recurrence",
44
44
  type: "string[]",
45
- description: "Recurrence rule(s) for the event. For example, `FREQ=DAILY;INTERVAL=2` means once every two days, `RRULE:FREQ=YEARLY` means annually.\nYou can combine multiple recurrence rules. [See the documentation](https://developers.google.com/calendar/api/concepts/events-calendars#recurrence_rule)",
45
+ description: "Recurrence rule(s) for the event. For example, `RRULE:FREQ=DAILY;INTERVAL=2` means once every two days, `RRULE:FREQ=YEARLY` means annually.\nYou can combine multiple recurrence rules. [See the documentation](https://developers.google.com/calendar/api/concepts/events-calendars#recurrence_rule)",
46
46
  optional: true,
47
47
  },
48
48
  timeZone: {
@@ -5,8 +5,8 @@ import { v4 as uuidv4 } from "uuid";
5
5
  export default {
6
6
  key: "google_calendar-create-event",
7
7
  name: "Create Event",
8
- description: "Create an event to the Google Calendar. [See the documentation](https://googleapis.dev/nodejs/googleapis/latest/calendar/classes/Resource$Events.html#insert)",
9
- version: "0.2.1",
8
+ description: "Create an event to the Google Calendar. [See the documentation](https://developers.google.com/calendar/api/v3/reference/events/insert)",
9
+ version: "0.2.2",
10
10
  type: "action",
11
11
  props: {
12
12
  googleCalendar,
@@ -36,7 +36,7 @@ export default {
36
36
  ...createEventCommon.methods,
37
37
  },
38
38
  async run({ $ }) {
39
- const timeZone = this.getTimeZone(this.timeZone);
39
+ const timeZone = await this.getTimeZone(this.timeZone);
40
40
  const attendees = this.formatAttendees(this.attendees);
41
41
 
42
42
  const data = {
@@ -5,7 +5,7 @@ export default {
5
5
  key: "google_calendar-update-event",
6
6
  name: "Update Event",
7
7
  description: "Update an event from Google Calendar. [See the documentation](https://googleapis.dev/nodejs/googleapis/latest/calendar/classes/Resource$Events.html#update)",
8
- version: "0.0.6",
8
+ version: "0.0.7",
9
9
  type: "action",
10
10
  props: {
11
11
  googleCalendar,
@@ -37,7 +37,7 @@ export default {
37
37
  eventId: this.eventId,
38
38
  });
39
39
 
40
- const timeZone = this.getTimeZone(this.timeZone || currentEvent.start.timeZone);
40
+ const timeZone = await this.getTimeZone(this.timeZone || currentEvent.start.timeZone);
41
41
  const attendees = this.formatAttendees(this.attendees, currentEvent.attendees);
42
42
 
43
43
  const response = await this.googleCalendar.updateEvent({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pipedream/google_calendar",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Pipedream Google_calendar Components",
5
5
  "main": "google_calendar.app.mjs",
6
6
  "keywords": [
@@ -1,6 +1,5 @@
1
1
  import taskScheduler from "../../../pipedream/sources/new-scheduled-tasks/new-scheduled-tasks.mjs";
2
2
  import googleCalendar from "../../google_calendar.app.mjs";
3
- import { axios } from "@pipedream/platform";
4
3
 
5
4
  export default {
6
5
  key: "google_calendar-upcoming-event-alert",
@@ -8,7 +7,7 @@ export default {
8
7
  description: `Emit new event based on a time interval before an upcoming event in the calendar. This source uses Pipedream's Task Scheduler.
9
8
  [See the documentation](https://pipedream.com/docs/examples/waiting-to-execute-next-step-of-workflow/#step-1-create-a-task-scheduler-event-source)
10
9
  for more information and instructions for connecting your Pipedream account.`,
11
- version: "0.0.5",
10
+ version: "0.0.6",
12
11
  type: "source",
13
12
  props: {
14
13
  pipedream: taskScheduler.props.pipedream,
@@ -29,45 +28,60 @@ export default {
29
28
  calendarId: c.calendarId,
30
29
  }),
31
30
  ],
31
+ optional: true,
32
32
  },
33
33
  time: {
34
34
  type: "integer",
35
35
  label: "Minutes Before",
36
36
  description: "Number of minutes to trigger before the start of the calendar event.",
37
37
  min: 0,
38
+ reloadProps: true,
38
39
  },
39
40
  },
40
- hooks: {
41
- async activate() {
42
- // workaround - self call run() because selfSubscribe() can't be run on activate or deploy
43
- // see selfSubscribe() method in pipedream/sources/new-scheduled-tasks/new-scheduled-tasks.mjs
44
- await axios(this, {
45
- url: this.http.endpoint,
46
- method: "POST",
47
- data: {
48
- schedule: true,
41
+ async additionalProps() {
42
+ const props = {};
43
+ if (this.time > 0) {
44
+ props.timer = {
45
+ type: "$.interface.timer",
46
+ description: "Poll the API to schedule alerts for any newly created events",
47
+ default: {
48
+ intervalSeconds: 60 * this.time,
49
49
  },
50
- });
51
- },
50
+ };
51
+ }
52
+ return props;
53
+ },
54
+ hooks: {
52
55
  async deactivate() {
53
- const id = this._getScheduledEventId();
54
- if (id && await this.deleteEvent({
55
- body: {
56
- id,
57
- },
58
- })) {
59
- console.log("Cancelled scheduled event");
60
- this._setScheduledEventId();
56
+ const ids = this._getScheduledEventIds();
57
+ if (!ids?.length) {
58
+ return;
61
59
  }
60
+ for (const id of ids) {
61
+ if (await this.deleteEvent({
62
+ body: {
63
+ id,
64
+ },
65
+ })) {
66
+ console.log("Cancelled scheduled event");
67
+ }
68
+ }
69
+ this._setScheduledEventIds();
62
70
  },
63
71
  },
64
72
  methods: {
65
73
  ...taskScheduler.methods,
66
- _getScheduledEventId() {
67
- return this.db.get("scheduledEventId");
74
+ _getScheduledEventIds() {
75
+ return this.db.get("scheduledEventIds");
76
+ },
77
+ _setScheduledEventIds(ids) {
78
+ this.db.set("scheduledEventIds", ids);
68
79
  },
69
- _setScheduledEventId(id) {
70
- this.db.set("scheduledEventId", id);
80
+ _getScheduledCalendarEventIds() {
81
+ return this.db.get("scheduledCalendarEventIds");
82
+ },
83
+ _setScheduledCalendarEventIds(ids) {
84
+ this.db.set("scheduledCalendarEventIds", ids);
71
85
  },
72
86
  _hasDeployed() {
73
87
  const result = this.db.get("hasDeployed");
@@ -77,6 +91,32 @@ export default {
77
91
  subtractMinutes(date, minutes) {
78
92
  return date.getTime() - minutes * 60000;
79
93
  },
94
+ async getCalendarEvents() {
95
+ const calendarEvents = [];
96
+ const params = {
97
+ calendarId: this.calendarId,
98
+ };
99
+ if (this.eventId) {
100
+ const item = await this.googleCalendar.getEvent({
101
+ ...params,
102
+ eventId: this.eventId,
103
+ });
104
+ calendarEvents.push(item);
105
+ } else {
106
+ do {
107
+ const {
108
+ data: {
109
+ items, nextPageToken,
110
+ },
111
+ } = await this.googleCalendar.getEvents(params);
112
+ if (items?.length) {
113
+ calendarEvents.push(...items);
114
+ }
115
+ params.pageToken = nextPageToken;
116
+ } while (params.pageToken);
117
+ }
118
+ return calendarEvents;
119
+ },
80
120
  },
81
121
  async run(event) {
82
122
  // self subscribe only on the first time
@@ -84,25 +124,37 @@ export default {
84
124
  await this.selfSubscribe();
85
125
  }
86
126
 
127
+ const scheduledEventIds = this._getScheduledEventIds() || [];
128
+
87
129
  // incoming scheduled event
88
130
  if (event.$channel === this.selfChannel()) {
131
+ const remainingScheduledEventIds = scheduledEventIds.filter((id) => id !== event["$id"]);
132
+ this._setScheduledEventIds(remainingScheduledEventIds);
89
133
  this.emitEvent(event, `Upcoming ${event.summary} event`);
90
- this._setScheduledEventId();
91
134
  return;
92
135
  }
93
136
 
94
- // received schedule command
95
- if (event.body?.schedule) {
96
- const calendarEvent = await this.googleCalendar.getEvent({
97
- calendarId: this.calendarId,
98
- eventId: this.eventId,
99
- });
137
+ // schedule new events
138
+ const scheduledCalendarEventIds = this._getScheduledCalendarEventIds() || {};
139
+ const calendarEvents = await this.getCalendarEvents();
100
140
 
101
- const startTime = new Date(calendarEvent.start.dateTime || calendarEvent.start.date);
141
+ for (const calendarEvent of calendarEvents) {
142
+ const startTime = calendarEvent.start
143
+ ? (new Date(calendarEvent.start.dateTime || calendarEvent.start.date))
144
+ : null;
145
+ if (!startTime
146
+ || startTime.getTime() < Date.now()
147
+ || scheduledCalendarEventIds[calendarEvent.id])
148
+ {
149
+ continue;
150
+ }
102
151
  const later = new Date(this.subtractMinutes(startTime, this.time));
103
152
 
104
153
  const scheduledEventId = this.emitScheduleEvent(calendarEvent, later);
105
- this._setScheduledEventId(scheduledEventId);
154
+ scheduledEventIds.push(scheduledEventId);
155
+ scheduledCalendarEventIds[calendarEvent.id] = true;
106
156
  }
157
+ this._setScheduledEventIds(scheduledEventIds);
158
+ this._setScheduledCalendarEventIds(scheduledCalendarEventIds);
107
159
  },
108
160
  };