@pipedream/google_calendar 0.5.0 → 0.5.1
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.
package/package.json
CHANGED
|
@@ -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.
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
54
|
-
if (
|
|
55
|
-
|
|
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
|
-
|
|
67
|
-
return this.db.get("
|
|
74
|
+
_getScheduledEventIds() {
|
|
75
|
+
return this.db.get("scheduledEventIds");
|
|
76
|
+
},
|
|
77
|
+
_setScheduledEventIds(ids) {
|
|
78
|
+
this.db.set("scheduledEventIds", ids);
|
|
68
79
|
},
|
|
69
|
-
|
|
70
|
-
this.db.
|
|
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
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
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
|
-
|
|
154
|
+
scheduledEventIds.push(scheduledEventId);
|
|
155
|
+
scheduledCalendarEventIds[calendarEvent.id] = true;
|
|
106
156
|
}
|
|
157
|
+
this._setScheduledEventIds(scheduledEventIds);
|
|
158
|
+
this._setScheduledCalendarEventIds(scheduledCalendarEventIds);
|
|
107
159
|
},
|
|
108
160
|
};
|