mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
fix(cycle): make removing a period start restore the neighbour it moved
Opening a cycle closes the one before it: the start write stamps the previous cycle's endDate and lengthDays, and a back-filled start takes its own end from its successor. Nothing undid any of that. Deleting the start left the previous cycle as a closed torso holding an end date from a cycle that no longer existed, and a record whose last cycle is closed has no open cycle to forecast from — the prediction went away and there was no way to bring it back from the app. Both removal paths now re-derive the boundary from the cycles that remain: a cycle ends the day before the next one begins, and with no next one it is open again. That is a re-derivation rather than an undo log, so it is correct for a history that came from an import or a restore, and running it twice changes nothing the second time. Deleting the day-log that opened a cycle now removes that cycle too. The one-tap start writes both rows and the log sheet's delete is the only way the web offers to take it back; leaving the cycle standing was how the broken state survived the undo. - soft-delete and re-anchor run in one transaction - the audit row names the neighbour's moved boundary, the only part of the write the tombstone cannot reconstruct - round trip through the real routes: photograph the cycle rows, log the start, remove it either way, compare field by field — including that the forecast comes back
This commit is contained in:
1 parent
c5a471f7fa
commit
6db2f6f9ae
4 files changed
+471
-10
No files matched your search
@@ -2,6 +2,12 @@
|
||||
* `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.
|
||||
*
|
||||
* 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
|
||||
* left the previous cycle standing as a closed torso with an end date taken
|
||||
* from a cycle that no longer exists — and a record whose last cycle is closed
|
||||
* has no open cycle to forecast from.
|
||||
*/
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
@@ -9,8 +15,10 @@ import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireRecordAuth } from "@/lib/api-handler";
|
||||
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 { requireCycleEnabled } from "@/lib/cycle/gate";
|
||||
import { reanchorAfterRemovedStart } from "@/lib/cycle/cycle-boundaries";
|
||||
|
||||
type RouteParams = { params: Promise<{ id: string }> };
|
||||
|
||||
@@ -28,21 +36,46 @@ export const DELETE = apiHandler(
|
||||
|
||||
const existing = await prisma.menstrualCycle.findUnique({
|
||||
where: { id },
|
||||
select: { id: true, userId: true },
|
||||
select: { id: true, userId: true, startDate: true },
|
||||
});
|
||||
if (!existing || existing.userId !== user.id) {
|
||||
return apiError("Cycle not found", 404);
|
||||
}
|
||||
|
||||
await prisma.menstrualCycle.update({
|
||||
where: { id },
|
||||
data: { deletedAt: new Date(), syncVersion: { increment: 1 } },
|
||||
// Tombstone + re-anchor as one unit: a neighbour re-derived against a
|
||||
// half-applied delete would read the row being removed as still live.
|
||||
const reanchored = await prisma.$transaction(async (db) => {
|
||||
await db.menstrualCycle.update({
|
||||
where: { id },
|
||||
data: { deletedAt: new Date(), syncVersion: { increment: 1 } },
|
||||
});
|
||||
return reanchorAfterRemovedStart(db, user.id, existing.startDate);
|
||||
});
|
||||
|
||||
await auditLog("cycle.cycle.delete", {
|
||||
userId: user.id,
|
||||
ipAddress: getClientIp(request),
|
||||
details: { cycleId: id },
|
||||
// The tombstone carries the deleted cycle's own columns, so the only
|
||||
// unrecoverable part of this write is what it moved on the NEIGHBOUR.
|
||||
details: {
|
||||
cycleId: id,
|
||||
startDate: existing.startDate,
|
||||
...(reanchored
|
||||
? {
|
||||
reanchoredCycleId: reanchored.cycleId,
|
||||
...overwriteDetails({
|
||||
before: {
|
||||
endDate: reanchored.endDateBefore,
|
||||
lengthDays: reanchored.lengthDaysBefore,
|
||||
},
|
||||
after: {
|
||||
endDate: reanchored.endDateAfter,
|
||||
lengthDays: reanchored.lengthDaysAfter,
|
||||
},
|
||||
}),
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
|
||||
annotate({
|
||||
@@ -51,6 +84,7 @@ export const DELETE = apiHandler(
|
||||
entity_type: "menstrual_cycle",
|
||||
entity_id: id,
|
||||
},
|
||||
meta: { reanchored_prior: reanchored !== null },
|
||||
});
|
||||
|
||||
return new Response(null, { status: 204 });
|
||||
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
safeJson,
|
||||
} from "@/lib/api-response";
|
||||
import { requireCycleEnabled } from "@/lib/cycle/gate";
|
||||
import { reanchorAfterRemovedStart } from "@/lib/cycle/cycle-boundaries";
|
||||
import { encrypt, decrypt } from "@/lib/crypto";
|
||||
import { getOrCreateCycleProfile } from "@/lib/cycle/profile";
|
||||
import { replaceSymptomLinks } from "@/lib/cycle/day-log-write";
|
||||
@@ -202,7 +203,7 @@ export const DELETE = apiHandler(
|
||||
|
||||
const existing = await prisma.cycleDayLog.findUnique({
|
||||
where: { id },
|
||||
select: { id: true, userId: true },
|
||||
select: { id: true, userId: true, date: true },
|
||||
});
|
||||
if (!existing || existing.userId !== user.id) {
|
||||
return apiError("Day-log not found", 404);
|
||||
@@ -211,15 +212,56 @@ export const DELETE = apiHandler(
|
||||
// Soft-delete: leave the row in place so the `/api/sync/changes`
|
||||
// delta feed surfaces it as a tombstone. A re-delete re-bumps
|
||||
// `syncVersion` harmlessly (idempotent).
|
||||
await prisma.cycleDayLog.update({
|
||||
where: { id },
|
||||
data: { deletedAt: new Date(), syncVersion: { increment: 1 } },
|
||||
//
|
||||
// A day that OPENS a cycle takes that cycle with it. The one-tap "started
|
||||
// my period" writes both rows, and this delete is the only way the web
|
||||
// offers to take it back — leaving the cycle behind meant the previous one
|
||||
// stayed closed against a start whose last trace had just been removed, and
|
||||
// the forecast never came back. The cycle boundary is then re-derived for
|
||||
// the preceding cycle, the same repair the cycle delete does.
|
||||
const removed = await prisma.$transaction(async (db) => {
|
||||
await db.cycleDayLog.update({
|
||||
where: { id },
|
||||
data: { deletedAt: new Date(), syncVersion: { increment: 1 } },
|
||||
});
|
||||
const openedCycle = await db.menstrualCycle.findFirst({
|
||||
where: { userId: user.id, deletedAt: null, startDate: existing.date },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!openedCycle) return { cycleId: null, reanchored: null };
|
||||
await db.menstrualCycle.update({
|
||||
where: { id: openedCycle.id },
|
||||
data: { deletedAt: new Date(), syncVersion: { increment: 1 } },
|
||||
});
|
||||
return {
|
||||
cycleId: openedCycle.id,
|
||||
reanchored: await reanchorAfterRemovedStart(db, user.id, existing.date),
|
||||
};
|
||||
});
|
||||
|
||||
await auditLog("cycle.day-log.delete", {
|
||||
userId: user.id,
|
||||
ipAddress: getClientIp(request),
|
||||
details: { dayLogId: id },
|
||||
details: {
|
||||
dayLogId: id,
|
||||
date: existing.date,
|
||||
...(removed.cycleId ? { removedCycleId: removed.cycleId } : {}),
|
||||
...(removed.reanchored
|
||||
? {
|
||||
reanchoredCycleId: removed.reanchored.cycleId,
|
||||
...overwriteDetails({
|
||||
before: {
|
||||
endDate: removed.reanchored.endDateBefore,
|
||||
lengthDays: removed.reanchored.lengthDaysBefore,
|
||||
},
|
||||
after: {
|
||||
endDate: removed.reanchored.endDateAfter,
|
||||
lengthDays: removed.reanchored.lengthDaysAfter,
|
||||
},
|
||||
}),
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
|
||||
annotate({
|
||||
@@ -228,6 +270,10 @@ export const DELETE = apiHandler(
|
||||
entity_type: "cycle_day_log",
|
||||
entity_id: id,
|
||||
},
|
||||
meta: {
|
||||
removed_cycle: removed.cycleId !== null,
|
||||
reanchored_prior: removed.reanchored !== null,
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(null, { status: 204 });
|
||||
|
||||
Reference in new issue
Block a user