-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathupcoming-event-alert.mjs
152 lines (146 loc) · 4.51 KB
/
upcoming-event-alert.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import taskScheduler from "../../../pipedream/sources/new-scheduled-tasks/new-scheduled-tasks.mjs";
import googleCalendar from "../../google_calendar.app.mjs";
import sampleEmit from "./test-event.mjs";
export default {
key: "google_calendar-upcoming-event-alert",
name: "New Upcoming Event Alert",
description: `Emit new event based on a time interval before an upcoming event in the calendar. This source uses Pipedream's Task Scheduler.
[See the documentation](https://pipedream.com/docs/examples/waiting-to-execute-next-step-of-workflow/#step-1-create-a-task-scheduler-event-source)
for more information and instructions for connecting your Pipedream account.`,
version: "0.0.10",
type: "source",
props: {
pipedream: taskScheduler.props.pipedream,
googleCalendar,
db: "$.service.db",
http: "$.interface.http",
calendarId: {
propDefinition: [
googleCalendar,
"calendarId",
],
},
eventTypes: {
propDefinition: [
googleCalendar,
"eventTypes",
],
},
time: {
type: "integer",
label: "Minutes Before",
description: "Number of minutes to trigger before the start of the calendar event.",
min: 0,
reloadProps: true,
},
},
async additionalProps() {
const props = {};
if (this.time > 0) {
props.timer = {
type: "$.interface.timer",
description: "Poll the API to schedule alerts for any newly created events",
default: {
intervalSeconds: 60 * this.time,
},
};
}
return props;
},
hooks: {
async deactivate() {
const ids = this._getScheduledEventIds();
if (!ids?.length) {
return;
}
for (const id of ids) {
if (await this.deleteEvent({
body: {
id,
},
})) {
console.log("Cancelled scheduled event");
}
}
this._setScheduledEventIds();
},
},
methods: {
...taskScheduler.methods,
_getScheduledEventIds() {
return this.db.get("scheduledEventIds");
},
_setScheduledEventIds(ids) {
this.db.set("scheduledEventIds", ids);
},
_getScheduledCalendarEventIds() {
return this.db.get("scheduledCalendarEventIds");
},
_setScheduledCalendarEventIds(ids) {
this.db.set("scheduledCalendarEventIds", ids);
},
_hasDeployed() {
const result = this.db.get("hasDeployed");
this.db.set("hasDeployed", true);
return result;
},
subtractMinutes(date, minutes) {
return date.getTime() - minutes * 60000;
},
async getCalendarEvents() {
const calendarEvents = [];
const params = {
returnOnlyData: false,
calendarId: this.calendarId,
eventTypes: this.eventTypes,
};
do {
const {
data: {
items, nextPageToken,
},
} = await this.googleCalendar.listEvents(params);
if (items?.length) {
calendarEvents.push(...items);
}
params.pageToken = nextPageToken;
} while (params.pageToken);
return calendarEvents;
},
},
async run(event) {
// self subscribe only on the first time
if (!this._hasDeployed()) {
await this.selfSubscribe();
}
const scheduledEventIds = this._getScheduledEventIds() || [];
// incoming scheduled event
if (event.$channel === this.selfChannel()) {
const remainingScheduledEventIds = scheduledEventIds.filter((id) => id !== event["$id"]);
this._setScheduledEventIds(remainingScheduledEventIds);
this.emitEvent(event, `Upcoming ${event.summary} event`);
return;
}
// schedule new events
const scheduledCalendarEventIds = this._getScheduledCalendarEventIds() || {};
const calendarEvents = await this.getCalendarEvents();
for (const calendarEvent of calendarEvents) {
const startTime = calendarEvent.start
? (new Date(calendarEvent.start.dateTime || calendarEvent.start.date))
: null;
if (!startTime
|| startTime.getTime() < Date.now()
|| scheduledCalendarEventIds[calendarEvent.id])
{
continue;
}
const later = new Date(this.subtractMinutes(startTime, this.time));
const scheduledEventId = this.emitScheduleEvent(calendarEvent, later);
scheduledEventIds.push(scheduledEventId);
scheduledCalendarEventIds[calendarEvent.id] = true;
}
this._setScheduledEventIds(scheduledEventIds);
this._setScheduledCalendarEventIds(scheduledCalendarEventIds);
},
sampleEmit,
};