swadhinbiswas Fix: Add date validation in analysis.ts to prevent RangeError · 1/20/2026
a7662cd
41 lines · 1.2 KB
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
33
34
35
36
37
38
39
40
41
/**
 * Mark all notifications as read
 */
import { type APIRoute } from 'astro';
import { eq, and } from 'drizzle-orm';
import { getDatabase, schema } from '@/db';
import { notifications } from '@/db/schema';
import { getUserFromRequest } from '@/lib/auth';
import { withErrorHandler, Errors } from "@/lib/errors";
import { logger } from "@/lib/logger";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";

export const POST: APIRoute = withErrorHandler(async ({ request, redirect }) => {
    const tokenPayload = await getUserFromRequest(request);
    if (!tokenPayload) {
        throw Errors.unauthorized();
    }

    const db = getDatabase() as NodePgDatabase<typeof schema>;
    const timestamp = new Date();

    await db
        .update(notifications)
        .set({
            isRead: true,
            readAt: timestamp,
            updatedAt: timestamp
        })
        .where(
            and(
                eq(notifications.userId, tokenPayload.userId),
                eq(notifications.isRead, false)
            )
        );

    logger.info({ userId: tokenPayload.userId }, "Marked all notifications as read");

    // Redirect back to notifications page
    return redirect('/notifications', 302);
});