Skip to content

Commit

Permalink
chore: Switch to a different completed tasks endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
scottlovegrove committed Dec 3, 2024
1 parent 8101d06 commit 8ea8203
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/services/todoist.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { lastValueFrom } from 'rxjs'

import type { Task } from '../types'

const LIMIT = 100
const LIMIT = 200

type SyncDue = {
date: string
Expand Down Expand Up @@ -36,6 +36,12 @@ export type SyncTask = {
due?: SyncDue | null
}

type CompletedTasksResponse = {
items: SyncTask[]
total: number
next_cursor?: string
}

@Injectable()
export class TodoistService {
constructor(private readonly httpService: HttpService) {}
Expand All @@ -47,37 +53,41 @@ export class TodoistService {
token: string
projectId: string
}): Promise<Task[]> {
const completedTasks = await this.getCompletedTasksInternal({ token, offset: 0, projectId })
const completedTasks = await this.getCompletedTasksInternal({ token, projectId })

return completedTasks.map((task) => this.getTaskFromQuickAddResponse(task))
}

private async getCompletedTasksInternal({
offset,
cursor,
projectId,
token,
}: {
token: string
offset: number
cursor?: string
projectId: string
}): Promise<SyncTask[]> {
const response = await lastValueFrom(
this.httpService.get<SyncTask[]>(
// At time of writing (08/02/2023), this endpoint is undocumented and its stability is not guaranteed.
`https://api.todoist.com/sync/v9/items/get_completed?project_id=${projectId}&offset=${offset}&limit=${LIMIT}`,
this.httpService.get<CompletedTasksResponse>(
'https://api.todoist.com/sync/v9/tasks/archived',
{
params: {
project_id: projectId,
cursor,
limit: LIMIT,
},
headers: {
Authorization: `Bearer ${token}`,
},
},
),
)

const { data: tasks } = response
const { items: tasks, next_cursor } = response.data

if (tasks.length === LIMIT) {
return tasks.concat(
await this.getCompletedTasksInternal({ token, offset: offset + LIMIT, projectId }),
await this.getCompletedTasksInternal({ token, cursor: next_cursor, projectId }),
)
}

Expand Down

0 comments on commit 8ea8203

Please sign in to comment.