Skip to content

Fix: AttReport bulk insert 500 — add numeric validation to ReferenceId columns - #188

Closed
buzz-code wants to merge 2 commits into
mainfrom
fix/att-report-reference-id-numeric-validation
Closed

buzz-code wants to merge 2 commits into
mainfrom
fix/att-report-reference-id-numeric-validation

Conversation

@buzz-code

Copy link
Copy Markdown
Owner

Bug

POST /att_report/bulk returned HTTP 500 when the payload contained non-integer values for the AttReport integer reference columns:

  • Incorrect integer value: 'כל' for column 'klassReferenceId'
  • Incorrect integer value: 'not-a-number' for column 'studentReferenceId'

MySQL error 1366 (ER_TRUNCATED_WRONG_VALUE_FOR_FIELD).

Root cause

In server/src/db/entities/AttReport.entity.ts, studentReferenceId and klassReferenceId had only @ValidateIf/@IsNotEmpty — no numeric validation — so non-numeric strings passed validation, survived fillFields() (findOneAndAssignReferenceId passes the value through when set), and hit the int DB column.

The analogous fields year, klassId, lessonId already had @NumberType + @IsNumber({ maxDecimalPlaces: 0 }, { always: true }) from #186; the ReferenceId columns were missed.

Fix

Adds the same validators to all four int ReferenceId columns:

  • studentReferenceId
  • klassReferenceId
  • teacherReferenceId (same latent gap, fixed for consistency)
  • lessonReferenceId (same latent gap, fixed for consistency)

@NumberType passes non-numeric strings through unchanged, so @IsNumber now rejects them with a 400 instead of a MySQL 500. Numeric strings (e.g. "123") are still coerced to numbers, matching the existing fields' behavior.

Verification

  • validateBulk (the real bulk path: plainToInstance + validate) rejects { studentReferenceId: 'כל' } and { klassReferenceId: 'not-a-number' }, and still accepts numeric reference IDs and tz-based payloads.
  • yarn test: 115/117 suites pass; the 2 failing suites (attendance-cleanup-sweep.handler, year.util.behavior) fail identically on clean main — pre-existing, unrelated.

No refactor — validators only.

POST /att_report/bulk returned HTTP 500 (MySQL 1366) when bulk payloads
contained non-integer strings for klassReferenceId ('כל') or
studentReferenceId ('not-a-number'). These columns had only
@ValidateIf/@isnotempty and no numeric validation, unlike year/klassId/
lessonId which got @NumberType + @isnumber({maxDecimalPlaces:0}) in #186.

Adds the same validators to studentReferenceId, klassReferenceId,
teacherReferenceId and lessonReferenceId (all int columns), so invalid
values now fail validation with 400 instead of reaching MySQL.
Copilot AI lite review requested due to automatic review settings September 16, 2026 12:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

@ValidateIf can skip validation when a timezone field is present, allowing invalid IDs to reach MySQL.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Fixes AttReport bulk inserts that return HTTP 500 for non-numeric reference IDs.

Changes:

  • Adds numeric validation to four integer reference ID fields.
  • Preserves existing numeric-string coercion behavior.
File summaries
File Summary
server/src/db/entities/AttReport.entity.ts Adds reference-ID validation decorators; validation can still be bypassed when timezone fields are present.
Review details

Suppressed comments (3)

server/src/db/entities/AttReport.entity.ts:135

  • @ValidateIf disables these validators when teacherId is present. fillFields() returns any truthy teacherReferenceId unchanged, so a payload containing both fields with a non-numeric reference ID still reaches the integer column and can produce HTTP 500. Make this validation depend only on whether teacherReferenceId is supplied, or reject conflicting inputs.
  @NumberType
  @IsNumber({ maxDecimalPlaces: 0 }, { always: true })

server/src/db/entities/AttReport.entity.ts:153

  • @ValidateIf disables these validators when klassId is present. fillFields() returns any truthy klassReferenceId unchanged, so a payload containing both fields with a non-numeric reference ID still reaches the integer column and can produce HTTP 500. Make this validation depend only on whether klassReferenceId is supplied, or reject conflicting inputs.
  @NumberType
  @IsNumber({ maxDecimalPlaces: 0 }, { always: true })

server/src/db/entities/AttReport.entity.ts:169

  • @ValidateIf disables these validators when lessonId is present. fillFields() returns any truthy lessonReferenceId unchanged, so a payload containing both fields with a non-numeric reference ID still reaches the integer column and can produce HTTP 500. Make this validation depend only on whether lessonReferenceId is supplied, or reject conflicting inputs.
  @NumberType
  @IsNumber({ maxDecimalPlaces: 0 }, { always: true })
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +118 to +119
@NumberType
@IsNumber({ maxDecimalPlaces: 0 }, { always: true })
…ling bypass

QA found a bypass in 5ce3752: the @ValidateIf predicates gated ALL
validation on the tz/id sibling being empty, so a payload supplying
BOTH studentTz AND studentReferenceId='garbage' skipped @isnumber and
hit MySQL 1366 -> HTTP 500 again.

Predicates now run validation whenever the referenceId value is
present (!== undefined && !== null), regardless of the sibling field.
Adds regression spec proving 'garbage' is rejected even when the
sibling tz/id is also supplied, plus tz/id-only payloads still pass.
@buzz-code

Copy link
Copy Markdown
Owner Author

QA-bypass fix pushed in db58962.

Bypass: the @ValidateIf predicates gated all validation on the tz/id sibling being empty, so a payload with BOTH e.g. studentTz AND studentReferenceId='garbage' skipped @IsNumber and reached MySQL again (1366 → 500).

Fix: all 4 ReferenceId predicates (studentReferenceId, teacherReferenceId, klassReferenceId, lessonReferenceId) now trigger validation whenever the value is present (!== undefined && !== null), regardless of the sibling field.

Regression spec: server/src/__tests__/att-report-bulk-validation.spec.ts — proves 'garbage' is rejected for all 4 fields even when the sibling tz/id is also supplied; valid numeric values (with or without siblings) and tz/id-only payloads still pass.

yarn test: 116/118 suites pass; only the 2 pre-existing unrelated failures remain (attendance-cleanup-sweep.handler, year.util.behavior), identical on clean main.

buzz-code pushed a commit that referenced this pull request Sep 16, 2026
…ling bypass

QA found the same bypass PR #188 (db58962) fixed for AttReport in the
sibling-gated @ValidateIf predicates: a payload supplying BOTH the
tz/id sibling AND referenceId='garbage' made the predicate false,
skipping @isnumber and reaching the MySQL INT column (1366 -> 500).

Predicates on every *ReferenceId field now run validation whenever the
value is present (field !== undefined && field !== null), regardless
of the sibling tz/id field — matching db58962's exact form. The
sibling-guarding @ValidateIf blocks (e.g. !Boolean(studentReferenceId)
above studentTz) are untouched; @IsNotEmpty/@IsOptional kept as-is.
Fields without a @ValidateIf (reportGroupSessionId x2, Student.year,
Lesson/Klass/KlassType teacherReferenceId, KnownAbsence.lessonReferenceId,
AttendanceCleanupRule ReferenceIds) needed no predicate change.

AttReport's four ReferenceId fields also gain the @NumberType +
@isnumber pair on this branch (5ce3752 added it on the unmerged PR
#188; without it the predicates had no numeric validator to run, and
the required both-present regression test cannot pass).

Regression tests added: 'garbage' studentReferenceId rejected even
when studentTz is also supplied, for Grade and AttReport (89 -> 91
tests).
@buzz-code

Copy link
Copy Markdown
Owner Author

נסגר ככפילות: התיקון כאן כלול ב-#190 (מכסה את AttReport ועוד 9 ישויות, כולל הבדיקות).

@buzz-code buzz-code closed this Sep 22, 2026
TziporaStern pushed a commit to TziporaStern/react-admin-nestjs that referenced this pull request Oct 17, 2026
…olumns)

Extends the PR buzz-code#186 / PR buzz-code#188 @NumberType + @isnumber({maxDecimalPlaces:0})
fix to the numeric ReferenceId/int columns still missing it, so a
non-numeric string fails class-validator with a clear 400 instead of
reaching the MySQL INT column as an HTTP-500:

- AttReport.reportGroupSessionId, Grade.reportGroupSessionId
- Grade: studentReferenceId, teacherReferenceId, lessonReferenceId
- AttendanceCleanupRule: lessonReferenceId, klassReferenceId
- StudentKlass: studentReferenceId, klassReferenceId
- KnownAbsence: studentReferenceId, lessonReferenceId
- Lesson/LessonSchedule/Klass/KlassType: teacherReferenceId
- Student.year

Bare fields get the full PR buzz-code#186 pattern (@IsOptional({always:true}) +
pair); fields with existing @ValidateIf/@IsNotEmpty/@IsOptional keep
them and only gain the @NumberType + @isnumber pair right before
@column, matching the placement PR buzz-code#188 used on AttReport. The
ValidateIf predicates all include '&& Boolean(field)', so omitted
values skip validation and partial updates keep passing.

New spec mirrors the CRUD validation pipeline (plainToInstance +
validate with CREATE/UPDATE groups): garbage rejected on create and
update, integers and numeric strings (NumberType coercion) accepted,
partial updates omitting the fields still pass, plus full-payload
create scenarios for Grade and Student.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants