Organize/lib/board.ts

50 lines
1.4 KiB
TypeScript

import "server-only";
import { prisma } from "@/lib/db";
import type { CategoryDTO } from "@/types/board";
/**
* Fetches one board's worth of categories/groups/todos, shaped for the
* client -- shared by the Home board and every Project board, which are
* identical in every way except which categories they scope to.
*/
export async function getBoard(
where: { userId: string; projectId: null } | { projectId: string }
): Promise<CategoryDTO[]> {
const categories = await prisma.category.findMany({
where,
orderBy: { order: "asc" },
include: {
// Archived groups are kept in the database (not deleted) but
// excluded from the board query.
groups: {
where: { archivedAt: null },
orderBy: { order: "asc" },
include: { todos: { orderBy: { order: "asc" } } },
},
},
});
return categories.map((category) => ({
id: category.id,
name: category.name,
order: category.order,
groups: category.groups.map((group) => ({
id: group.id,
title: group.title,
color: group.color,
order: group.order,
noteContent: group.noteContent,
categoryId: group.categoryId,
todos: group.todos.map((todo) => ({
id: todo.id,
title: todo.title,
details: todo.details,
completed: todo.completed,
order: todo.order,
groupId: todo.groupId,
})),
})),
}));
}