-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracking.ts
128 lines (118 loc) · 3.72 KB
/
tracking.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
// import cw, { morgen } from "morgen-cw-sdk";
import cw, { sandbox } from "../src";
const { log, morgen } = sandbox.util;
const { luxon } = sandbox.deps;
const wf = cw.workflow(
{
name: "Time tracker (SDK test)",
},
async function run(trigger) {
if (!trigger.accounts?.calendar?.[0]?.calendarId)
throw new Error("No calendar configured!");
const calId = trigger.accounts.calendar[0].calendarId;
const accId = trigger.accounts.calendar[0].accountId;
const title = trigger.httpParams.title;
// - Check if there's an ongoing #tracking task
// - If the task is different to the ongoing one:
// - set the end time of the ongoing task to end now
// - create a new task starting now
log("Uploading new event");
log(calId);
const todayStart = luxon.DateTime.now()
.startOf("day")
.toISO({ includeOffset: false, suppressMilliseconds: true });
const todayEnd = luxon.DateTime.now()
.startOf("day")
.toISO({ includeOffset: false, suppressMilliseconds: true });
const getEventsResp = await morgen().events.listEventsV3({
calendarIds: calId,
accountId: accId,
start: todayStart!,
end: todayEnd!,
});
log(JSON.stringify(getEventsResp));
const trackingEvents = getEventsResp.data?.events?.filter((ev: any) =>
ev.description?.includes("#tracking")
);
const ongoingEvent = trackingEvents?.find((ev: any) =>
ev.description?.includes("#ongoing")
);
if (ongoingEvent) {
log({ ongoingEvent: ongoingEvent.description });
// New duration from existing start to now
const newDuration = luxon.Interval.fromDateTimes(
luxon.DateTime.fromISO(ongoingEvent.start!, {
zone: ongoingEvent.timeZone,
}),
luxon.DateTime.now()
)
.toDuration()
.toISO();
const { start, timeZone, showWithoutTime } = ongoingEvent;
// Update ongoing task to end now
log("Update event with ID " + ongoingEvent.id);
await morgen().events.updateEventV3({
requestBody: {
id: ongoingEvent.id,
accountId: accId,
calendarId: calId,
start,
timeZone,
showWithoutTime,
duration: newDuration!,
description:
"#tracking " +
(title === ongoingEvent.title ? "#ongoing" : "#finished"),
},
});
}
// Either there's no ongoing task or the specified task title is new, so a
// new task should start being tracked
if (!ongoingEvent || title !== ongoingEvent.title) {
const newStart = luxon.DateTime.now().toUTC().toISO()?.split(".")[0];
// TODO Figure this shit out
const resp = await morgen().events.createEventV3({
requestBody: {
title,
accountId: accId,
calendarId: calId,
start: newStart,
duration: "PT10M",
timeZone: "UTC",
showWithoutTime: false,
description: "#tracking #ongoing",
},
});
log("New task started:", resp);
}
}
);
// Upload, trigger remotely:
// wf.upload().then(() => wf.trigger());
// Upload, but run locally
wf.upload().then(async () => {
const result = await wf.run({
httpParams: {
title: "My next task",
},
eventUpdates: {
added: [],
modified: [],
removed: [],
},
user: {
email: "",
firstName: "",
},
accounts: {
calendar: [
{
accountId: "6441683859cf95c5a82dd83a",
calendarId:
"WyI2NDQxNjgzODU5Y2Y5NWM1YTgyZGQ4M2EiLCJkODQzNjAzOTljZDdlMTkyNmJjOWEyZTljYjliNWUzYzViMmViMDI5ZTMzZWI1M2FlNTQ3ODg1MDg5YTNhM2ZjQGdyb3VwLmNhbGVuZGFyLmdvb2dsZS5jb20iXQ",
},
],
},
});
console.info({ result });
});