-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
207 lines (197 loc) · 6.17 KB
/
index.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { Agent, Log } from "@aspen.cloud/agent-typings";
import {
addDays,
addMinutes,
addMonths,
addYears,
isAfter,
isBefore,
} from "date-fns";
import { rrulestr } from "rrule";
import { CalendarEvent, SetEventParams } from "./types";
enum Operations {
SET_EVENT = "SET_EVENT",
DELETE_EVENT = "DELETE_EVENT",
}
const NOTIFICATION_OFFSET = 30;
function eventRangeFilter(startDateUtc: string, endDateUtc: string) {
const rangeStart = new Date(startDateUtc);
const rangeEnd = new Date(endDateUtc);
return (event: CalendarEvent) => {
const eventStart = new Date(event.startDateUtc);
const eventEnd = new Date(event.endDateUtc);
return isBefore(rangeStart, eventEnd) && isAfter(rangeEnd, eventStart);
};
}
function generateRecurringEvents(
event: CalendarEvent,
startDateUtc: string,
endDateUtc: string
) {
const rule = rrulestr(event.recurrencePattern);
const dates = rule.between(new Date(startDateUtc), new Date(endDateUtc));
return dates.map<CalendarEvent>((date) => ({
...event,
startDateUtc: date.toISOString(),
endDateUtc: addMinutes(date, event.duration).toISOString(),
}));
}
function parseEvents(
events: CalendarEvent[],
startDateUtc: string,
endDateUtc: string
) {
const nonRecurring = events.filter((e) => !e.isRecurring);
const recurring = events
.filter((e) => e.isRecurring)
.flatMap((event) =>
generateRecurringEvents(event, startDateUtc, endDateUtc)
);
return [...nonRecurring, ...recurring];
}
const agent: Agent = {
aggregations: {
events: {
initialize: (calendarData?: Record<string, CalendarEvent>) => {
return calendarData ?? {};
},
reducer: (
calendarData: Record<string, CalendarEvent>,
event: Log<any>
) => {
switch (event.data.type) {
case Operations.SET_EVENT:
calendarData[event.data.event.id] = event.data.event;
break;
case Operations.DELETE_EVENT:
delete calendarData[event.data.eventId];
break;
}
return { ...calendarData };
},
serialize: (calendarData: Record<string, CalendarEvent>) => {
return calendarData;
},
},
},
views: {
eventsForDate: async (
params: { year: string; month: string; day: string },
aspen
) => {
const { year, month, day } = params;
const events = (await aspen.getAggregation("events", {
range: "continuous",
})) as Record<string, CalendarEvent>;
const startDate = new Date(Number(year), Number(month), Number(day));
const startDateUtc = startDate.toISOString();
const endDate = addDays(startDate, 1);
const endDateUtc = endDate.toISOString();
const eventsInRange = Object.values(events).filter(
eventRangeFilter(startDateUtc, endDateUtc)
);
return parseEvents(eventsInRange, startDateUtc, endDateUtc);
},
eventsForMonth: async (params: { year: string; month: string }, aspen) => {
const { year, month } = params;
const events = (await aspen.getAggregation("events", {
range: "continuous",
})) as Record<string, CalendarEvent>;
const startDate = new Date(Number(year), Number(month));
const startDateUtc = startDate.toISOString();
const endDate = addMonths(startDate, 1);
const endDateUtc = endDate.toISOString();
const eventsInRange = Object.values(events).filter(
eventRangeFilter(startDateUtc, endDateUtc)
);
return parseEvents(eventsInRange, startDateUtc, endDateUtc);
},
eventsForYear: async (params: { year: string }, aspen) => {
const { year } = params;
const events = (await aspen.getAggregation("events", {
range: "continuous",
})) as Record<string, CalendarEvent>;
const startDate = new Date(Number(year));
const startDateUtc = startDate.toISOString();
const endDate = addYears(startDate, 1);
const endDateUtc = endDate.toISOString();
const eventsInRange = Object.values(events).filter(
eventRangeFilter(startDateUtc, endDateUtc)
);
return parseEvents(eventsInRange, startDateUtc, endDateUtc);
},
eventsInRange: async (
params: { startDateUtc: string; endDateUtc: string },
aspen
) => {
const { startDateUtc, endDateUtc } = params;
const events = (await aspen.getAggregation("events", {
range: "continuous",
})) as Record<string, CalendarEvent>;
return Object.values(events).filter(
eventRangeFilter(startDateUtc, endDateUtc)
);
},
eventById: async (params: { eventId: string }, aspen) => {
const { eventId } = params;
const events = (await aspen.getAggregation("events", {
range: "continuous",
})) as Record<string, CalendarEvent>;
const event = events[eventId];
if (!event) throw new Error("Event not found");
return event;
},
},
actions: {
setEvent: async (params: SetEventParams, aspen) => {
const {
title,
description,
startDateUtc,
endDateUtc,
duration,
id,
isRecurring,
recurrencePattern,
} = params;
const rid = id ?? (await aspen.createResource());
const event = {
title,
description,
startDateUtc,
endDateUtc,
duration: Number(duration),
id: rid,
isRecurring,
recurrencePattern,
} as CalendarEvent;
await aspen.pushEvent(
Operations.SET_EVENT,
{ event },
{
resourceId: rid,
}
);
const startDate = new Date(startDateUtc);
const notifyDate = new Date(
startDate.getTime() - NOTIFICATION_OFFSET * 60 * 1000
);
aspen.scheduleAction("notify", event, notifyDate, {
jobKey: `reminder_${rid}`,
});
return event;
},
deleteEvent: async (params, aspen) => {
await aspen.pushEvent(Operations.DELETE_EVENT, {
eventId: params.eventId,
});
aspen.unscheduleAction(`reminder_${params.eventId}`);
return params.eventId;
},
notify: async (params, aspen) => {
log(`UPCOMING EVENT: ${JSON.stringify(params)}`);
return "notified";
},
},
};
export default agent;