Files
ClassHelper/packages/server/src/lib/mappers.ts
T
LoveChengke 11008928a1 fix(island,homework,schedule,web): 16 项反馈修复(联合通知点不开 / 设置不生效 / 红点位置 / 看板当天且铺满 / 单双周与周数可调 / Web 手机端与批量通知等)
一、灵动岛
- ①「紧急通知后来了新作业,收起再点开不了新作业」根因有二,都已修:
  (a) active 仍是紧急那条,点击只会反复打开它 —— 现在点击胶囊优先打开**最新**那条未处理消息(队列里更晚的);
  (b) 上课时段 syncWindow 会把普通通知立刻隐藏(表现为点了没反应)—— 新增 explicitShowId,
      "用户主动点开"的那条守卫放行(岛完全隐藏时仍严格不打扰)。
  冒烟新增「紧急通知后来了新作业:收起再点击打开的是新作业(联合通知回归)」。
- ② 设置实时预览/保存:补「设置页真实链路」断言(window.desktop.islandSetAppearance → IPC → 主进程 →
  渲染进程 CSS 变量 + 窗口尺寸),并断言"岛显示中改尺寸立即生效"(实测均通过)。
- ③ 未读红点移到「通知」图标右上角(此前在文字后面),并加 DOM 几何断言(dx>0 且 dy<0,
  没有未读时自动造一条未读通知再断言、断言后删除)。
- 冒烟稳定性:新增 island.setBlurCollapseEnabled()(默认开),测试期间关掉"失焦收起",
  避免系统抢焦点导致展开态被收回的随机失败;专门验证失焦的用例仍临时打开。

二、作业看板(学生端)
- ⑥ 只看当天:新增「只看今天」开关(默认开),按 createdAt 的本地日期过滤,昨天的过期作业不再混入。
- ⑧ 完整显示:卡片同时渲染标题 + 作业要求全文(列表模式也加「作业要求」列)。
- ⑨ 铺满不滚动:看板外层高度固定 + overflow hidden,按内容/可用高度算缩放系数用 transform: scale 适配
  (工具栏显示当前缩放比例),页面本身不出现滚动条。
- ⑦ 全屏看板标题栏用 #header 插槽:标题左对齐、当天日期时间绝对居中(受「显示时间」开关控制,每分钟刷新)。

三、课表与周数
- ⑤ 学期周数可调:新增 Class.termWeeks(默认 20,迁移 + 维护脚本),班主任在班级管理里改;
  GET /api/dashboard/term?classId= 返回该班 maxWeek,课表周次下拉与新建课表的默认 weekEnd 都按它来
  (不再固定 30 周)。
- ⑩ 课表管理只保留「导入 ClassIsland 课程表」入口(时间表由课程表 JSON 的 TimeLayouts 一并带入),
  「导入时间配置」按钮下线;ui-smoke 第 7.7 段改为断言新弹窗(解析 4 行 + 单双周标签 + 旧入口已下线)。

四、以班级为单位(⑬)与 Web 端
- 个人学生账号登录停用:POST /api/auth/login 对 role=STUDENT 返回 403(改用班级码 + 班级密码);
  学生行仍作为个人数据记录键由班级会话代全班读写;e2e 全面切换到班级会话并新增该断言。
- ④ 作业科目过少:新增共享 SUBJECT_CATALOG(16 个常见科目)+ 种子课程扩到全科目;
  Web 作业发布弹窗可选「其他科目(新建)」,提交前自动建课(同名复用)。
- ⑪ 通知支持多班批量发布(多选 + 逐班发布 + 汇总结果 + 二次确认覆盖所有选中班);
  ⑫ 单班只显示已读情况、跨班才显示已读率;⑭ 手机端课表可横向滚动看全 7 天;
  ⑮ 手机端已连接不再显示孤立绿点(仅未连接时显示文字标签);⑯ 手机端「叫人/发布通知」按钮同宽左对齐;
  ⑬ Web 学生管理去掉「用户名」列(新建自动生成学号式用户名,导入仍支持显式指定)。

验证:verify:e2e 149/149、verify:web 20/20、verify:desktop 75/75(复跑 68/68,打包便携版 75/75 packaged=true)、
typecheck / lint / format 全绿;服务端与 Web 已重新打包并部署到本机安装目录。
2026-09-13 08:39:11 +08:00

325 lines
8.7 KiB
TypeScript

import type {
ClassDto,
ClassTeacherBrief,
CourseBrief,
CourseDto,
GradeDto,
HomeworkDto,
HomeworkStatusDto,
NotificationDto,
NotificationPriority,
ScheduleDto,
StudentDto,
UserDto,
UserRole,
} from '@classhelper/shared';
/**
* 数据库记录 -> API DTO 的映射层。
*
* 这里刻意使用结构化的 `XxxLike` 接口而不是 Prisma 生成的模型类型:
* 1) 生成代码的模型类型命名随版本变化,映射层不应该跟着漂移;
* 2) 只有被 include/select 出来的字段才会出现在结果里,结构化类型能精确表达这一点。
*/
const toIso = (value: Date): string => value.toISOString();
const toIsoOrNull = (value: Date | null | undefined): string | null => (value ? value.toISOString() : null);
/**
* "个人记录"归属范围解析:
* - 传入 userIds(班级账号 = 全班学生)时优先使用;
* - 否则退化为单个 userId(普通学生账号 = 自己)。
*/
function resolvePool(options: { userId?: string | null; userIds?: string[] }): string[] {
if (options.userIds && options.userIds.length > 0) return options.userIds;
return options.userId ? [options.userId] : [];
}
export interface UserLike {
id: string;
username: string;
name: string;
role: string;
classId: string | null;
createdAt: Date;
}
export interface TeacherBriefLike {
id: string;
name: string;
username: string;
}
export interface StudentLike extends UserLike {
class?: { name: string; grade: string } | null;
}
export interface CourseBriefLike {
id: string;
name: string;
}
export function toUserDto(user: UserLike): UserDto {
return {
id: user.id,
username: user.username,
name: user.name,
role: user.role as UserRole,
classId: user.classId,
createdAt: toIso(user.createdAt),
};
}
export function toStudentDto(user: StudentLike): StudentDto {
return {
...toUserDto(user),
className: user.class?.name ?? null,
grade: user.class?.grade ?? null,
};
}
export function toTeacherBrief(user: TeacherBriefLike): ClassTeacherBrief {
return { id: user.id, name: user.name, username: user.username };
}
export function toCourseBrief(course: CourseBriefLike): CourseBrief {
return { id: course.id, name: course.name };
}
/* ------------------------------------------------------------------ 班级 */
export interface ClassLike {
id: string;
name: string;
grade: string;
teacherId: string;
/** 本学期教学周数(班主任可调,默认 20) */
termWeeks?: number;
createdAt: Date;
teacher?: TeacherBriefLike | null;
_count?: {
students?: number;
courses?: number;
homeworks?: number;
notifications?: number;
enrollments?: number;
};
}
export function toClassDto(item: ClassLike): ClassDto {
return {
id: item.id,
name: item.name,
grade: item.grade,
teacherId: item.teacherId,
termWeeks: item.termWeeks ?? 20,
createdAt: toIso(item.createdAt),
teacher: item.teacher ? toTeacherBrief(item.teacher) : null,
studentCount: item._count?.students ?? undefined,
courseCount: item._count?.courses ?? undefined,
homeworkCount: item._count?.homeworks ?? undefined,
notificationCount: item._count?.notifications ?? undefined,
};
}
/* ------------------------------------------------------------------ 课程 */
export interface CourseLike {
id: string;
name: string;
teacherId: string;
classId: string;
createdAt: Date;
teacher?: TeacherBriefLike | null;
}
export function toCourseDto(item: CourseLike): CourseDto {
return {
id: item.id,
name: item.name,
teacherId: item.teacherId,
classId: item.classId,
createdAt: toIso(item.createdAt),
teacher: item.teacher ? toTeacherBrief(item.teacher) : null,
};
}
/* ------------------------------------------------------------------ 课表 */
export interface ScheduleLike {
id: string;
classId: string;
courseId: string;
dayOfWeek: number;
startTime: string;
endTime: string;
location: string | null;
weekStart: number;
weekEnd: number;
weekParity: string;
createdAt: Date;
course?: CourseBriefLike | null;
}
export function toScheduleDto(item: ScheduleLike): ScheduleDto {
return {
id: item.id,
classId: item.classId,
courseId: item.courseId,
dayOfWeek: item.dayOfWeek,
startTime: item.startTime,
endTime: item.endTime,
location: item.location,
weekStart: item.weekStart,
weekEnd: item.weekEnd,
weekParity: (item.weekParity ?? 'ALL') as ScheduleDto['weekParity'],
createdAt: toIso(item.createdAt),
course: item.course ? toCourseBrief(item.course) : null,
};
}
/* ------------------------------------------------------------------ 作业 */
export interface HomeworkStatusLike {
id: string;
homeworkId: string;
userId: string;
completed: boolean;
updatedAt: Date;
}
export interface HomeworkLike {
id: string;
classId: string;
courseId: string | null;
title: string;
content: string;
attachmentUrl: string | null;
createdBy: string;
createdAt: Date;
course?: CourseBriefLike | null;
creator?: TeacherBriefLike | null;
statuses?: HomeworkStatusLike[];
}
export function toHomeworkStatusDto(item: HomeworkStatusLike): HomeworkStatusDto {
return {
id: item.id,
homeworkId: item.homeworkId,
userId: item.userId,
completed: item.completed,
updatedAt: toIso(item.updatedAt),
};
}
/**
* @param options.userId 指定当前用户,用于填充 completed / homeworkStatus
* @param options.userIds 班级账号(班级设备)场景:以"全班学生"为范围找完成状态
* @param options.withStatus 是否统计完成人数(教师视角)
*/
export function toHomeworkDto(
item: HomeworkLike,
options: { userId?: string | null; userIds?: string[]; withStatus?: boolean } = {},
): HomeworkDto {
const statuses = item.statuses ?? [];
const pool = resolvePool(options);
const own = pool.length > 0 ? statuses.find((status) => pool.includes(status.userId)) : undefined;
return {
id: item.id,
classId: item.classId,
courseId: item.courseId,
title: item.title,
content: item.content,
attachmentUrl: item.attachmentUrl,
createdBy: item.createdBy,
createdAt: toIso(item.createdAt),
course: item.course ? toCourseBrief(item.course) : null,
creator: item.creator ? toTeacherBrief(item.creator) : null,
completed: own ? own.completed : undefined,
homeworkStatus: own ? toHomeworkStatusDto(own) : null,
completedCount: options.withStatus ? statuses.filter((status) => status.completed).length : undefined,
};
}
/* ------------------------------------------------------------------ 通知 */
export interface NotificationReadLike {
id: string;
notificationId: string;
userId: string;
readAt: Date;
}
export interface NotificationLike {
id: string;
classId: string;
title: string;
content: string;
priority: string;
createdBy: string;
createdAt: Date;
creator?: TeacherBriefLike | null;
reads?: NotificationReadLike[];
}
/**
* @param options.userId 指定当前用户(普通学生账号)
* @param options.userIds 班级账号(班级设备)场景:以"全班学生"为范围找已读状态
* @param options.withStatus 是否统计已读人数(教师视角)
*/
export function toNotificationDto(
item: NotificationLike,
options: { userId?: string | null; userIds?: string[]; withStatus?: boolean } = {},
): NotificationDto {
const reads = item.reads ?? [];
const pool = resolvePool(options);
const own = pool.length > 0 ? reads.find((read) => pool.includes(read.userId)) : undefined;
return {
id: item.id,
classId: item.classId,
title: item.title,
content: item.content,
priority: item.priority as NotificationPriority,
createdBy: item.createdBy,
createdAt: toIso(item.createdAt),
creator: item.creator ? toTeacherBrief(item.creator) : null,
read: own ? true : false,
readAt: toIsoOrNull(own?.readAt),
readCount: options.withStatus ? reads.length : undefined,
};
}
/* ------------------------------------------------------------------ 成绩 */
export interface GradeLike {
id: string;
classId: string;
courseId: string | null;
userId: string;
examName: string;
score: number;
totalScore: number;
publishedAt: Date;
course?: CourseBriefLike | null;
student?: TeacherBriefLike | null;
class?: { name: string } | null;
}
export function toGradeDto(item: GradeLike): GradeDto {
return {
id: item.id,
classId: item.classId,
courseId: item.courseId,
userId: item.userId,
examName: item.examName,
score: item.score,
totalScore: item.totalScore,
publishedAt: toIso(item.publishedAt),
course: item.course ? toCourseBrief(item.course) : null,
student: item.student ? toTeacherBrief(item.student) : null,
className: item.class?.name ?? null,
};
}