Organize/lib/access.ts

39 lines
1.4 KiB
TypeScript

import "server-only";
/**
* Central place for "does this user have access to this Category" -- every
* action in lib/actions/{categories,groups,todos,notes}.ts filters its
* Prisma queries through this instead of writing `userId` directly, so
* there's exactly one place to update when Projects become shared spaces
* (add a `project: { members: { some: { userId } } }` branch below) rather
* than an action-by-action audit.
*
* A category belongs to exactly one of:
* - a User directly (a Home category, `projectId` null), or
* - a Project (`projectId` set) -- accessible today only to its owner.
*/
export function categoryAccessFilter(userId: string) {
return {
OR: [{ userId, projectId: null }, { project: projectAccessFilter(userId) }],
};
}
/** Same idea as categoryAccessFilter, but starting from a Project row itself. */
export function projectAccessFilter(userId: string) {
return { ownerId: userId };
}
/**
* Same ownership pattern as categoryAccessFilter, but simpler: a
* ScheduledTodo carries userId/projectId directly (no Category/Group join
* to traverse).
*/
export function scheduledTodoAccessFilter(userId: string) {
return { OR: [{ userId }, { project: projectAccessFilter(userId) }] };
}
/** The board route a given scope's mutations should revalidate. */
export function boardPath(projectId: string | null | undefined): string {
return projectId ? `/projects/${projectId}` : "/";
}