swadhinbiswas Add digest delivery retries and observability metrics · 2/19/2026
4b685b4
32 lines · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import type { APIRoute } from "astro";
import { z } from "zod";
import { withErrorHandler } from "@/lib/errors";
import { badRequest, success, unauthorized } from "@/lib/api";
import { runUserDigest } from "@/lib/chat-notifications";

const testDigestSchema = z.object({
  dryRun: z.boolean().optional(),
  period: z.enum(["daily", "weekly"]).optional(),
  maxRetries: z.number().int().min(0).max(5).optional(),
});

export const POST: APIRoute = withErrorHandler(async ({ locals, request }) => {
  const user = locals.user;
  if (!user) return unauthorized();

  const body = await request.json().catch(() => null);
  const parsed = testDigestSchema.safeParse(body || {});
  if (!parsed.success) {
    return badRequest(parsed.error.issues[0]?.message || "Invalid digest test payload");
  }

  const result = await runUserDigest({
    userId: user.id,
    dryRun: parsed.data.dryRun ?? true,
    period: parsed.data.period,
    maxRetries: parsed.data.maxRetries,
  });

  return success(result);
});