From 9274f6cfcc60be98e3156bbd2070163a47549716 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 23 Aug 2026 19:14:04 +0000 Subject: [PATCH 1/2] Fix 500 on creating attendance_cleanup_rule: incomplete entity set fillFields() built an ad-hoc DataSource with getDataSource([Lesson, Klass, User]), but Klass has relations to KlassType and Teacher (and Lesson to LessonKlassName) that TypeORM must resolve when building entity metadata, even though this code never touches them. Missing targets throw at DataSource.initialize(): Entity metadata for Klass#klassType was not found. Reproduced against a real MySQL instance and confirmed fixed. Every other entity in this codebase resolving Lesson/Klass this way (AttReport, Grade, KnownAbsence, LessonSchedule, StudentKlass) already includes the full closure - this one was left incomplete. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s --- server/src/db/entities/AttendanceCleanupRule.entity.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/src/db/entities/AttendanceCleanupRule.entity.ts b/server/src/db/entities/AttendanceCleanupRule.entity.ts index e9f6dc33..15d6789f 100644 --- a/server/src/db/entities/AttendanceCleanupRule.entity.ts +++ b/server/src/db/entities/AttendanceCleanupRule.entity.ts @@ -19,7 +19,10 @@ import { StringType, NumberType } from '@shared/utils/entity/class-transformer'; import { CreatedAtColumn, UpdatedAtColumn } from '@shared/utils/entity/column-types.util'; import { Lesson } from './Lesson.entity'; import { Klass } from './Klass.entity'; +import { KlassType } from './KlassType.entity'; +import { Teacher } from './Teacher.entity'; import { User } from './User.entity'; +import { LessonKlassName } from '../view-entities/LessonKlassName.entity'; /** * A recurring attendance-cleanup rule: on `dayOfWeek`, delete att_reports for @@ -46,7 +49,7 @@ export class AttendanceCleanupRule implements IHasUserId { async fillFields() { let dataSource: DataSource; try { - dataSource = await getDataSource([Lesson, Klass, User]); + dataSource = await getDataSource([Lesson, Klass, KlassType, Teacher, User, LessonKlassName]); this.lessonId = await findOneAndAssignKey(dataSource, Lesson, this.userId, this.lessonReferenceId, this.lessonId); this.klassId = await findOneAndAssignKey(dataSource, Klass, this.userId, this.klassReferenceId, this.klassId); From 597f457e241fb762b0508c1a0cbd98a6eb94410d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 23 Aug 2026 19:35:32 +0000 Subject: [PATCH 2/2] Add unmocked regression test for AttendanceCleanupRule's DataSource entity closure Addresses Copilot review comment on PR #152: the existing fillFields tests mock getDataSource entirely, so they'd still pass if a relation target were dropped from the entity list again - exactly what caused the production 500. This test doesn't mock getDataSource, so it builds real TypeORM metadata (against Jest's default sqlite in-memory databaseConfig). Confirmed it fails with the exact production error when reverted to the old [Lesson, Klass, User] list, and passes with the fix. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s --- ...tendance-cleanup-rule.entity.datasource.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 server/src/jobs/__tests__/attendance-cleanup-rule.entity.datasource.spec.ts diff --git a/server/src/jobs/__tests__/attendance-cleanup-rule.entity.datasource.spec.ts b/server/src/jobs/__tests__/attendance-cleanup-rule.entity.datasource.spec.ts new file mode 100644 index 00000000..d44d6fbe --- /dev/null +++ b/server/src/jobs/__tests__/attendance-cleanup-rule.entity.datasource.spec.ts @@ -0,0 +1,14 @@ +import { AttendanceCleanupRule } from 'src/db/entities/AttendanceCleanupRule.entity'; + +// Unlike attendance-cleanup-rule.entity.spec.ts, getDataSource here is NOT mocked - +// this exercises the real TypeORM metadata build (against Jest's sqlite in-memory +// databaseConfig) so an entity closure that's missing a relation target (as happened +// in production - Klass#klassType) fails this test instead of only failing at runtime. +describe('AttendanceCleanupRule.fillFields (real DataSource)', () => { + it('builds TypeORM metadata for the full entity set without throwing', async () => { + const rule = new AttendanceCleanupRule(); + rule.userId = 1; + + await rule.fillFields(); + }); +});