mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
fix: recover managed sharing boundaries
This commit is contained in:
1 parent
01562b4aef
commit
d86358f3e4
96 files changed
+3578
-397
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
import { apiHandler, requireAuth, requireRecordAuth } from "@/lib/api-handler";
|
||||
import { apiHandler, requireRecordAuth } from "@/lib/api-handler";
|
||||
import {
|
||||
apiError,
|
||||
apiSuccess,
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
sanitiseZodIssues,
|
||||
} from "@/lib/api-response";
|
||||
import { auditLog } from "@/lib/auth/audit";
|
||||
import { overwriteDetails } from "@/lib/sharing/audit-details";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { invalidateUserHealthScore } from "@/lib/cache/invalidate";
|
||||
import {
|
||||
@@ -90,7 +91,10 @@ export const GET = apiHandler(
|
||||
|
||||
export const PUT = apiHandler(
|
||||
async (request: NextRequest, { params }: RouteParams) => {
|
||||
const { user } = await requireAuth();
|
||||
// v1.37.0 — MANAGE. Correcting a transcribed lab result is the ordinary
|
||||
// reason somebody manages a record; the previous numbers ride the audit
|
||||
// row below.
|
||||
const { user } = await requireRecordAuth("manage", "labs");
|
||||
const { id } = await params;
|
||||
|
||||
const existing = await prisma.labResult.findFirst({
|
||||
@@ -116,17 +120,16 @@ export const PUT = apiHandler(
|
||||
const auditIssues = sanitiseZodIssues(parsed.error.issues, {
|
||||
stripValuesFromMessage: true,
|
||||
});
|
||||
prisma.auditLog
|
||||
.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
action: "labs.update.validation-failed",
|
||||
details: JSON.stringify({ issues: auditIssues, labResultId: id }),
|
||||
},
|
||||
})
|
||||
.catch(() => {
|
||||
/* swallow — the 422 response is the contract */
|
||||
});
|
||||
// v1.37.0 — through `auditLog()` rather than a bare `prisma.auditLog
|
||||
// .create`, because that helper is the only writer that stamps
|
||||
// `actorUserId`. Filed under the resolved record either way; without the
|
||||
// stamp a manager's malformed payload would read as the owner's own.
|
||||
void auditLog("labs.update.validation-failed", {
|
||||
userId: user.id,
|
||||
details: { issues: auditIssues, labResultId: id },
|
||||
}).catch(() => {
|
||||
/* swallow — the 422 response is the contract */
|
||||
});
|
||||
return returnAllZodIssues(parsed.error, 422, {
|
||||
errorCode: "labs.update.invalid",
|
||||
});
|
||||
@@ -223,7 +226,34 @@ export const PUT = apiHandler(
|
||||
await auditLog("labResult.update", {
|
||||
userId: user.id,
|
||||
ipAddress: getClientIp(request),
|
||||
details: { labResultId: id },
|
||||
// C4 — the replaced result. The note is named and never quoted: it is
|
||||
// encrypted on the row.
|
||||
details: {
|
||||
labResultId: id,
|
||||
...overwriteDetails({
|
||||
before: {
|
||||
analyte: existing.analyte,
|
||||
value: existing.value,
|
||||
valueText: existing.valueText,
|
||||
unit: existing.unit,
|
||||
panel: existing.panel,
|
||||
referenceLow: existing.referenceLow,
|
||||
referenceHigh: existing.referenceHigh,
|
||||
takenAt: existing.takenAt,
|
||||
},
|
||||
after: {
|
||||
analyte: updated.analyte,
|
||||
value: updated.value,
|
||||
valueText: updated.valueText,
|
||||
unit: updated.unit,
|
||||
panel: updated.panel,
|
||||
referenceLow: updated.referenceLow,
|
||||
referenceHigh: updated.referenceHigh,
|
||||
takenAt: updated.takenAt,
|
||||
},
|
||||
redacted: d.note !== undefined ? ["note"] : [],
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
annotate({ action: { name: "labs.update" }, meta: { labResultId: id } });
|
||||
@@ -236,7 +266,9 @@ export const PUT = apiHandler(
|
||||
|
||||
export const DELETE = apiHandler(
|
||||
async (request: NextRequest, { params }: RouteParams) => {
|
||||
const { user } = await requireAuth();
|
||||
// v1.37.0 — MANAGE. Soft-delete with `labs/restore` beside it, so the
|
||||
// reading is reconstructable from the row itself.
|
||||
const { user } = await requireRecordAuth("manage", "labs");
|
||||
const { id } = await params;
|
||||
|
||||
const existing = await prisma.labResult.findFirst({
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { apiHandler, requireRecordAuth } from "@/lib/api-handler";
|
||||
import { auditLog } from "@/lib/auth/audit";
|
||||
import {
|
||||
apiError,
|
||||
@@ -39,10 +39,17 @@ const restoreSchema = z.object({
|
||||
export const POST = apiHandler(withIdempotency<[NextRequest]>(postRestore));
|
||||
|
||||
async function postRestore(request: NextRequest): Promise<Response> {
|
||||
const { user } = await requireAuth();
|
||||
// v1.37.0 — MANAGE. The undo beside a delete the level already admits; the
|
||||
// `where` only ever clears a tombstone the record already carried, so a
|
||||
// foreign or live id is a no-op.
|
||||
const { user, actor } = await requireRecordAuth("manage", "labs");
|
||||
|
||||
// v1.37.0 — C1: the bucket keys on the ACTOR, the frozen precedent from
|
||||
// `medications/compliance`. A manager burns their own allowance rather than
|
||||
// locking the owner out of their own record, and cannot collect a fresh one
|
||||
// by switching records.
|
||||
const rl = await checkRateLimit(
|
||||
`labs:restore:${user.id}`,
|
||||
`labs:restore:${actor.id}`,
|
||||
RATE_LIMIT_MAX,
|
||||
RATE_LIMIT_WINDOW_MS,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user