mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-05 11:20:09 +08:00
- 组卷可选已接受共享的错题本,按归属取题,卷别分组允许共享所有者 - 今日复习、练习记录与 AI 判题支持共享错题,只读成员可记录自己的作答 - 统计按共享错题本筛选并标注所有者,练习与薄弱知识点按当前账号证据计算 - 错题保存增加版本校验,共享成员并发编辑过期返回 409 - 补充共享组卷/复习/练习/统计集成测试与文档 Refs: https://github.com/margrop/wrong-notebook/issues/72
182 lines
7.0 KiB
TypeScript
182 lines
7.0 KiB
TypeScript
import "server-only";
|
||
|
||
/**
|
||
* 错题本共享的权限判定与可访问范围。
|
||
* 所有者(Subject.userId)始终可读写;共享成员按 active 关系的 read/edit 角色读写。
|
||
* 错题创建者记在 ErrorItem.userId,但归属以 subjectId 为准,避免成员写入的题只对创建者可见。
|
||
*/
|
||
|
||
import type { Prisma } from "@prisma/client";
|
||
import { prisma } from "@/lib/prisma";
|
||
|
||
export const NOTEBOOK_ROLES = ["owner", "edit", "read"] as const;
|
||
export type NotebookRole = (typeof NOTEBOOK_ROLES)[number];
|
||
|
||
/** 共享关系里的角色,不含所有者 */
|
||
export const NOTEBOOK_SHARE_ROLES = ["edit", "read"] as const;
|
||
export type NotebookShareRole = (typeof NOTEBOOK_SHARE_ROLES)[number];
|
||
|
||
export const NOTEBOOK_SHARE_STATUSES = ["invited", "active"] as const;
|
||
export type NotebookShareStatus = (typeof NOTEBOOK_SHARE_STATUSES)[number];
|
||
|
||
export function isNotebookShareRole(value: unknown): value is NotebookShareRole {
|
||
return typeof value === "string" && (NOTEBOOK_SHARE_ROLES as readonly string[]).includes(value);
|
||
}
|
||
|
||
export function isNotebookShareStatus(value: unknown): value is NotebookShareStatus {
|
||
return typeof value === "string" && (NOTEBOOK_SHARE_STATUSES as readonly string[]).includes(value);
|
||
}
|
||
|
||
/** 可修改错题本内容:所有者与 edit 成员;read 成员只能查看 */
|
||
export function canWriteNotebook(role: NotebookRole | null | undefined): boolean {
|
||
return role === "owner" || role === "edit";
|
||
}
|
||
|
||
/** 共享成员在该错题本的可访问范围 */
|
||
export interface NotebookAccessSets {
|
||
/** 自己拥有的错题本 */
|
||
ownedIds: string[];
|
||
/** 已接受共享的错题本 */
|
||
sharedIds: string[];
|
||
/** 可读:自有 + 全部已生效共享 */
|
||
readIds: string[];
|
||
/** 可写:自有 + edit 共享 */
|
||
writeIds: string[];
|
||
}
|
||
|
||
/** 一次查询得到当前用户可读/可写的错题本集合;空集合时列表只按 userId 过滤 */
|
||
export async function getNotebookAccessSets(userId: string): Promise<NotebookAccessSets> {
|
||
const [owned, shares] = await Promise.all([
|
||
prisma.subject.findMany({ where: { userId }, select: { id: true } }),
|
||
prisma.notebookShare.findMany({
|
||
where: { memberId: userId, status: "active" },
|
||
select: { subjectId: true, role: true },
|
||
}),
|
||
]);
|
||
const ownedIds = owned.map((subject) => subject.id);
|
||
const sharedIds = shares.map((share) => share.subjectId);
|
||
const editIds = shares.filter((share) => share.role === "edit").map((share) => share.subjectId);
|
||
return {
|
||
ownedIds,
|
||
sharedIds,
|
||
readIds: [...new Set([...ownedIds, ...sharedIds])],
|
||
writeIds: [...new Set([...ownedIds, ...editIds])],
|
||
};
|
||
}
|
||
|
||
/** 单个错题本的角色;不是所有者或已生效成员时返回 null */
|
||
export async function getNotebookRole(userId: string, subjectId: string): Promise<NotebookRole | null> {
|
||
const subject = await prisma.subject.findUnique({
|
||
where: { id: subjectId },
|
||
select: {
|
||
userId: true,
|
||
shares: {
|
||
where: { memberId: userId, status: "active" },
|
||
select: { role: true },
|
||
take: 1,
|
||
},
|
||
},
|
||
});
|
||
if (!subject) return null;
|
||
if (subject.userId === userId) return "owner";
|
||
const role = subject.shares[0]?.role;
|
||
return isNotebookShareRole(role) ? role : null;
|
||
}
|
||
|
||
/** 错题访问判定所需的归属字段(由各路由按需 select) */
|
||
export interface ErrorItemAccessFields {
|
||
userId: string;
|
||
subjectId?: string | null;
|
||
subject?: {
|
||
userId: string;
|
||
shares?: { role: string }[];
|
||
} | null;
|
||
}
|
||
|
||
/**
|
||
* 纯函数判定错题角色:有错题本时以错题本归属与共享角色为准(read 成员即使创建过题也只能读);
|
||
* 无错题本的旧数据按创建者判定。共享查询须已按当前用户与 active 过滤。
|
||
*/
|
||
export function resolveErrorItemRole(userId: string, item: ErrorItemAccessFields): NotebookRole | null {
|
||
if (item.subjectId && item.subject) {
|
||
if (item.subject.userId === userId) return "owner";
|
||
const shareRole = item.subject.shares?.[0]?.role;
|
||
if (isNotebookShareRole(shareRole)) return shareRole;
|
||
}
|
||
return item.userId === userId ? "owner" : null;
|
||
}
|
||
|
||
/** 按 id 读取错题并判定角色;错题不存在时返回 null */
|
||
export async function getErrorItemRole(userId: string, itemId: string): Promise<NotebookRole | null> {
|
||
const item = await prisma.errorItem.findUnique({
|
||
where: { id: itemId },
|
||
select: {
|
||
userId: true,
|
||
subjectId: true,
|
||
subject: {
|
||
select: {
|
||
userId: true,
|
||
shares: {
|
||
where: { memberId: userId, status: "active" },
|
||
select: { role: true },
|
||
take: 1,
|
||
},
|
||
},
|
||
},
|
||
},
|
||
});
|
||
return item ? resolveErrorItemRole(userId, item) : null;
|
||
}
|
||
|
||
/** 错题访问判定所需 select;共享查询按当前用户与 active 过滤 */
|
||
export function errorItemAccessSelect(userId: string) {
|
||
return {
|
||
userId: true,
|
||
subjectId: true,
|
||
subject: { select: subjectAccessSelect(userId) },
|
||
} as const;
|
||
}
|
||
|
||
/** 错题本访问判定所需字段;shared 查询按当前用户与 active 过滤 */
|
||
export function subjectAccessSelect(userId: string) {
|
||
return {
|
||
userId: true,
|
||
shares: {
|
||
where: { memberId: userId, status: "active" },
|
||
select: { role: true },
|
||
take: 1,
|
||
},
|
||
} as const;
|
||
}
|
||
|
||
/**
|
||
* 可读错题条件:自己创建的题,或位于已接受共享的错题本中的题。
|
||
* 用关系过滤直接按当前用户与 active 共享判断,适合统计、复习等不预先取错题本 ID 的查询。
|
||
*/
|
||
export function accessibleErrorItemOwnerOrShareWhere(userId: string): Prisma.ErrorItemWhereInput {
|
||
return {
|
||
OR: [
|
||
{ userId },
|
||
{ subject: { shares: { some: { memberId: userId, status: "active" } } } },
|
||
],
|
||
};
|
||
}
|
||
|
||
/** 可读错题条件:自己创建的题,或位于可读错题本(自有/共享)中的题 */
|
||
export function accessibleErrorItemWhere(userId: string, readIds: string[]): Prisma.ErrorItemWhereInput {
|
||
if (readIds.length === 0) return { userId };
|
||
return { OR: [{ userId }, { subjectId: { in: readIds } }] };
|
||
}
|
||
|
||
/** 可写错题条件:无错题本的自己的题,或位于自有/edit 共享错题本中的题 */
|
||
export function writableErrorItemWhere(userId: string, writeIds: string[]): Prisma.ErrorItemWhereInput {
|
||
const conditions: Prisma.ErrorItemWhereInput[] = [{ userId, subjectId: null }];
|
||
if (writeIds.length > 0) conditions.push({ subjectId: { in: writeIds } });
|
||
return { OR: conditions };
|
||
}
|
||
|
||
/** 可访问错题本条件:自有或已接受共享 */
|
||
export function accessibleSubjectWhere(userId: string): Prisma.SubjectWhereInput {
|
||
return { OR: [{ userId }, { shares: { some: { memberId: userId, status: "active" } } }] };
|
||
}
|