44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
export const dynamic = "force-dynamic";
|
|
|
|
import { z } from "zod";
|
|
import { requireAdmin } from "@/lib/auth/session";
|
|
import { fail, handleAuthError, ok } from "@/lib/api";
|
|
import { readJsonBody, validateMutationRequestOrigin } from "@/lib/security/request";
|
|
import { testCaldavConnection } from "@/lib/services/caldav";
|
|
|
|
const testConnectionSchema = z.object({
|
|
url: z.string().trim().url("Bitte eine gültige CalDAV-URL eingeben"),
|
|
username: z.string().trim().min(1, "Benutzername ist erforderlich").max(160),
|
|
password: z.string().min(1, "Passwort ist erforderlich").max(2000)
|
|
});
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const originError = validateMutationRequestOrigin(req);
|
|
if (originError) return originError;
|
|
|
|
await requireAdmin();
|
|
const bodyResult = await readJsonBody(req, { maxBytes: 16 * 1024 });
|
|
if (!bodyResult.ok) return bodyResult.response;
|
|
|
|
const parsed = testConnectionSchema.safeParse(bodyResult.data);
|
|
if (!parsed.success) {
|
|
return fail("Ungültige Verbindungsdaten", 400, parsed.error.flatten());
|
|
}
|
|
|
|
const result = await testCaldavConnection(parsed.data);
|
|
return ok({
|
|
message: `${result.calendarCount} Kalender gefunden`,
|
|
...result
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
const authResponse = handleAuthError(error);
|
|
if (authResponse.status !== 500) return authResponse;
|
|
return fail(error.message || "CalDAV-Verbindung fehlgeschlagen", 502);
|
|
}
|
|
|
|
return fail("CalDAV-Verbindung fehlgeschlagen", 502);
|
|
}
|
|
}
|