mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
fix(cycle): honour the sensitive-data encryption toggle + harden the write path
- When the sensitive-category encryption preference is on (the default), encrypt the intent-revealing day-log fields (sexual activity, protection, pregnancy/progesterone tests, contraception) into an encrypted envelope and leave the queryable columns null, decrypting on read. The toggle now does what it says rather than storing those fields in plaintext. - Make the day-log write a partial merge so a re-log only overwrites the fields it carries, never nulling untouched ones (no more data loss when the same day is logged again). - Make the upsert collision-safe: a manual entry and an imported entry on the same date adopt one canonical row instead of throwing, and the single POST returns a clean conflict rather than a 500; one bad date no longer aborts a whole import. - Compare the decrypted note, not the ciphertext, so an unchanged re-log is a true no-op and stops needless re-sync. - Hoist cycle attribution out of the per-entry loop; map bulk failures to stable reason codes; audit the import goal change; mask the event name in discreet reminder pushes; add a one-tap hard-delete purge for all cycle data. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3e398295a3
commit
ac13dd28ed
22 files changed
+770
-174
No files matched your search
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* `DELETE /api/cycle/all` — one-click cycle hard-delete purge.
|
||||
*
|
||||
* Distinct from the per-row soft-delete (`DELETE /api/cycle/day-logs/{id}`,
|
||||
* which leaves a tombstone for the sync feed): this HARD-deletes every
|
||||
* cycle row the user owns — day-logs (and their symptom links, by cascade),
|
||||
* menstrual cycles, predictions, and the reminder ledger — so nothing
|
||||
* reproductive persists in Postgres. The `CycleProfile` row is reset to its
|
||||
* defaults (tracking left enabled) rather than dropped, so the gate and
|
||||
* settings page keep working.
|
||||
*
|
||||
* Gated (`cycle.disabled` 403) and owner-scoped. Audited.
|
||||
*/
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { auditLog } from "@/lib/auth/audit";
|
||||
import { apiSuccess, getClientIp } from "@/lib/api-response";
|
||||
import { requireCycleEnabled } from "@/lib/cycle/gate";
|
||||
|
||||
export const DELETE = apiHandler(async (request: NextRequest) => {
|
||||
const { user } = await requireAuth();
|
||||
|
||||
const gate = await requireCycleEnabled(user.id, user.gender);
|
||||
if (!gate.enabled) return gate.response;
|
||||
|
||||
// Hard delete in a transaction. Symptom links cascade off the day-log
|
||||
// delete (the schema's onDelete: Cascade). Order child-before-parent for
|
||||
// the rows that don't cascade.
|
||||
const cleared = await prisma.$transaction(async (tx) => {
|
||||
const dayLogs = await tx.cycleDayLog.deleteMany({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
const predictions = await tx.cyclePrediction.deleteMany({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
const cycles = await tx.menstrualCycle.deleteMany({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
return {
|
||||
dayLogs: dayLogs.count,
|
||||
predictions: predictions.count,
|
||||
cycles: cycles.count,
|
||||
};
|
||||
});
|
||||
|
||||
await auditLog("cycle.purge", {
|
||||
userId: user.id,
|
||||
ipAddress: getClientIp(request),
|
||||
details: cleared,
|
||||
});
|
||||
|
||||
annotate({
|
||||
action: { name: "cycle.purge" },
|
||||
meta: cleared,
|
||||
});
|
||||
|
||||
return apiSuccess({ purged: true, ...cleared });
|
||||
});
|
||||
Reference in new issue
Block a user