rebreak-monorepo/backend/server/api/devices/protection-state.post.ts
chahinebrini a7ac5545ae
Some checks failed
Deploy Staging / Build backend (Nitro) (push) Has been cancelled
Build ReBreak Magic Windows / NSIS Installer (x64) (push) Waiting to run
ci/woodpecker/push/woodpecker Pipeline was successful
Deploy Staging / Deploy zu Hetzner (push) Has been cancelled
fix(magic): entferne reportDeviceProtectionState aus Lock-Profil-Flow
- Status kommt jetzt über /api/protection/event vom iOS Plugin.
- /api/devices/protection-state gibt jetzt { success: true, data: null }
  zurück, damit der Rust-Client nicht failed.
2026-06-18 09:22:41 +02:00

93 lines
1.8 KiB
TypeScript

import { requireUser } from "../../utils/auth";
import {
PROTECTION_TYPES,
upsertDeviceProtectionState,
type ProtectionType,
} from "../../db/device-protection";
/**
* POST /api/devices/protection-state
*
* Body: {
* deviceId: string,
* platform: string,
* protectionType: 'nefilter' | 'vpn' | 'dns',
* active: boolean,
* reason?: string,
* source?: string
* }
*
* Reports the per-device protection state from a client.
*/
export default defineEventHandler(async (event) => {
const user = await requireUser(event);
const body = await readBody(event);
const {
deviceId,
platform,
protectionType,
active,
reason,
source,
} = body as {
deviceId?: string;
platform?: string;
protectionType?: string;
active?: boolean;
reason?: string;
source?: string;
};
if (!deviceId) {
throw createError({
statusCode: 400,
data: { error: "device_id_required" },
});
}
if (!platform) {
throw createError({
statusCode: 400,
data: { error: "platform_required" },
});
}
if (!protectionType) {
throw createError({
statusCode: 400,
data: { error: "protection_type_required" },
});
}
if (!PROTECTION_TYPES.includes(protectionType as ProtectionType)) {
throw createError({
statusCode: 400,
data: {
error: "invalid_protection_type",
validTypes: PROTECTION_TYPES,
},
});
}
if (typeof active !== "boolean") {
throw createError({
statusCode: 400,
data: { error: "active_boolean_required" },
});
}
await upsertDeviceProtectionState(
user.id,
deviceId,
platform,
protectionType as ProtectionType,
active,
undefined,
reason ?? null,
source ?? "app",
);
return { success: true, data: null };
});