-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedule-task.ts
65 lines (57 loc) · 1.88 KB
/
schedule-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
// import cw, { morgen } from "morgen-cw-sdk";
import cw, { sandbox } from "../src";
const { morgen } = sandbox.util;
const { luxon } = sandbox.deps;
/**
* This example will create Morgen 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 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, Duration } = luxon;
// Create the start time using the luxon library
const startDT = DateTime.now().startOf("minute");
const title = "Auto-scheduled task 001";
const estimatedDuration = Duration.fromObject({ minutes: 30 }).toISO()!;
const taskResp = await morgen().tasks.createTaskV2({
requestBody: {
title,
estimatedDuration,
},
});
// Perform the request using our client
const resp = await morgen().events.createEventV3({
requestBody: {
"morgen.so:metadata": {
taskId: taskResp.id.split("@")[0],
},
calendarId,
accountId,
title,
description:
"A task that I need to have automatically scheduled right now.",
timeZone: startDT.zoneName || "UTC",
showWithoutTime: false,
start: startDT.toISO({
// Milliseconds aren't supported by the Morgen API
suppressMilliseconds: true,
// Exclude the time zone, which is provided separately
includeOffset: false,
})!,
duration: estimatedDuration,
},
});
return resp;
}
);
// Upload the workflow, and trigger it when upload is complete
wf.upload().then(() => wf.trigger());