From b980ea9eb62ebbd165a60fea6a2e44948bfcfeda Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 16 Sep 2026 17:55:53 +0200 Subject: [PATCH] fix: accept numeric teacherId in lesson bulk upload Excel cells arrive as JSON numbers, but Lesson.teacherId only had @IsDigitsOnly (a string validator), so every row of the lessons bulk-upload failed validation with 'teacherId must contain digits only'. Add @StringType (Type(() => String)) like Student.tz, so class-transformer coerces the number to a digits string before both the CREATE-group ValidationPipe and the entity save. Covered by a new Lesson.entity.spec.ts that validates a row exactly as the client parses it from the lessons_kodesh Excel (numeric teacherId/klasses, date strings, empty year) and asserts the @StringType transform, CREATE-group validation, and fillFields reference resolution. --- server/src/db/entities/Lesson.entity.ts | 1 + .../entities/__tests__/Lesson.entity.spec.ts | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 server/src/db/entities/__tests__/Lesson.entity.spec.ts diff --git a/server/src/db/entities/Lesson.entity.ts b/server/src/db/entities/Lesson.entity.ts index 79f9437e..3a7f28a3 100644 --- a/server/src/db/entities/Lesson.entity.ts +++ b/server/src/db/entities/Lesson.entity.ts @@ -130,6 +130,7 @@ export class Lesson implements IHasUserId { klassReferenceIdsJson: string; @IsDigitsOnly(10, { always: true }) + @StringType @Column('varchar', { name: 'teacher_id', length: 10, nullable: true }) teacherId: string; diff --git a/server/src/db/entities/__tests__/Lesson.entity.spec.ts b/server/src/db/entities/__tests__/Lesson.entity.spec.ts new file mode 100644 index 00000000..e2249b4c --- /dev/null +++ b/server/src/db/entities/__tests__/Lesson.entity.spec.ts @@ -0,0 +1,69 @@ +const mockDestroy = jest.fn(); +const mockGetRepository = jest.fn(); + +// Only getDataSource is mocked (to avoid a real DB connection); IsUniqueCombination +// (key uniqueness during validation) and fillFields run for real against it. +jest.mock('@shared/utils/entity/foreignKey.util', () => ({ + ...jest.requireActual('@shared/utils/entity/foreignKey.util'), + getDataSource: jest.fn().mockResolvedValue({ + getRepository: (...args: any[]) => mockGetRepository(...args), + destroy: mockDestroy, + }), +})); + +import { plainToInstance } from 'class-transformer'; +import { validate } from 'class-validator'; +import { CrudValidationGroups } from '@dataui/crud'; +import { Lesson } from 'src/db/entities/Lesson.entity'; +import { getCurrentHebrewYear } from '@shared/utils/entity/year.util'; + +// A row exactly as the client parses it from the lessons bulk-upload Excel: +// numeric cells arrive as numbers, empty cells are omitted, dates as 'YYYY-MM-DD'. +const excelRow = { + key: 71101, + name: 'השקפה ואקטואליה כיתה י"ג1', + klasses: 71100, + teacherId: 21617840, + startDate: '2026-09-01', + endDate: '2027-08-31', + displayName: 'השקפה ואקטואליה', +}; + +describe('Lesson bulk upload', () => { + beforeEach(() => { + mockGetRepository.mockReset(); + mockDestroy.mockReset(); + }); + + it('transforms the numeric Excel teacherId into a digits string (IsDigitsOnly)', () => { + const lesson = plainToInstance(Lesson, excelRow); + expect(lesson.teacherId).toBe('21617840'); + }); + + it('passes CREATE validation for a row parsed from the bulk-upload Excel', async () => { + mockGetRepository.mockImplementation((entity: any) => { + if (entity.name === 'Lesson') return { countBy: jest.fn().mockResolvedValue(0) }; + throw new Error(`unexpected repo for ${entity?.name}`); + }); + + const lesson = plainToInstance(Lesson, excelRow); + const errors = await validate(lesson, { groups: [CrudValidationGroups.CREATE] }); + expect(errors).toEqual([]); + }); + + it('fillFields resolves klass/teacher references and defaults the year', async () => { + mockGetRepository.mockImplementation((entity: any) => { + if (entity.name === 'Klass') return { find: jest.fn().mockResolvedValue([{ id: 55 }]) }; + if (entity.name === 'Teacher') return { findOne: jest.fn().mockResolvedValue({ id: 77 }) }; + throw new Error(`unexpected repo for ${entity?.name}`); + }); + + const lesson = plainToInstance(Lesson, excelRow) as Lesson; + await lesson.fillFields(); + + expect(lesson.klassReferenceIds).toEqual([55]); + expect(lesson.teacherReferenceId).toBe(77); + expect(lesson.year).toBe(getCurrentHebrewYear()); + expect(lesson.startDate).toEqual(new Date('2026-09-01')); + }); +}); \ No newline at end of file