0

New Task

by
Published today

Returns tasks created since the last run.

Scriptยท trigger outreach Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
import * as wmill from "windmill-client"
4

5
/**
6
 * New Task
7
 * Returns tasks created since the last run (first run sets the watermark and returns nothing).
8
 */
9
export async function main(auth: RT.Outreach) {
10
  const lastChecked: string | undefined = await wmill.getState()
11

12
  const url = new URL("https://api.outreach.io/api/v2/tasks")
13
  url.searchParams.append("sort", "-createdAt")
14
  url.searchParams.append("page[size]", "100")
15
  url.searchParams.append("count", "false")
16
  if (lastChecked) {
17
    url.searchParams.append("newFilterSyntax", "true")
18
    url.searchParams.append("filter[createdAt][gte]", lastChecked)
19
  }
20

21
  const response = await fetch(url, {
22
    headers: {
23
      Authorization: `Bearer ${auth.token}`,
24
      Accept: "application/vnd.api+json",
25
    },
26
  })
27

28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${await response.text()}`)
30
  }
31

32
  const { data } = (await response.json()) as {
33
    data: { id: number; attributes: { createdAt: string } }[]
34
  }
35

36
  const newItems = lastChecked
37
    ? data.filter((t) => t.attributes.createdAt > lastChecked)
38
    : []
39
  const newWatermark =
40
    data[0]?.attributes.createdAt ?? lastChecked ?? new Date().toISOString()
41
  await wmill.setState(newWatermark)
42

43
  return newItems
44
}
45