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
- Every weekday at 08:00 the worker sends one reminder email to each manager who has at least one timesheet in status `submitted`, saying how many are waiting and linking to `<appUrl>/approvals`. A manager with nothing waiting gets no email.
- A failed send for one manager (the mail provider rejects the address, or the call times out) must not stop the others: that failure is logged and counted, and the job goes on to the next manager. Failed reminders are not retried within the run; the manager gets the next weekday's reminder as usual.
- When the run has gone through every manager, it writes one `JobRun` row with `job = 'approval-reminders'`, `startedAt`, `finishedAt`, `sent` (reminders the provider accepted) and `failed` (reminders whose send failed), and returns `{ sent, failed }`. The ops dashboard reads these rows.
- If the waiting timesheets cannot be read (a database error), the error propagates to the scheduler, which logs it, and no `JobRun` row is written for that run.
Files touched
- src/jobs/approvalReminders.js
- src/jobs/index.js
- test/approvalReminders.test.js