-
Notifications
You must be signed in to change notification settings - Fork 1
/
schedule-3rd-party-task.ts
80 lines (67 loc) · 2.41 KB
/
schedule-3rd-party-task.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
// import cw, { morgen } from "morgen-cw-sdk";
import cw, { sandbox } from "../src";
const { morgen, log } = sandbox.util;
const { luxon } = sandbox.deps;
/**
* This example will find a 3rd party task and schedule for the current time.
*
* NOTE: Please specify the calendar in the UI once the workflow has been
* uploaded.
*/
const wf = cw.workflow(
{
name: "SDK Example: Schedule a 3rd-party Task",
},
async function run(trigger) {
if (!trigger.accounts?.calendar?.[0]?.calendarId)
throw new Error("No calendar configured!");
const { accountId, calendarId } = trigger.accounts.calendar[0];
// Get exported classes of the luxon library
const { DateTime } = luxon;
const integs = await morgen().accounts.listAccountsV3();
const serviceName = "todoist"; // One of googleTasks, microsoftToDo, microsoftOutlook, todoist
const taskAccountId = integs.data?.accounts?.find(
(integ) => integ.integrationId === serviceName
)?.providerId;
if (!taskAccountId) {
throw new Error("User has not connected this type of account");
}
const taskResp = await morgen().tasks.listTasksV2({
accountId: taskAccountId,
serviceName,
});
if (taskResp.length < 1) throw new Error("No tasks found");
log(taskResp.length + " tasks found");
// Choose the first task as a demonstration
const t = taskResp[0];
// Construct the correctly formatted metadata ID
const taskId = [t.serviceName, t.accountId, t.accountId, t.id].join("---");
// Schedule the task event to begin now
const now = DateTime.now();
const resp = await morgen().events.createEventV3({
requestBody: {
// Link the event back to the third-party task so that it appears linked in Morgen
"morgen.so:metadata": {
taskId,
},
calendarId,
accountId,
title: t.title,
description: t.description,
timeZone: now.zoneName || "UTC",
showWithoutTime: false,
start: now.toISO({
// Milliseconds aren't supported by the Morgen API
suppressMilliseconds: true,
// Exclude the time zone, which is provided separately
includeOffset: false,
})!,
duration: t.estimatedDuration || "PT30M",
},
});
log(JSON.stringify(resp));
return resp;
}
);
// Upload the workflow, and trigger it when upload is complete
wf.upload().then(() => wf.trigger());