mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
feat(workouts): accept an optional HR series on the batch contract
Extend POST /api/workouts/batch with an optional per-workout `samples`
array — a route-independent heart-rate series so indoor workouts still
yield a strain-relevant signal. Each entry is `{ t, hr?, speedMs?,
power?, cadence? }`; the array is capped at 30 000 points
(MAX_WORKOUT_HR_SAMPLES), covering a long endurance session sampled at
1 Hz while bounding the largest new workout-side write stream the same
way the 20 000-point route cap bounds the geometry.
Persistence rides the existing route-attachment path: the freshly
written workout id is resolved once (by composite key, or the manual
tuple), then both the route geometry and the HR series attach by FK
with skipDuplicates so a re-submitted batch stays idempotent. A workout
may carry both a GPS route and a canonical HR series, or just one.
GET /api/workouts/{id} returns the series alongside the route. OpenAPI
regenerated.
`samples` keeps first-write-wins/overwrite parity with the workout row
it hangs off: the 1:1 FK plus skipDuplicates means a re-ingest of an
existing workout never duplicates or mutates its series.
This commit is contained in:
1 parent
d247f6966a
commit
30be0c4e0d
8 files changed
+463
-13
No files matched your search
@@ -97,6 +97,13 @@ interface WorkoutFixture {
|
||||
geometry: { type: "LineString"; coordinates: [number, number][] };
|
||||
sampleTimestamps?: Array<{ t: string; speedMs?: number; hr?: number }>;
|
||||
};
|
||||
samples?: Array<{
|
||||
t: string;
|
||||
hr?: number;
|
||||
speedMs?: number;
|
||||
power?: number;
|
||||
cadence?: number;
|
||||
}>;
|
||||
}
|
||||
|
||||
function makeRequest(
|
||||
@@ -189,6 +196,42 @@ describe("POST /api/workouts/batch (real Postgres)", () => {
|
||||
expect(workouts[0]?.durationSec).toBe(45 * 60);
|
||||
});
|
||||
|
||||
it("persists a route-independent HR series for an indoor workout (v1.10.0)", async () => {
|
||||
const { POST } = await import("@/app/api/workouts/batch/route");
|
||||
|
||||
const body = {
|
||||
workouts: [
|
||||
baseWorkout("hk-uuid-indoor-001", {
|
||||
sportType: "strength",
|
||||
// No route — an indoor session has no GPS geometry.
|
||||
samples: [
|
||||
{ t: "2026-05-14T06:30:00.000Z", hr: 110 },
|
||||
{ t: "2026-05-14T06:30:05.000Z", hr: 118, power: 0, cadence: 0 },
|
||||
{ t: "2026-05-14T06:30:10.000Z", hr: 124 },
|
||||
],
|
||||
}),
|
||||
],
|
||||
};
|
||||
|
||||
const res = await POST(makeRequest(body));
|
||||
expect(res.status).toBe(200);
|
||||
const json = (await res.json()) as {
|
||||
data: { inserted: number };
|
||||
};
|
||||
expect(json.data.inserted).toBe(1);
|
||||
|
||||
const workouts = await getPrismaClient().workout.findMany({
|
||||
where: { userId: TEST_USER_ID, externalId: "hk-uuid-indoor-001" },
|
||||
include: { route: true, samples: true },
|
||||
});
|
||||
expect(workouts).toHaveLength(1);
|
||||
// No route row — the series lives in the dedicated child table.
|
||||
expect(workouts[0]?.route).toBeNull();
|
||||
expect(workouts[0]?.samples).not.toBeNull();
|
||||
expect(workouts[0]?.samples?.sampleCount).toBe(3);
|
||||
expect((workouts[0]?.samples?.samples as unknown[]).length).toBe(3);
|
||||
});
|
||||
|
||||
it("inserts a 100-workout batch with a mix of routed and routeless entries", async () => {
|
||||
const { POST } = await import("@/app/api/workouts/batch/route");
|
||||
|
||||
|
||||
Reference in new issue
Block a user