swadhinbiswas Fix: Add date validation in analysis.ts to prevent RangeError · 1/20/2026
a7662cd
81 lines · 2.1 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { getDatabase, schema } from "@/db";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { eq } from "drizzle-orm";
import type { APIRoute } from "astro";
import { withErrorHandler } from "@/lib/errors";
import { logger } from "@/lib/logger";
import { forbidden, success, badRequest } from "@/lib/api";

// GET /api/admin/config/general
export const GET: APIRoute = withErrorHandler(async ({ locals }) => {
    const user = locals.user;
    if (!user?.isAdmin) {
        return forbidden();
    }

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

    // Fetch configs
    const configs = await db.query.systemConfig.findMany({
        where: eq(schema.systemConfig.key, "general_config")
    });

    const configRow = configs[0];

    // Default
    let config = {
        siteName: "OpenCodeHub",
        siteDescription: "Open Source Git Hosting",
        allowSignups: true,
        smtpHost: "",
        smtpPort: 587,
        smtpUser: "",
        smtpFrom: "[email protected]"
        // Password not returned usually
    };

    if (configRow) {
        config = { ...config, ...JSON.parse(configRow.value) };
    }

    return success(config);
});

// POST /api/admin/config/general
export const POST: APIRoute = withErrorHandler(async ({ request, locals }) => {
    const user = locals.user;
    if (!user?.isAdmin) {
        return forbidden();
    }

    let config;
    try {
        config = await request.json();
    } catch (e) {
        return badRequest("Invalid JSON");
    }

    // Save to DB
    const db = getDatabase() as NodePgDatabase<typeof schema>;
    const value = JSON.stringify(config);

    await db.insert(schema.systemConfig)
        .values({
            key: "general_config",
            value,
            updatedById: user.id
        })
        .onConflictDoUpdate({
            target: schema.systemConfig.key,
            set: {
                value,
                updatedAt: new Date(),
                updatedById: user.id
            }
        });

    logger.info({ adminId: user.id }, "General config updated");

    return success({ success: true });
});