Files
kita-planer/src/app/alert/[token]/actions.ts
T

38 lines
1.4 KiB
TypeScript

"use server";
import { publicActionError } from "@/lib/action-error";
import { revalidatePath } from "next/cache";
import { respondToAlarm } from "@/lib/alarms/service";
import { deliverAlarmEmails } from "@/lib/alarms/email";
import { limitPublicAction } from "@/lib/rate-limit";
import { hashToken, isPublicToken } from "@/lib/tokens";
export async function respondAlertAction(token: string, approve: boolean) {
if (!isPublicToken(token) || typeof approve !== "boolean")
return { error: "Ungültiger Rückmeldelink." };
if (!(await limitPublicAction("alarm-response", hashToken(token), 12, 60)))
return { error: "Bitte kurz warten und erneut versuchen." };
try {
const result = await respondToAlarm(token, approve);
// The household response is already committed; transport failure must not undo it.
try {
await deliverAlarmEmails(result.emailIds);
} catch {
/* Persistent jobs will retry. */
}
revalidatePath("/alert/[token]", "page");
revalidatePath("/dashboard");
revalidatePath("/dashboard/notdienst/alarm");
return { success: true };
} catch (error) {
const message = publicActionError(error, "");
if (message) return { error: message };
return {
error:
"Die Rückmeldung konnte nicht gespeichert werden. Bitte den aktuellen Stand prüfen.",
};
}
}
export async function confirmAlertAction(token: string) {
return respondAlertAction(token, true);
}