feat(sleep): export one row per night, not per stage

The CSV + JSON export emitted a wall of per-stage sleep minutes with no
night attribution. Collapse SLEEP_DURATION to one record per night by
default — the main session's TIME ASLEEP per the nap convention — with
the stage breakdown, nap count, and awakenings in the notes column so
the single uniform export reads as one night. `granularity=raw` keeps
the per-stage rows for power users. Storage is untouched.
This commit is contained in:
Marc-André Bombeck committed 2026-06-04 18:56:20 +02:00
1 parent 5c863a0bcc
commit bb4f762968
4 files changed
+217 -14

No files matched your search

+12 -1
View File
@@ -37,6 +37,12 @@ export const GET = apiHandler(async (request: NextRequest) => {
const { since, until } = parseRange(request.url);
const where = buildWhere(user.id, { since, until });
// v1.11.5 — sleep export defaults to one row per night; `granularity=raw`
// keeps the per-stage rows for power users.
const granularity =
new URL(request.url).searchParams.get("granularity") === "raw"
? "raw"
: "night";
const [measurements, userTz] = await Promise.all([
prisma.measurement.findMany({
@@ -46,7 +52,12 @@ export const GET = apiHandler(async (request: NextRequest) => {
resolveUserTimezone(user.id),
]);
const csv = toCSV(formatMeasurementsForExport(measurements, userTz));
const csv = toCSV(
formatMeasurementsForExport(measurements, userTz, {
granularity,
sleepTz: userTz,
}),
);
await auditLog("user.export.measurements", {
userId: user.id,
+8 -3
View File
@@ -61,10 +61,15 @@ export const GET = apiHandler(async (request: NextRequest) => {
where: { userId, deletedAt: null },
orderBy: { measuredAt: "desc" },
});
// v1.11.5 — sleep collapses to one row per night by default (the
// formatter's `night` granularity), matching the per-type CSV route.
const measurementRecords = formatMeasurementsForExport(
measurements,
userTz,
{ sleepTz: userTz },
);
data.measurements =
format === "csv"
? toCSV(formatMeasurementsForExport(measurements, userTz))
: formatMeasurementsForExport(measurements, userTz);
format === "csv" ? toCSV(measurementRecords) : measurementRecords;
}
if (type === "medications" || type === "all") {