refactor(anotações): centraliza transformação dos dados de notas

This commit is contained in:
Felipe Coutinho
2026-03-17 17:10:06 +00:00
parent 076953340f
commit a7c6f3c632

View File

@@ -18,25 +18,15 @@ export type NoteData = {
createdAt: string;
};
export async function fetchNotesForUser(userId: string): Promise<NoteData[]> {
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) => {
function toNoteData(note: Note): NoteData {
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(),
@@ -46,7 +36,15 @@ export async function fetchNotesForUser(userId: string): Promise<NoteData[]> {
archived: note.archived,
createdAt: note.createdAt.toISOString(),
};
}
export async function fetchNotesForUser(userId: string): Promise<NoteData[]> {
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(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);
}