Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | 1x 1x 4x 4x 3x 3x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x | import { NextResponse } from "next/server";
import { requireAppToken, isAppAuthError } from "@/lib/app-auth";
import { ownPatientIdForSession } from "@/lib/overview/patient";
import { isSandboxUsername } from "@/lib/sandbox-account";
import {
MAX_WEARABLE_OBSERVATIONS,
parseWearableBundle,
storeWearableRows,
} from "@/lib/patient/app-wearables";
import { createRateLimiter } from "@/lib/rate-limit";
export const dynamic = "force-dynamic";
/** A sync after the switch is turned on, and one now and then after that. */
const limiter = createRateLimiter({
windowMs: 60_000,
perKey: 6,
inTotal: 600,
});
/**
* POST /api/patient/app/wearables
*
* The Klarbefund app sends the person's weekly device values from Apple
* Health into their own record (ADR-057): resting heart rate, heart rate
* variability, steps per day and body weight, one mean per week. The person
* turns this on in the app with its own switch, apart from the lab reports.
* Each sync replaces the previous one.
*
* App token and a connected device only (ADR-049), and only an account the
* app created: a demo persona's record is synthetic, and a real person's
* values must never land in one (as POST /api/patient/app/record).
*/
export async function POST(request: Request) {
const auth = await requireAppToken(request, { device: true });
if (isAppAuthError(auth)) return auth;
const { username } = auth.app;
if (!isSandboxUsername(username)) {
return NextResponse.json(
{
error: "Forbidden",
reason:
"values go only into a record the app created; a demo record is synthetic",
},
{ status: 403 },
);
}
Iif (!limiter.allow(username)) {
return NextResponse.json(
{ error: "Too many syncs, try again in a minute" },
{ status: 429 },
);
}
const patientId = ownPatientIdForSession({ preferredUsername: username });
Iif (!patientId) {
return NextResponse.json(
{ error: "No record is assigned to this login" },
{ status: 404 },
);
}
let parsed;
try {
parsed = parseWearableBundle(await request.json(), patientId);
} catch (err) {
return NextResponse.json(
{
error: "Bad request",
reason: err instanceof Error ? err.message : "not JSON",
},
{ status: 400 },
);
}
Iif (parsed.rows.length > MAX_WEARABLE_OBSERVATIONS) {
return NextResponse.json(
{
error: "Too large",
reason: `at most ${MAX_WEARABLE_OBSERVATIONS} weekly values per sync`,
},
{ status: 413 },
);
}
try {
const stored = await storeWearableRows({ patientId, rows: parsed.rows });
return NextResponse.json(
{ stored, skipped: parsed.skipped },
{ status: 201 },
);
} catch (err) {
console.error("POST /api/patient/app/wearables:", err);
return NextResponse.json({ error: "Neo4j unavailable" }, { status: 502 });
}
}
|