Urgent.News

What's breaking now, across thousands of outlets.

Tech

Isolating Multi‑Tenant Email Cron Jobs in a Next.js/Node Monorepo

Isolating Multi‑Tenant Email Cron Jobs in a Next.js/Node Monorepo TL;DR: I refactored the email.cron.ts worker so each organization processes its own notifications in isolation, added a comprehensive Jest test suite, and bumped coverage from 1.28 % to realistic levels. The change prevents cross‑tenant data leakage and makes the cron safe for production. The Problem Our SaaS platform runs a single…

Our SaaS platform runs a single Node cron that sends daily email notifications to all tenants. However, the worker pulls pending alerts from a shared email_queue table without scoping the query to a tenant ID. This caused duplicate key violations, where emails were sent to users of one organization while processing alerts from another organization, or worse, users receiving emails about contracts belonging to a different organization.

The existing test suite only tested the cron with a single organization, resulting in misleading coverage metrics only at 1.28%.

To fix this, I refactored the email.cron.ts worker into two phases: discovery and processing. The discovery phase fetches a distinct list of organization IDs that have pending emails. The processing phase then iterates over this list, explicitly passing the tenant ID to the existing email‑sending logic. I also added a guard inside sendEmailForRecord to ensure that the email's organization ID matches the tenant being processed.

If there's a mismatch, the job logs a warning and skips the record. Finally, I wrote a comprehensive Jest test suite that seeds two organizations with distinct email queues and verifies that each organization receives only its own emails.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Tres proyectos, tres formas distintas de romperme la cabeza

Publicado originalmente en mi blog . Soy estudiante de Ingeniería en Computación y desarrollador. Este año construí tres cosas muy distintas entre sí, y cada una me obligó a desaprender algo. Las dejo aquí porque los problemas interesantes no siempre fueron los que esperaba. 1.

  • First project taught hardware integration challenges beyond web development assumptions
  • Second project revealed privacy concerns as key decision in social app development
  • Third project discovered synchronization issues in offline-first Progressive Web App

More from Tuesday 15 September →