Skip to content

Attendance cleanup sweep: recurring per-lesson cleanup by track - #144

Merged
buzz-code merged 4 commits into
mainfrom
claude/attendance-cleanup-sweep
Aug 20, 2026
Merged

buzz-code merged 4 commits into
mainfrom
claude/attendance-cleanup-sweep

Conversation

@buzz-code

Copy link
Copy Markdown
Owner

What

Self-service, recurring attendance cleanup (the client-negotiated ₪500 feature): for a chosen lesson and a klass/track to preserve, every student not in that track has attendance for that lesson deleted, across the last N occurrences of a chosen weekday — automatically, every Motzei Shabbat. New rules (different lesson/day/track) are pure data, no redeploy.

Built entirely on the job queue infra already in this app (nra-server/nra-client #29–#38, wired in #141).

How it works

  • AttendanceCleanupRule — a small, friendly admin-only CRUD screen: lesson (dropdown), klass/track to preserve (dropdown), day of week (select), weeks back (number, default 2), active (toggle). No JSON, no IDs to hand-type.
  • attendance-cleanup-sweep handler — runs weekly: for every active rule owned by the job's user, computes the last weeksBack occurrences of dayOfWeek, and deletes att_reports for lessonReferenceId = <rule's lesson> AND reportDate = <target date> AND klassReferenceId != <preserved klass>.
  • Cross-year safe by design: the rule stores Lesson.key/Klass.key (stable identifiers), not a fixed referenceId — Lesson/Klass get new ids every academic year. The handler re-resolves key→referenceId against each target date's own academic year at run time, so a rule set up this year keeps working next year without anyone touching it. If a lesson/klass can't be resolved for some date's year, that date is skipped, never treated as "no exclusion" (which would over-delete).
  • "Run now" bulk button on the rule list enqueues the same handler scoped to just the selected rule(s), so a rule can be tested immediately instead of waiting for Saturday night.
  • One-time setup (not part of this PR, done once after merge): create a single Schedule row — jobType: attendance-cleanup-sweep, weekly cron for Motzei Shabbat (e.g. 0 22 * * 6 for Saturday 22:00 Asia/Jerusalem — adjust as needed), empty payload. The sweep itself reads every active rule for that job's user, so adding a new rule later needs no further Schedule changes.

Why hard-delete is correct here

Verified against StudentPercentReport's aggregation: attendance percentage is computed only over lesson+student combinations that have an att_report row. Deleting the row is exactly what removes that lesson from a student's percentage denominator — the intended fix for students (e.g. newly married) who stop attending a lesson mid-year but shouldn't be penalized with a low percentage for it.

Testing

All verified on a fresh clone of this exact branch (fresh yarn install, real submodule checkout):

  • tsc --noEmit: 0 errors.
  • yarn test: 977/977 passing (10 new: date-window computation, handler scoping/deletion/skip-on-unresolved-year, doAction routing).
  • yarn test:e2e: 4/4 passing.
  • Migration DDL validated directly against a real MySQL instance (MariaDB): table created, defaults (weeks_back=2, active=1) apply correctly, insert succeeds.

🤖 Generated with Claude Code

https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s


Generated by Claude Code

Self-service, recurring attendance cleanup requested by a paying
client: for a chosen lesson and a klass/track to preserve, every
student NOT in that track has att_reports deleted for that lesson
across the last N occurrences of a chosen weekday, run automatically
every Motzei Shabbat.

- AttendanceCleanupRule entity: friendly CRUD (lesson/klass reference
  dropdowns, day-of-week select, weeks-back, active). Stores
  lessonId/klassId as Lesson.key/Klass.key (stable across years), not
  a fixed referenceId, so a rule doesn't go stale when a new school
  year's Lesson/Klass rows get created with fresh ids.
- attendance-cleanup-sweep job handler: re-resolves lessonId/klassId
  against each target date's own academic year at run time, then
  deletes att_reports for that lesson+date where klass != preserved
  klass. Skips (rather than over-deletes) a date it can't resolve.
- "Run now" bulk button on the rule list enqueues the same handler
  scoped to the selected rule ids, via the job queue infra
  (buzz-code/nra-server#29..38) already merged into this app (#141).
- One weekly Schedule row (jobType: attendance-cleanup-sweep, empty
  payload) is a one-time manual setup — the sweep itself reads every
  active rule for that job's user, so new rules need no further setup.
- Migration for attendance_cleanup_rules (validated against real
  MySQL).
- Unit tests: date-window computation, handler (scoping, deletion,
  skip-on-unresolved), doAction routing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s
Copilot AI lite review requested due to automatic review settings August 19, 2026 20:10

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.

Pull request overview

Add recurring attendance cleanup feature: admin CRUD rules + weekly job handler to delete attendance for students not in preserved track, across last N weekday occurrences. Fits job queue/scheduler infra in app.

Changes:

  • Add attendance_cleanup_rules table + AttendanceCleanupRule entity
  • Add attendance-cleanup-sweep job handler + unit tests; wire handler into app module
  • Add React-Admin resource screen + translations + roadmap entry

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
server/src/migrations/1787200000000-addAttendanceCleanupRulesTable.ts Create attendance_cleanup_rules schema for cleanup rules
server/src/db/entities/AttendanceCleanupRule.entity.ts Define rule entity, hooks, validation, relations
server/src/entity-modules/attendance-cleanup-rule.config.ts Register CRUD module + runNow doAction enqueuing sweep job
server/src/entity-modules/tests/attendance-cleanup-rule.config.spec.ts Test runNow action enqueue behavior
server/src/jobs/attendance-cleanup-sweep.handler.ts Implement weekly sweep: resolve lesson/klass per year, delete att_reports
server/src/jobs/tests/attendance-cleanup-sweep.handler.spec.ts Test date window logic + handler scoping/deletion/skip behavior
server/src/app.module.ts Register sweep handler in jobHandlers
server/src/entities.module.ts Register AttendanceCleanupRule base entity module
client/src/entities/attendance-cleanup-rule.jsx Add admin CRUD UI + bulk “run now” action
client/src/domainTranslations.js Add Hebrew resource + field/action translations
client/src/App.jsx Add admin-only Resource menu entry + icon
client/src/roadmapFeatures.js Mark feature as done in roadmap
Files not reviewed (1)
  • client/src/roadmapFeatures.js: Generated file

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

helperText="הכיתה/מסלול שישמר - הנוכחות תימחק לכל שאר הכיתות"
/>
<SelectInput source="dayOfWeek" choices={dayOfWeekChoices} validate={required()} />
<NumberInput source="weeksBack" defaultValue={2} validate={required()} helperText="כמה שבועות אחורה לנקות בכל הרצה" />
Comment on lines +43 to +67
async fillFields() {
let dataSource: DataSource;
try {
dataSource = await getDataSource([Lesson, Klass, User]);

this.lessonReferenceId = await findOneAndAssignReferenceId(
dataSource,
Lesson,
{ key: this.lessonId },
this.userId,
this.lessonReferenceId,
this.lessonId,
);
this.klassReferenceId = await findOneAndAssignReferenceId(
dataSource,
Klass,
{ key: this.klassId },
this.userId,
this.klassReferenceId,
this.klassId,
);
} finally {
dataSource?.destroy();
}
}
claude added 3 commits August 20, 2026 08:28
Three review fixes on the attendance-cleanup-sweep feature:

- Correctness: exclusion was checking att_reports.klassReferenceId
  (the report/session's own klass, the same for every row in it) instead
  of actual student-klass membership. Now looks up student_klasses for
  the preserved klass and excludes by studentReferenceId membership.
  Also flipped AttendanceCleanupRule.fillFields from key->id resolution
  (which the form never triggers, since it submits the picked
  referenceId directly) to id->key: the picked lessonReferenceId/
  klassReferenceId now correctly resolves down to the stable
  Lesson.key/Klass.key the sweep needs for cross-year lookups. Without
  this fix lessonId/klassId would have stayed null forever.
- Timezone: getTargetDates defaulted to `new Date()` in the server
  process's own timezone (commonly UTC in prod), which could misdate
  every target date by a day. Added getIsraelToday() (Intl.DateTimeFormat
  pinned to Asia/Jerusalem) as the default anchor instead.
- Client weeksBack input had no min/max, so a value outside the
  server's Min(1)/Max(52) validation would round-trip as a 400. Added
  matching min/max/step plus minValue/maxValue validators.
- Bump client/shared for buzz-code/nra-client#42: the shared Job
  monitor didn't show `result` at all, so there was no way to see
  a completed sweep's summary (e.g. "נמחקו 14 רשומות נוכחות") without
  querying the API directly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s
real-data-source.ts globs every .ts under src/db/** as an entity
module (join(serverRoot, 'src/db/**/*.{js,ts}')) — no other entity in
this repo has a colocated spec, so this was never exercised before.
With the spec under src/db/entities/__tests__/, TypeORM's real
DataSource.initialize() (used by shared/utils/yemot/testing) required
the spec file as an entity, executing its top-level describe/it calls
outside Jest's own run and breaking 4 unrelated suites with "Tests
cannot be nested".

Moved to src/jobs/__tests__/, alongside the rest of this feature's
tests, which isn't part of that glob.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s
Bump server/shared for buzz-code/nra-server#39 and replace
AttendanceCleanupRule's inline id->key lookups with the new shared
helper, mirroring the existing key->id convention used everywhere
else in this codebase.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s
@buzz-code
buzz-code merged commit 408cc83 into main Aug 20, 2026
4 checks passed
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.

3 participants