mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
feat(cycle): name the start a period start folds in and the starts a removal gives back
This commit is contained in:
1 parent
74fa4e10b6
commit
b562587c45
18 files changed
+481
-49
No files matched your search
@@ -2,6 +2,9 @@
|
||||
* `DELETE /api/cycle/cycles/{id}` — soft-delete a cycle
|
||||
* (ios-contract §2.F): set `deletedAt` + bump `syncVersion`, emit a
|
||||
* tombstone on the next sync page. 204. Idempotent. Owner-scoped + gated.
|
||||
* With `Prefer: return=representation` (v1.43, RFC 7240) the answer is 200
|
||||
* with what the delete changed — the removed cycle and the starts it gave
|
||||
* back — so a client can say "the start on 17 Sep is back".
|
||||
*
|
||||
* The delete also hands the span back to the preceding cycle. Opening a cycle
|
||||
* closes the one before it, so removing it without re-deriving that boundary
|
||||
@@ -18,6 +21,7 @@ import { annotate } from "@/lib/logging/context";
|
||||
import { auditLog } from "@/lib/auth/audit";
|
||||
import { overwriteDetails } from "@/lib/sharing/audit-details";
|
||||
import { apiError, getClientIp } from "@/lib/api-response";
|
||||
import { cycleDeleteOutcome } from "@/lib/cycle/delete-outcome";
|
||||
import { requireCycleEnabled } from "@/lib/cycle/gate";
|
||||
import { removeCycleStartedOn } from "@/lib/cycle/cycle-boundaries";
|
||||
|
||||
@@ -44,7 +48,7 @@ export const DELETE = apiHandler(
|
||||
}
|
||||
// Already a tombstone: the delete is done, and nothing is left to move.
|
||||
if (existing.deletedAt !== null) {
|
||||
return new Response(null, { status: 204 });
|
||||
return cycleDeleteOutcome(request, null, []);
|
||||
}
|
||||
|
||||
// Tombstone + re-anchor as one unit: a neighbour re-derived against a
|
||||
@@ -99,6 +103,10 @@ export const DELETE = apiHandler(
|
||||
meta: { reanchored_prior: reanchored !== null },
|
||||
});
|
||||
|
||||
return new Response(null, { status: 204 });
|
||||
return cycleDeleteOutcome(
|
||||
request,
|
||||
removed?.cycleId ?? null,
|
||||
removed?.restored ?? [],
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -3,7 +3,8 @@
|
||||
* (ios-contract §2.A).
|
||||
* `DELETE /api/cycle/day-logs/{id}` — soft-delete (ios-contract §2.F):
|
||||
* set `deletedAt` + bump `syncVersion`, emit a tombstone on the next
|
||||
* sync page. 204. Idempotent.
|
||||
* sync page. 204. Idempotent. With `Prefer: return=representation` 200
|
||||
* with the removed cycle and the starts given back (v1.43).
|
||||
*
|
||||
* Both are gated (`cycle.disabled` 403) and owner-scoped (a row owned by
|
||||
* another user 404s).
|
||||
@@ -34,6 +35,7 @@ import { getOrCreateCycleProfile } from "@/lib/cycle/profile";
|
||||
import { replaceSymptomLinks } from "@/lib/cycle/day-log-write";
|
||||
import { cycleDayLogPatchSchema } from "@/lib/validations/cycle";
|
||||
import { toCycleDayLogDTO, dayLogSymptomInclude } from "@/lib/cycle/dto";
|
||||
import { cycleDeleteOutcome } from "@/lib/cycle/delete-outcome";
|
||||
|
||||
type RouteParams = { params: Promise<{ id: string }> };
|
||||
|
||||
@@ -176,11 +178,13 @@ export const PATCH = apiHandler(
|
||||
row,
|
||||
);
|
||||
let removedCycleId: string | null = null;
|
||||
let restored: Array<{ cycleId: string; startDate: string }> = [];
|
||||
if (!openedCycleId && !stillBleeds(row)) {
|
||||
const removed = await prisma.$transaction((db) =>
|
||||
removeCycleStartedOn(db, user.id, existing.date),
|
||||
);
|
||||
removedCycleId = removed?.cycleId ?? null;
|
||||
restored = removed?.restored ?? [];
|
||||
}
|
||||
|
||||
await auditLog("cycle.day-log.update", {
|
||||
@@ -221,7 +225,9 @@ export const PATCH = apiHandler(
|
||||
},
|
||||
});
|
||||
|
||||
return apiSuccess(toCycleDayLogDTO(row));
|
||||
// v1.43 (ios #116 item 9) — the starts this edit gave back by clearing
|
||||
// the flow on a start day; empty when it removed none.
|
||||
return apiSuccess({ ...toCycleDayLogDTO(row), restored });
|
||||
},
|
||||
);
|
||||
|
||||
@@ -303,7 +309,11 @@ export const DELETE = apiHandler(
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(null, { status: 204 });
|
||||
return cycleDeleteOutcome(
|
||||
request,
|
||||
removed?.cycleId ?? null,
|
||||
removed?.restored ?? [],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
* and writes the boundary `CycleDayLog(flow=MEDIUM)`. `action:"end"`
|
||||
* stamps the current cycle's `periodEndDate` and writes the boundary
|
||||
* day-log. Convenience over §2.A; returns
|
||||
* `{ cycle: MenstrualCycleDTO, dayLog: CycleDayLogDTO }`.
|
||||
* `{ cycle: MenstrualCycleDTO, dayLog: CycleDayLogDTO, absorbed }`, where
|
||||
* `absorbed` names a later start this one folded in (v1.43, null otherwise).
|
||||
*/
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
@@ -221,12 +222,22 @@ async function postPeriod(request: NextRequest): Promise<Response> {
|
||||
return apiSuccess({
|
||||
cycle: toMenstrualCycleDTO(cycleRow),
|
||||
dayLog: toCycleDayLogDTO(dayLogRow),
|
||||
// v1.43 (ios #116 item 9) — a later start inside the same period that
|
||||
// this start folded in, so the client can say which date stopped being a
|
||||
// start. Null when nothing was folded; present on every `start`.
|
||||
absorbed:
|
||||
txResult.moved.absorbedCycleId && txResult.moved.absorbedStartDate
|
||||
? {
|
||||
cycleId: txResult.moved.absorbedCycleId,
|
||||
startDate: txResult.moved.absorbedStartDate,
|
||||
}
|
||||
: null,
|
||||
});
|
||||
}
|
||||
|
||||
/** `end` with no preceding cycle is a no-op the client should not hit. */
|
||||
function apiSuccessNoCycle(): Response {
|
||||
return apiSuccess({ cycle: null, dayLog: null });
|
||||
return apiSuccess({ cycle: null, dayLog: null, absorbed: null });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user