mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 15:02:04 +00:00
Close the way round the delegated-intake refusal
v1.36.1 stopped a delegate changing a dose the owner had already recorded, and it stopped them on one route. The sibling `POST /api/medications/[id]/intake` reaches the same rows through the shared slot upsert, where the only existing protection is against a pending sync echo washing out a recorded dose. An explicit skip is not an echo: it fell through, flipped the outcome, and the inventory the take had consumed was refunded on the way. So the refusal I added was a door with the next one left open. The rule compares OUTCOMES rather than refusing every write onto an actioned slot, and the reason is the third case in the new contract. A double tap, an offline replay and a retry after a partial batch all re-post the same decision, and a caregiver hits all three; refusing those would make the feature unreliable exactly when somebody is standing in a kitchen with a pill box. Same decision applies, a different one is refused. The check rides into `applyCanonicalSlotWrite` rather than sitting at the call site, because that is where the existing row is already loaded: no extra query, and nothing between the read and the update for a concurrent write to slip through. A refused attempt files under the owner with the same action name the sibling route uses, so it reaches the activity panel the same way. Three integration cases, and each asserts the row rather than the status alone — a 403 that had already written the flip would read as protection while being the opposite. Watched fail: with the delegate flag forced off, the flip returns 201 while the replay and the owner's own correction stay green.
This commit is contained in:
1 parent
25e86b36b3
commit
24922b2787
3 files changed
+199
No files matched your search
@@ -52,6 +52,38 @@ async function postIntake(request: NextRequest, { params }: RouteParams) {
|
||||
// `user` is the record the dose belongs to; `actor` is whoever pressed the
|
||||
// button, and is used for nothing but the owner's notification below.
|
||||
const { user, actor, authMethod } = await requireRecordAuth("write");
|
||||
|
||||
// v1.36.1 follow-up — the sibling route `POST /api/medications/intake`
|
||||
// refuses a delegate changing a dose the owner already recorded. This route
|
||||
// reaches the same rows through the slot upsert, so without the same rule it
|
||||
// is simply the way round the other one: an explicit skip posted onto a
|
||||
// taken slot flips the outcome and refunds the inventory the take consumed.
|
||||
// The flag rides into `applyCanonicalSlotWrite`, which is where the existing
|
||||
// row is already loaded, so the check adds no query and cannot race.
|
||||
const isDelegated = actor.id !== user.id;
|
||||
|
||||
/**
|
||||
* The refusal itself, shared by both upsert call sites so the two cannot
|
||||
* drift apart. Files under the OWNER — `auditLog` stamps the actor from the
|
||||
* request's acting context — so a refused attempt reaches the activity panel
|
||||
* the same way the sibling route's does, under the same action name.
|
||||
*/
|
||||
function refuseOutcomeChange(): Response {
|
||||
annotate({
|
||||
action: { name: "medications.intake.update.refused" },
|
||||
meta: { reason: "resolved_event" },
|
||||
});
|
||||
void auditLog("medications.intake.update.refused", {
|
||||
userId: user.id,
|
||||
ipAddress: getClientIp(request),
|
||||
details: { medicationId: id, reason: "resolved_event" },
|
||||
});
|
||||
return apiError(
|
||||
"Changing a dose that is already recorded is not part of shared access",
|
||||
403,
|
||||
{ errorCode: "sharing.not_permitted" },
|
||||
);
|
||||
}
|
||||
// v1.32.8 (iOS #64) — write provenance derived from the transport, never
|
||||
// client-asserted: a Bearer/native call stores `API`, a cookie/browser call
|
||||
// stores `WEB`. The other `IntakeSource` values are producer-owned by OTHER
|
||||
@@ -334,6 +366,7 @@ async function postIntake(request: NextRequest, { params }: RouteParams) {
|
||||
isExplicitSkip,
|
||||
idempotencyKey: idempotencyKey ?? null,
|
||||
createSource: intakeSource,
|
||||
refuseOutcomeChange: isDelegated,
|
||||
// v1.8.5 — resolved + validated site (null unless a tracking-on
|
||||
// injection taken write supplied an allowed site).
|
||||
injectionSite: resolvedInjectionSite,
|
||||
@@ -342,6 +375,7 @@ async function postIntake(request: NextRequest, { params }: RouteParams) {
|
||||
// carried one).
|
||||
doseTaken: resolvedDoseTaken,
|
||||
});
|
||||
if (applied.outcomeChangeRefused) return refuseOutcomeChange();
|
||||
event = applied.row;
|
||||
consumedTransition = applied.consumedTransition;
|
||||
// Reset the snooze when a dose is actually recorded (not on a
|
||||
@@ -400,10 +434,12 @@ async function postIntake(request: NextRequest, { params }: RouteParams) {
|
||||
isExplicitSkip,
|
||||
idempotencyKey: idempotencyKey ?? null,
|
||||
createSource: intakeSource,
|
||||
refuseOutcomeChange: isDelegated,
|
||||
injectionSite: resolvedInjectionSite,
|
||||
attributionSource,
|
||||
doseTaken: resolvedDoseTaken,
|
||||
});
|
||||
if (applied.outcomeChangeRefused) return refuseOutcomeChange();
|
||||
event = applied.row;
|
||||
consumedTransition = applied.consumedTransition;
|
||||
if (!skipped && !applied.noDowngradeNoOp) {
|
||||
|
||||
Reference in new issue
Block a user