0

Move Task To Section

by
Published Jun 6, 2022

Move a Task to a different section within the same project. [See the docs here](https://developer.todoist.com/sync/v9/#move-an-item)

Script todoist Verified

The script

Submitted by hugo697 Bun
Verified 398 days ago
1
import { v4 as uuidv4 } from 'uuid'
2
import { v9 as Todoist } from 'todoist'
3

4
type Todoist = {
5
	Token: string
6
}
7

8
interface Response {
9
	sync_status: Sync_status
10
}
11

12
interface Sync_status {
13
	[key: string]: string
14
}
15

16
export async function main(resource: Todoist, taskId: string, parentId: string) {
17
	const response = await fetch('https://api.todoist.com/sync/v9/sync', {
18
		method: 'POST',
19
		headers: {
20
			Authorization: `Bearer ${resource.Token}`,
21
			'Content-Type': 'application/json'
22
		},
23
		body: JSON.stringify({
24
			commands: [
25
				{
26
					type: 'item_move',
27
					uuid: uuidv4(),
28
					args: {
29
						id: taskId,
30
						parent_id: parentId
31
					}
32
				}
33
			]
34
		})
35
	})
36

37
	if (!response.ok) {
38
		throw new Error(`HTTP error! status: ${response.status}`)
39
	}
40

41
	const responseData = (await response.json()) as Response
42

43
	return responseData.sync_status[0] === 'ok' ? true : false
44
}
45