fix: audit remediation — SSE user scoping, FK transactional safety, UI error handling

- H4: scoped SSE import progress to exact userId match; non-HTTP events excluded from all subscribers
- H2: moved PRAGMA foreign_key_check inside SQLite transaction before COMMIT; violations rollback preserving old tables
- M1: removed dead axios-style error branch from extractErrorMessage (interceptor already unwraps)
- M2: split handleSave try/catch — save errors vs reload errors shown distinctly
- M3: added provider field validation before AI config test request
- Added SSE scoping regression tests (import service + controller)
- Added FK check failure rollback test (database-migrations.spec)
- Updated controller spec expectations for userId parameter

Co-authored-by: Code Review <branch-review>
This commit is contained in:
2026-07-12 22:59:03 +08:00
parent b6fca99390
commit cc4f4dae4e
69 changed files with 6262 additions and 1980 deletions

View File

@@ -0,0 +1,53 @@
import { describe, expect, it } from 'vitest';
import {
canPullAttendance,
getAttendanceExperience,
getSchedulePhase,
summarizeAttendance,
} from './attendance-workspace';
describe('attendance role experience', () => {
it('routes class-scoped teachers to the teaching workspace', () => {
expect(
getAttendanceExperience(
['attendance:view', 'attendance:create', 'schedule:create'],
['老师'],
),
).toBe('teacher');
});
it('routes users with attendance administration permission to history management', () => {
expect(getAttendanceExperience(['attendance:view', 'attendance:edit'], ['教务管理员'])).toBe(
'admin',
);
});
});
describe('teacher schedule phase', () => {
it('marks a finished lesson as ready for attendance review', () => {
expect(getSchedulePhase('08:00', '09:00', new Date('2026-07-11T10:00:00'))).toBe('ended');
});
it('keeps future lessons read-only', () => {
expect(getSchedulePhase('14:00', '15:00', new Date('2026-07-11T10:00:00'))).toBe('upcoming');
});
it('allows attendance pulls once the lesson starts', () => {
expect(canPullAttendance('ongoing')).toBe(true);
expect(canPullAttendance('ended')).toBe(true);
expect(canPullAttendance('upcoming')).toBe(false);
});
});
describe('attendance summary', () => {
it('summarizes status counts for an administrator history view', () => {
expect(
summarizeAttendance([
{ status: 'present' },
{ status: 'present' },
{ status: 'late' },
{ status: 'absent' },
]),
).toEqual({ total: 4, present: 2, late: 1, absent: 1, leave: 0, pending: 0 });
});
});

View File

@@ -0,0 +1,65 @@
export type AttendanceExperience = 'teacher' | 'admin';
export type SchedulePhase = 'upcoming' | 'ongoing' | 'ended';
import { getRoleDomains } from '../../auth/menu-policy';
export function getAttendanceExperience(
permissions: readonly string[],
roles: readonly string[],
): AttendanceExperience {
const domains = getRoleDomains(roles, permissions);
if (
permissions.includes('attendance:manage') ||
domains.has('academic') ||
domains.has('super')
) {
return 'admin';
}
return 'teacher';
}
function toMinuteOfDay(time: string): number {
const [hour = 0, minute = 0] = time.split(':').map(Number);
return hour * 60 + minute;
}
export function getSchedulePhase(
startTime: string,
endTime: string,
now = new Date(),
): SchedulePhase {
const current = now.getHours() * 60 + now.getMinutes();
if (current < toMinuteOfDay(startTime)) return 'upcoming';
if (current <= toMinuteOfDay(endTime)) return 'ongoing';
return 'ended';
}
export function canPullAttendance(phase: SchedulePhase): boolean {
return phase !== 'upcoming';
}
export interface AttendanceSummary {
total: number;
present: number;
late: number;
absent: number;
leave: number;
pending: number;
}
export function summarizeAttendance(records: readonly { status: string }[]): AttendanceSummary {
const summary: AttendanceSummary = {
total: records.length,
present: 0,
late: 0,
absent: 0,
leave: 0,
pending: 0,
};
for (const record of records) {
if (record.status in summary && record.status !== 'total') {
summary[record.status as Exclude<keyof AttendanceSummary, 'total'>] += 1;
}
}
return summary;
}

View File

@@ -0,0 +1,504 @@
.attendance-page {
--ink: #172033;
--muted: #667085;
--line: #e6eaf0;
--blue: #1677ff;
color: var(--ink);
padding-bottom: 28px;
}
.attendance-hero {
position: relative;
overflow: hidden;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
min-height: 188px;
padding: 34px 38px;
margin-bottom: 18px;
border-radius: 18px;
}
.attendance-hero::after {
content: '';
position: absolute;
width: 320px;
height: 320px;
right: -105px;
top: -165px;
border: 64px solid rgb(255 255 255 / 8%);
border-radius: 50%;
pointer-events: none;
}
.attendance-hero--teacher {
color: white;
background: linear-gradient(125deg, #122c5a 0%, #174c93 58%, #1e78c8 100%);
box-shadow: 0 14px 34px rgb(24 76 147 / 18%);
}
.attendance-hero--admin {
color: #172033;
background: linear-gradient(120deg, #f7f9fc 0%, #eef3f9 100%);
border: 1px solid #e2e8f0;
}
.attendance-hero h1 {
margin: 7px 0 8px;
font-size: clamp(28px, 3vw, 40px);
line-height: 1.15;
letter-spacing: -1.2px;
}
.attendance-hero p {
max-width: 650px;
margin: 0;
color: inherit;
opacity: 0.72;
font-size: 15px;
}
.attendance-eyebrow {
font-size: 11px;
font-weight: 700;
letter-spacing: 1.7px;
opacity: 0.72;
}
.teacher-overview {
margin-bottom: 30px;
}
.teacher-kpi {
height: 112px;
padding: 21px 24px;
border: 1px solid var(--line);
border-radius: 14px;
background: white;
box-shadow: 0 5px 16px rgb(19 33 68 / 5%);
}
.teacher-kpi > span,
.teacher-kpi > small {
display: block;
color: var(--muted);
}
.teacher-kpi > strong {
display: inline-block;
margin: 6px 7px 0 0;
font-size: 30px;
line-height: 1;
}
.teacher-kpi--next {
border-color: #cfe1fb;
background: #f4f8ff;
}
.attendance-section-heading {
display: flex;
align-items: flex-end;
justify-content: space-between;
margin: 0 2px 14px;
}
.attendance-section-heading span {
color: var(--muted);
font-size: 12px;
}
.attendance-section-heading h2 {
margin: 2px 0 0;
font-size: 22px;
}
.lesson-timeline {
display: grid;
gap: 12px;
}
.lesson-card {
display: grid;
grid-template-columns: 54px 120px minmax(220px, 1fr) auto;
align-items: center;
min-height: 116px;
padding: 18px 20px 18px 12px;
border: 1px solid var(--line);
border-left: 4px solid #c9d2df;
border-radius: 14px;
background: white;
transition: transform 180ms ease, box-shadow 180ms ease, border-color 180ms ease;
}
.lesson-card:hover {
transform: translateY(-1px);
box-shadow: 0 10px 26px rgb(24 39 75 / 9%);
}
.lesson-card--ongoing {
border-left-color: #1677ff;
background: linear-gradient(90deg, #f6f9ff, #fff 32%);
}
.lesson-card--ended {
border-left-color: #32a46d;
}
.lesson-sequence {
align-self: start;
padding-top: 3px;
color: #a6b0bf;
font-size: 12px;
font-weight: 700;
letter-spacing: 1px;
}
.lesson-time {
display: grid;
grid-template-columns: auto 1px auto;
align-items: center;
gap: 10px;
padding-right: 22px;
}
.lesson-time strong {
font-size: 15px;
}
.lesson-time span {
height: 28px;
background: #d9e0e9;
}
.lesson-main {
padding-left: 24px;
border-left: 1px solid var(--line);
}
.lesson-title-row {
display: flex;
align-items: center;
gap: 10px;
}
.lesson-title-row h3 {
margin: 0;
font-size: 18px;
}
.lesson-main p {
display: flex;
gap: 18px;
margin: 8px 0 0;
color: var(--muted);
}
.lesson-action {
padding-left: 20px;
}
.attendance-empty-card {
padding: 28px;
border-radius: 14px;
}
.attendance-empty-card strong {
display: block;
color: var(--ink);
font-size: 16px;
}
.attendance-empty-card p {
margin: 4px 0 0;
color: var(--muted);
}
.lesson-record-header {
padding: 8px 0 22px;
}
.lesson-record-header h2 {
margin: 6px 0;
font-size: 28px;
}
.lesson-record-header p {
margin: 0;
color: var(--muted);
}
.attendance-summary-strip {
display: grid;
grid-template-columns: minmax(190px, 1.35fr) repeat(4, minmax(100px, 1fr));
align-items: center;
gap: 0;
min-height: 106px;
margin-bottom: 18px;
padding: 16px 8px;
border: 1px solid var(--line);
border-radius: 14px;
background: white;
box-shadow: 0 5px 18px rgb(23 32 51 / 4%);
}
.attendance-rate,
.attendance-summary-cell {
display: flex;
align-items: center;
gap: 12px;
min-height: 66px;
padding: 0 20px;
}
.attendance-rate {
border-right: 1px solid var(--line);
}
.attendance-rate span,
.attendance-summary-cell span {
display: block;
color: var(--muted);
font-size: 12px;
}
.attendance-rate strong,
.attendance-summary-cell strong {
display: block;
margin-top: 3px;
font-size: 20px;
}
.attendance-summary-icon {
display: grid !important;
flex: 0 0 auto;
width: 36px;
height: 36px;
place-items: center;
border-radius: 10px;
font-weight: 700;
}
.is-present { color: #198754 !important; background: #eaf8f1; }
.is-late { color: #b56b00 !important; background: #fff4db; }
.is-absent { color: #cf3030 !important; background: #fff0f0; }
.is-leave { color: #2874c6 !important; background: #edf5ff; }
.is-pending { color: #667085 !important; background: #f1f3f6; }
.attendance-status {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 4px 9px;
border-radius: 999px;
font-size: 12px;
font-weight: 600;
}
.attendance-status__dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
}
.archive-alert {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 16px;
padding: 13px 16px;
color: #8c5d05;
border: 1px solid #f3d79c;
border-radius: 12px;
background: #fffbf0;
}
.archive-alert > span.anticon {
font-size: 20px;
}
.archive-alert div {
flex: 1;
}
.archive-alert strong,
.archive-alert span {
display: block;
}
.archive-alert span {
margin-top: 2px;
color: #8a7452;
font-size: 12px;
}
.archive-card {
overflow: hidden;
border: 1px solid var(--line);
border-radius: 14px;
box-shadow: 0 6px 24px rgb(23 32 51 / 5%);
}
.archive-card .ant-card-body {
padding: 0;
}
.archive-toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 18px 20px;
border-bottom: 1px solid var(--line);
background: #fbfcfe;
}
.archive-toolbar__title {
display: flex;
align-items: center;
gap: 11px;
min-width: 150px;
}
.archive-toolbar__title > span.anticon {
color: var(--blue);
font-size: 21px;
}
.archive-toolbar__title strong,
.archive-toolbar__title span {
display: block;
}
.archive-toolbar__title span {
color: var(--muted);
font-size: 11px;
}
.archive-card .ant-table-wrapper {
padding: 0 20px 10px;
}
.archive-card .ant-table-thead > tr > th {
color: #667085;
font-size: 12px;
font-weight: 600;
background: white;
}
.student-cell {
display: flex;
align-items: center;
gap: 10px;
}
.student-cell .ant-avatar {
color: #245b9e;
background: #e8f1fd;
}
.student-cell strong,
.student-cell span {
display: block;
}
.student-cell span {
margin-top: 2px;
color: var(--muted);
font-size: 11px;
}
.muted-text {
color: #a1a9b5;
}
@media (max-width: 900px) {
.attendance-hero,
.archive-toolbar {
align-items: flex-start;
flex-direction: column;
}
.attendance-summary-strip {
grid-template-columns: repeat(2, 1fr);
}
.attendance-rate {
grid-column: 1 / -1;
border-right: 0;
border-bottom: 1px solid var(--line);
padding-bottom: 14px;
}
.lesson-card {
grid-template-columns: 42px 1fr auto;
}
.lesson-time {
grid-column: 2;
}
.lesson-main {
grid-column: 2 / -1;
margin-top: 12px;
padding: 12px 0 0;
border-top: 1px solid var(--line);
border-left: 0;
}
.lesson-action {
grid-column: 2 / -1;
padding: 14px 0 0;
}
}
@media (max-width: 576px) {
.attendance-hero {
padding: 26px 22px;
border-radius: 14px;
}
.attendance-hero h1 {
font-size: 28px;
}
.attendance-summary-strip {
grid-template-columns: 1fr 1fr;
}
.attendance-summary-cell {
padding: 10px 14px;
}
.lesson-card {
grid-template-columns: 1fr;
padding: 18px;
}
.lesson-sequence {
display: none;
}
.lesson-time,
.lesson-main,
.lesson-action {
grid-column: 1;
}
.lesson-main p {
flex-direction: column;
gap: 4px;
}
}
.attendance-marking-actions {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.attendance-marking-actions .ant-btn {
min-width: 54px;
}

File diff suppressed because it is too large Load Diff