From a7c6f3c6327fa5ef444bdcd6129f1fb1b19448b2 Mon Sep 17 00:00:00 2001 From: Felipe Coutinho Date: Tue, 17 Mar 2026 17:10:06 +0000 Subject: [PATCH] =?UTF-8?q?refactor(anota=C3=A7=C3=B5es):=20centraliza=20t?= =?UTF-8?q?ransforma=C3=A7=C3=A3o=20dos=20dados=20de=20notas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/features/notes/queries.ts | 68 ++++++++++++----------------------- 1 file changed, 22 insertions(+), 46 deletions(-) diff --git a/src/features/notes/queries.ts b/src/features/notes/queries.ts index 56ef03e..46ad810 100644 --- a/src/features/notes/queries.ts +++ b/src/features/notes/queries.ts @@ -18,35 +18,33 @@ export type NoteData = { createdAt: string; }; +function toNoteData(note: Note): NoteData { + let tasks: Task[] | undefined; + if (note.tasks) { + try { + tasks = JSON.parse(note.tasks); + } catch (error) { + console.error("Failed to parse tasks for note", note.id, error); + } + } + return { + id: note.id, + title: (note.title ?? "").trim(), + description: (note.description ?? "").trim(), + type: (note.type ?? "nota") as "nota" | "tarefa", + tasks, + archived: note.archived, + createdAt: note.createdAt.toISOString(), + }; +} + export async function fetchNotesForUser(userId: string): Promise { const noteRows = await db.query.notes.findMany({ where: and(eq(notes.userId, userId), eq(notes.archived, false)), orderBy: (table, { desc }) => [desc(table.createdAt)], }); - return noteRows.map((note: Note) => { - let tasks: Task[] | undefined; - - // Parse tasks if they exist - if (note.tasks) { - try { - tasks = JSON.parse(note.tasks); - } catch (error) { - console.error("Failed to parse tasks for note", note.id, error); - tasks = undefined; - } - } - - return { - id: note.id, - title: (note.title ?? "").trim(), - description: (note.description ?? "").trim(), - type: (note.type ?? "nota") as "nota" | "tarefa", - tasks, - archived: note.archived, - createdAt: note.createdAt.toISOString(), - }; - }); + return noteRows.map(toNoteData); } export async function fetchAllNotesForUser( @@ -68,27 +66,5 @@ export async function fetchArchivedForUser( orderBy: (table, { desc }) => [desc(table.createdAt)], }); - return noteRows.map((note: Note) => { - let tasks: Task[] | undefined; - - // Parse tasks if they exist - if (note.tasks) { - try { - tasks = JSON.parse(note.tasks); - } catch (error) { - console.error("Failed to parse tasks for note", note.id, error); - tasks = undefined; - } - } - - return { - id: note.id, - title: (note.title ?? "").trim(), - description: (note.description ?? "").trim(), - type: (note.type ?? "nota") as "nota" | "tarefa", - tasks, - archived: note.archived, - createdAt: note.createdAt.toISOString(), - }; - }); + return noteRows.map(toNoteData); }