diff --git a/app/api/twenty-crm/tasks/route.ts b/app/api/twenty-crm/tasks/route.ts index 9282091..8a8ac3b 100644 --- a/app/api/twenty-crm/tasks/route.ts +++ b/app/api/twenty-crm/tasks/route.ts @@ -6,14 +6,20 @@ import { logger } from "@/lib/logger"; interface TwentyTask { id: string; title: string; - bodyV2?: any; // Can be rich text/JSON + bodyV2?: { + blocknote?: string; + markdown?: string; + }; dueAt?: string; status?: string; // e.g., "Done", "Todo", etc. type?: string; assigneeId?: string; assignee?: { id: string; - name?: string; + name?: { + firstName?: string; + lastName?: string; + }; }; } @@ -42,8 +48,8 @@ async function fetchTwentyTasks(): Promise { const todayISO = today.toISOString(); // GraphQL query to fetch tasks from Twenty CRM - // Trying simpler query first - tasks may not accept filter/orderBy arguments - // We'll filter client-side if needed + // bodyV2 is RichTextV2 type - trying common subfields + // name is FullName type - using firstName and lastName const query = ` query GetTasks { tasks { @@ -51,13 +57,19 @@ async function fetchTwentyTasks(): Promise { node { id title - bodyV2 + bodyV2 { + blocknote + markdown + } dueAt status assigneeId assignee { id - name + name { + firstName + lastName + } } } } @@ -144,16 +156,12 @@ async function fetchTwentyTasks(): Promise { .map((edge: any) => { const node = edge.node || edge; // Handle both edge.node and direct node - // Extract text from bodyV2 if it's a rich text object - let bodyText = null; - if (node.bodyV2) { - if (typeof node.bodyV2 === 'string') { - bodyText = node.bodyV2; - } else if (node.bodyV2 && typeof node.bodyV2 === 'object') { - // Try to extract text from rich text structure - bodyText = JSON.stringify(node.bodyV2).substring(0, 200); - } - } + // Extract text from bodyV2 (RichTextV2 type) + let bodyText = null; + if (node.bodyV2) { + // bodyV2 has blocknote and markdown subfields + bodyText = node.bodyV2.markdown || node.bodyV2.blocknote || null; + } return { id: node.id, @@ -163,10 +171,13 @@ async function fetchTwentyTasks(): Promise { status: node.status || null, type: node.type || 'Task', assigneeId: node.assigneeId || null, - assignee: node.assignee ? { - id: node.assignee.id, - name: node.assignee.name || null, + assignee: node.assignee ? { + id: node.assignee.id, + name: node.assignee.name ? { + firstName: node.assignee.name.firstName || null, + lastName: node.assignee.name.lastName || null, } : null, + } : null, // Store extracted body text for easier access _bodyText: bodyText, }; @@ -237,8 +248,8 @@ export async function GET(request: NextRequest) { projectId: 0, status: task.status === 'Done' ? 5 : 1, // 5 = Done, 1 = New (or other status) editorId: task.assigneeId || null, - editorFirstname: task.assignee?.name?.split(' ')[0] || null, // Extract first name from full name - editorLastname: task.assignee?.name?.split(' ').slice(1).join(' ') || null, // Extract last name from full name + editorFirstname: task.assignee?.name?.firstName || null, + editorLastname: task.assignee?.name?.lastName || null, authorFirstname: null, authorLastname: null, milestoneHeadline: null,