Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/src/db/entities/Lesson.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
69 changes: 69 additions & 0 deletions server/src/db/entities/__tests__/Lesson.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
});
Loading