Email Digests (Daily/Weekly) on Website

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

Developing an Email Digest System (Daily and Weekly)

An email digest is an aggregated message with a curated selection of events over a period: new comments, project activity, new articles, task changes. Unlike real-time notifications, a digest allows users to manage the frequency of notifications they receive.

Architecture

Events → Event Store (DB) → Digest Scheduler (Cron) → Digest Builder → ESP → User
                                     ↓
                             User Preferences
                         (daily/weekly, timezone)

Accumulating Events for Digest

// Digest events table
// CREATE TABLE digest_events (
//   id UUID PRIMARY KEY,
//   user_id UUID NOT NULL,
//   type VARCHAR(100) NOT NULL,
//   payload JSONB NOT NULL,
//   created_at TIMESTAMPTZ DEFAULT now(),
//   included_in_digest_at TIMESTAMPTZ
// );

async function trackDigestEvent(
  userId: string,
  type: string,
  payload: Record<string, unknown>
) {
  await db.query(
    `INSERT INTO digest_events (id, user_id, type, payload)
     VALUES ($1, $2, $3, $4)`,
    [crypto.randomUUID(), userId, type, JSON.stringify(payload)]
  );
}

// Usage from different parts of the application
await trackDigestEvent(userId, 'new_comment', {
  postTitle: post.title,
  commenterName: commenter.name,
  commentPreview: comment.body.slice(0, 100),
  url: `https://app.example.com/posts/${post.id}#comment-${comment.id}`,
});

await trackDigestEvent(userId, 'task_assigned', {
  taskTitle: task.title,
  assignerName: assigner.name,
  dueDate: task.dueDate,
  url: `https://app.example.com/tasks/${task.id}`,
});

Digest Scheduler

import { CronJob } from 'cron';

// Daily digest — every day at 8:00 UTC
new CronJob('0 8 * * *', async () => {
  await sendDailyDigests();
}).start();

// Weekly — every Monday at 9:00 UTC
new CronJob('0 9 * * 1', async () => {
  await sendWeeklyDigests();
}).start();

async function sendDailyDigests() {
  // Get users with daily digest setting
  const users = await db.query<User[]>(`
    SELECT u.id, u.email, u.name, u.timezone, up.digest_time
    FROM users u
    JOIN user_preferences up ON u.id = up.user_id
    WHERE up.digest_frequency = 'daily'
      AND up.digest_enabled = true
  `);

  // Consider timezone — send at local morning time
  const usersToSend = users.filter(user => {
    const localHour = new Date().toLocaleString('en-US', {
      timeZone: user.timezone,
      hour: 'numeric',
      hour12: false,
    });
    return localHour === String(user.digest_time ?? 8);
  });

  await Promise.allSettled(
    usersToSend.map(user => sendUserDigest(user, 'daily'))
  );
}

Building and Sending Digest

async function sendUserDigest(user: User, period: 'daily' | 'weekly') {
  const since = period === 'daily'
    ? new Date(Date.now() - 24 * 60 * 60 * 1000)
    : new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);

  // Get events for the period
  const events = await db.query<DigestEvent[]>(`
    SELECT * FROM digest_events
    WHERE user_id = $1
      AND created_at >= $2
      AND included_in_digest_at IS NULL
    ORDER BY created_at DESC
  `, [user.id, since]);

  if (events.length === 0) return;  // don't send empty digest

  // Group by type
  const grouped = events.reduce((acc, event) => {
    acc[event.type] = (acc[event.type] ?? []).concat(event);
    return acc;
  }, {} as Record<string, DigestEvent[]>);

  // Render template
  const html = renderDigestTemplate({
    user,
    period,
    groups: grouped,
    totalCount: events.length,
    unsubscribeUrl: generateUnsubscribeUrl(user.id),
  });

  await sendEmail({
    to: user.email,
    subject: period === 'daily'
      ? `Today's digest — ${events.length} updates`
      : `Weekly digest — ${events.length} events`,
    html,
  });

  // Mark events as included in digest
  await db.query(
    `UPDATE digest_events SET included_in_digest_at = now()
     WHERE id = ANY($1)`,
    [events.map(e => e.id)]
  );
}

User Preferences

// API for managing digest preferences
app.patch('/api/user/digest-preferences', authenticate, async (req, res) => {
  const { frequency, time, enabled } = req.body;
  // frequency: 'none' | 'daily' | 'weekly'
  // time: 0-23 (hour for sending in UTC+local)

  await db.query(
    `INSERT INTO user_preferences (user_id, digest_frequency, digest_time, digest_enabled)
     VALUES ($1, $2, $3, $4)
     ON CONFLICT (user_id) DO UPDATE
     SET digest_frequency = $2, digest_time = $3, digest_enabled = $4`,
    [req.user.id, frequency, time, enabled]
  );

  res.json({ ok: true });
});

Timeline

A digest system with event accumulation, scheduler, timezone consideration, and user preferences takes 4–6 days to implement.