52 lines
1.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* POST /api/magic/devices/:deviceId/cooldown
*
* Sets a temporary cooldown on a Magic device binding until the given
* duration (in minutes, 11440) elapses.
*/
export default defineEventHandler(async (event) => {
const user = await requireUser(event);
const deviceId = getRouterParam(event, "deviceId");
if (!deviceId) {
throw createError({ statusCode: 400, message: "deviceId required" });
}
const body = await readBody(event);
const raw = body?.durationMinutes;
if (
typeof raw !== "number" ||
!Number.isFinite(raw) ||
!Number.isInteger(raw) ||
raw < 1 ||
raw > 1440
) {
throw createError({
statusCode: 400,
message: "durationMinutes must be an integer between 1 and 1440",
});
}
const durationMinutes = raw;
const cooldownUntil = new Date(Date.now() + durationMinutes * 60 * 1000);
const db = usePrisma();
const device = await db.userDevice.findUnique({
where: { userId_deviceId: { userId: user.id, deviceId } },
select: { id: true, magicEnrolledAt: true, magicRevokedAt: true },
});
if (!device || !device.magicEnrolledAt || device.magicRevokedAt) {
throw createError({ statusCode: 404, message: "Magic-Binding nicht gefunden" });
}
await db.userDevice.update({
where: { id: device.id },
data: { magicCooldownUntil: cooldownUntil },
});
return {
success: true,
data: { cooldownUntil: cooldownUntil.toISOString() },
};
});