mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
HealthLog — personal health-tracking PWA with weight, blood pressure, pulse, mood tracking, medication compliance, Withings integration, doctor report PDF export, and Dracula-themed UI.
93 lines
2.3 KiB
TypeScript
93 lines
2.3 KiB
TypeScript
import { prisma } from "@/lib/db";
|
|
import { apiSuccess, apiError, safeJson } from "@/lib/api-response";
|
|
import { ntfySettingsSchema } from "@/lib/validations/notifications";
|
|
import { encrypt, decrypt } from "@/lib/crypto";
|
|
import { NextRequest } from "next/server";
|
|
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
|
import { annotate } from "@/lib/logging/context";
|
|
|
|
/**
|
|
* GET: Return current ntfy config (without auth token).
|
|
* PUT: Update ntfy config.
|
|
*/
|
|
export const GET = apiHandler(async () => {
|
|
const { user } = await requireAuth();
|
|
|
|
const channel = await prisma.notificationChannel.findUnique({
|
|
where: {
|
|
userId_type: { userId: user.id, type: "NTFY" },
|
|
},
|
|
});
|
|
|
|
if (!channel) {
|
|
return apiSuccess({
|
|
enabled: false,
|
|
serverUrl: "https://ntfy.sh",
|
|
topic: "",
|
|
hasAuthToken: false,
|
|
});
|
|
}
|
|
|
|
const config = JSON.parse(decrypt(channel.config)) as {
|
|
serverUrl: string;
|
|
topic: string;
|
|
authToken?: string;
|
|
};
|
|
|
|
annotate({ action: { name: "settings.ntfy.get" } });
|
|
|
|
return apiSuccess({
|
|
enabled: channel.enabled,
|
|
serverUrl: config.serverUrl,
|
|
topic: config.topic,
|
|
hasAuthToken: !!config.authToken,
|
|
});
|
|
});
|
|
|
|
export const PUT = apiHandler(async (request: NextRequest) => {
|
|
const { user } = await requireAuth();
|
|
|
|
const { data: body, error: jsonError } = await safeJson(request);
|
|
|
|
if (jsonError) return jsonError;
|
|
const parsed = ntfySettingsSchema.safeParse(body);
|
|
if (!parsed.success) return apiError("Ungueltige Daten", 422);
|
|
|
|
const { serverUrl, topic, authToken, enabled } = parsed.data;
|
|
|
|
if (enabled && (!serverUrl || !topic)) {
|
|
return apiError(
|
|
"Server-URL und Topic sind erforderlich, wenn ntfy aktiviert ist",
|
|
422,
|
|
);
|
|
}
|
|
|
|
const config = JSON.stringify({
|
|
serverUrl: serverUrl || "https://ntfy.sh",
|
|
topic: topic || "",
|
|
...(authToken ? { authToken } : {}),
|
|
});
|
|
|
|
const encryptedConfig = encrypt(config);
|
|
|
|
await prisma.notificationChannel.upsert({
|
|
where: {
|
|
userId_type: { userId: user.id, type: "NTFY" },
|
|
},
|
|
create: {
|
|
userId: user.id,
|
|
type: "NTFY",
|
|
enabled,
|
|
config: encryptedConfig,
|
|
},
|
|
update: {
|
|
enabled,
|
|
config: encryptedConfig,
|
|
},
|
|
});
|
|
|
|
annotate({ action: { name: "settings.ntfy.update" }, meta: { enabled } });
|
|
|
|
return apiSuccess({ saved: true });
|
|
});
|