Morning reminders for timesheets waiting for approval

Node · Node · intermediate · modification

Managers keep forgetting to approve timesheets, so the worker now nudges them every weekday morning. `src/jobs/approvalReminders.js` groups the `submitted` timesheets by manager, sends each manager one email with the count and a link to the approvals page, and records the run (sent and failed counts) in `JobRun` for the ops dashboard. `src/jobs/index.js` schedules it for 08:00 on weekdays. Adds a job test in `test/approvalReminders.test.js`.

Express 4 API and a separate worker process (CommonJS, Node 22) with Prisma 5.22 on PostgreSQL 16. Existing, unchanged: `src/db.js` exports the shared `prisma` client; `src/logger.js` is the app's pino logger (`logger.info(obj, msg)`, `logger.error(obj, msg)`, an `err` field is serialised with its stack); `src/config.js` exports `appUrl`; `src/mailer.js` exports `sendEmail({ to, subject, text })`, which resolves to `{ id }` once the provider accepts the email and rejects with an `Error` when the provider refuses the address (bounced or suppressed addresses are normal, a few a week) or the call fails (10 s timeout); `src/jobs/scheduler.js` exports `schedule(name, cron, fn)`, runs cron expressions in the company time zone (Europe/Berlin), runs the worker as a single replica, never starts a job while its previous run is still going, and logs a run that throws. Models: `User { id Int @id @default(autoincrement()), email String @unique, name String }`; `Timesheet { id Int @id @default(autoincrement()), userId Int, managerId Int, status String, createdAt DateTime @default(now()) }` where `managerId` is a required foreign key to `User` (`onDelete: Restrict`) and `status` is one of `draft`, `submitted`, `approved`, `rejected`, with `@@index([status, managerId])`; `JobRun { id Int @id @default(autoincrement()), job String, startedAt DateTime, finishedAt DateTime, sent Int, failed Int }` (existing; `invoice-sync` writes one row per run too). There are about 400 managers; sends run one after another and a run may take a few minutes, which is fine. `test/helpers/db.js` (existing) exports `resetDb()`, which empties the three tables of the test database.

Requirements

Files touched

Review this PR

Node practice