mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 11:01:45 +00:00
refactor(anotações): centraliza transformação dos dados de notas
This commit is contained in:
@@ -18,35 +18,33 @@ export type NoteData = {
|
|||||||
createdAt: string;
|
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<NoteData[]> {
|
export async function fetchNotesForUser(userId: string): Promise<NoteData[]> {
|
||||||
const noteRows = await db.query.notes.findMany({
|
const noteRows = await db.query.notes.findMany({
|
||||||
where: and(eq(notes.userId, userId), eq(notes.archived, false)),
|
where: and(eq(notes.userId, userId), eq(notes.archived, false)),
|
||||||
orderBy: (table, { desc }) => [desc(table.createdAt)],
|
orderBy: (table, { desc }) => [desc(table.createdAt)],
|
||||||
});
|
});
|
||||||
|
|
||||||
return noteRows.map((note: Note) => {
|
return noteRows.map(toNoteData);
|
||||||
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(),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchAllNotesForUser(
|
export async function fetchAllNotesForUser(
|
||||||
@@ -68,27 +66,5 @@ export async function fetchArchivedForUser(
|
|||||||
orderBy: (table, { desc }) => [desc(table.createdAt)],
|
orderBy: (table, { desc }) => [desc(table.createdAt)],
|
||||||
});
|
});
|
||||||
|
|
||||||
return noteRows.map((note: Note) => {
|
return noteRows.map(toNoteData);
|
||||||
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(),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user