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
72 changes: 72 additions & 0 deletions server/src/__tests__/att-report-bulk-validation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { validateBulk } from '@shared/base-entity/base-entity.util';
import { AttReport } from 'src/db/entities/AttReport.entity';

describe('AttReport bulk validation', () => {
const REPORT_DATE = '2026-09-16';

it('rejects non-numeric studentReferenceId even when studentTz is also supplied', async () => {
await expect(
validateBulk([{ studentTz: '1234567892', studentReferenceId: 'garbage', reportDate: REPORT_DATE }], AttReport),
).rejects.toThrow();
});

it('rejects non-numeric teacherReferenceId even when teacherId is also supplied', async () => {
await expect(
validateBulk([{ teacherId: '1234567892', teacherReferenceId: 'garbage', reportDate: REPORT_DATE }], AttReport),
).rejects.toThrow();
});

it('rejects non-numeric klassReferenceId even when klassId is also supplied', async () => {
await expect(
validateBulk([{ klassId: 7, klassReferenceId: 'garbage', reportDate: REPORT_DATE }], AttReport),
).rejects.toThrow();
});

it('rejects non-numeric lessonReferenceId even when lessonId is also supplied', async () => {
await expect(
validateBulk([{ lessonId: 7, lessonReferenceId: 'garbage', reportDate: REPORT_DATE }], AttReport),
).rejects.toThrow();
});

it('rejects non-numeric referenceIds when supplied without the tz/id sibling', async () => {
await expect(
validateBulk(
[
{ studentReferenceId: 'כל', reportDate: REPORT_DATE },
{ klassReferenceId: 'not-a-number', reportDate: REPORT_DATE },
],
AttReport,
),
).rejects.toThrow();
});

it('accepts valid numeric referenceIds, even alongside tz/id siblings', async () => {
await expect(
validateBulk(
[
{
studentReferenceId: 123,
teacherReferenceId: 456,
klassReferenceId: 45,
lessonReferenceId: 78,
studentTz: '1234567892',
teacherId: '1234567892',
klassId: 7,
lessonId: 8,
reportDate: REPORT_DATE,
},
],
AttReport,
),
).resolves.toBeUndefined();
});

it('accepts tz/id-based payload without referenceIds', async () => {
await expect(
validateBulk(
[{ studentTz: '1234567892', teacherId: '1234567892', klassId: 7, lessonId: 8, reportDate: REPORT_DATE }],
AttReport,
),
).resolves.toBeUndefined();
});
});
44 changes: 32 additions & 12 deletions server/src/db/entities/AttReport.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,15 @@ export class AttReport implements IHasUserId {
@Column('varchar', { name: 'student_tz', length: 10, nullable: true })
studentTz: string;

@ValidateIf((attReport: AttReport) => !Boolean(attReport.studentTz) && Boolean(attReport.studentReferenceId), {
always: true,
})
@ValidateIf(
(attReport: AttReport) => attReport.studentReferenceId !== undefined && attReport.studentReferenceId !== null,
{
always: true,
},
)
@IsNotEmpty({ groups: [CrudValidationGroups.CREATE] })
@NumberType
@IsNumber({ maxDecimalPlaces: 0 }, { always: true })
Comment on lines +121 to +122
@Column({ nullable: true })
@Index('att_reports_student_reference_id_idx')
studentReferenceId: number;
Expand All @@ -125,10 +130,15 @@ export class AttReport implements IHasUserId {
@Column('varchar', { name: 'teacher_id', length: 10, nullable: true })
teacherId: string;

@ValidateIf((attReport: AttReport) => !Boolean(attReport.teacherId) && Boolean(attReport.teacherReferenceId), {
always: true,
})
@ValidateIf(
(attReport: AttReport) => attReport.teacherReferenceId !== undefined && attReport.teacherReferenceId !== null,
{
always: true,
},
)
@IsNotEmpty({ groups: [CrudValidationGroups.CREATE] })
@NumberType
@IsNumber({ maxDecimalPlaces: 0 }, { always: true })
@Column({ nullable: true })
@Index('att_reports_teacher_reference_id_idx')
teacherReferenceId: number;
Expand All @@ -141,10 +151,15 @@ export class AttReport implements IHasUserId {
@Column('int', { name: 'klass_id', nullable: true })
klassId: number | null;

@ValidateIf((attReport: AttReport) => !Boolean(attReport.klassId) && Boolean(attReport.klassReferenceId), {
always: true,
})
@ValidateIf(
(attReport: AttReport) => attReport.klassReferenceId !== undefined && attReport.klassReferenceId !== null,
{
always: true,
},
)
@IsNotEmpty({ groups: [CrudValidationGroups.CREATE] })
@NumberType
@IsNumber({ maxDecimalPlaces: 0 }, { always: true })
@Column({ nullable: true })
@Index('att_reports_klass_reference_id_idx')
klassReferenceId: number;
Expand All @@ -156,9 +171,14 @@ export class AttReport implements IHasUserId {
@Column('int', { name: 'lesson_id', nullable: true })
lessonId: number;

@ValidateIf((attReport: AttReport) => !Boolean(attReport.lessonId) && Boolean(attReport.lessonReferenceId), {
always: true,
})
@ValidateIf(
(attReport: AttReport) => attReport.lessonReferenceId !== undefined && attReport.lessonReferenceId !== null,
{
always: true,
},
)
@NumberType
@IsNumber({ maxDecimalPlaces: 0 }, { always: true })
@Column({ nullable: true })
@Index('att_reports_lesson_reference_id_idx')
lessonReferenceId: number;
Expand Down
Loading