Salud: reset one measurement, without losing the rest
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s

"Podemos reiniciar los números de esas mediciones para yo enviar el de hoy." The whole-file reset
already existed two taps away in the setup sheet, but it is the wrong size for the question: the
four metrics he suspects are not the profile. Throwing away a month of heart rate to start steps
over is not a reset, it is a tax.

So a metric can now be forgotten on its own: the detail sheet has **Reset this measurement**, which
deletes that metric's days (and hours) and nothing else — the other metrics, the workouts and the
ingest key stay. With any Date Range wider than a day the phone re-sends that metric on its next
sync, which is what makes this a reset rather than a loss: Apple Health still has the data, and a
day total replaces the day it names, so the stored past is rewritten rather than duplicated.

The two destructive rows are worded apart on purpose and the confirm names the metric ("¿Reiniciar
Pasos?"), because the neighbouring action in the setup sheet takes every metric there is.

`dropMetric()` is pure and returns how many days it took, and the route answers `metric: null` for
a metric the profile does not have (or a profile with no health file at all) instead of an error —
a screen that says "nothing stored" is more useful than a 404 answering a button press. Tested
from both ends: the helper's purity and arithmetic, the route against a real server (one metric
gone, the others and the workouts intact, the key still working, and a re-sync rebuilding it), and
the sheet's flow through the real confirm. Mutation-checked: pointing the row at the whole-file
delete fails it.
This commit is contained in:
dsantillan committed 2026-09-23 16:18:28 -04:00
1 parent d93861a73f
commit 5c69540f62
11 files changed
+209 -7

No files matched your search

+41
View File
@@ -251,6 +251,47 @@ test('GET /api/vitals/hours opens one metric\'s day, and only with a session', a
assert.equal((await read('metric=nope&day=' + today)).metric, null);
});
test('DELETE /api/vitals?metric= resets one measurement and keeps everything else', async t => {
const h = await startServer(t);
const key = (await (await fetch(`${h.api}/api/vitals/key`, { method: 'POST', headers: cookie('u_h1') })).json()).key;
await ingest(h, key, payload());
await ingest(h, key, { data: { metrics: [{ name: 'step_count', units: 'count', data: [{ qty: 9000, date: `${yday} 00:00:00 +0000` }] }] } });
// A session, like every other read: the ingest key cannot delete anything either.
assert.equal((await fetch(`${h.api}/api/vitals?metric=step_count`)).status, 401);
assert.equal((await fetch(`${h.api}/api/vitals?metric=step_count`, { method: 'DELETE', headers: { Authorization: `Bearer ${key}` } })).status, 401);
const before = await (await fetch(`${h.api}/api/vitals`, { headers: cookie('u_h1') })).json();
assert.equal(before.metrics.step_count.days.length, 2, 'two days of steps to start over from');
const gone = await (await fetch(`${h.api}/api/vitals?metric=step_count`, { method: 'DELETE', headers: cookie('u_h1') })).json();
assert.deepEqual(gone, { ok: true, metric: 'step_count', days: 2 });
const after = await (await fetch(`${h.api}/api/vitals`, { headers: cookie('u_h1') })).json();
assert.equal(after.metrics.step_count, undefined, 'the metric is gone');
assert.deepEqual(Object.keys(after.metrics).sort(), ['heart_rate', 'weight_body_mass'], 'the others are not');
assert.equal(after.workouts.length, 1, 'and the workouts are not health data');
assert.equal(after.key, key, 'the key survives, so the phone keeps syncing');
// Asking again is not an error and not a surprise: it says it found nothing.
assert.deepEqual(await (await fetch(`${h.api}/api/vitals?metric=step_count`, { method: 'DELETE', headers: cookie('u_h1') })).json(),
{ ok: true, metric: null, days: 0 });
// A profile with no health file at all answers the same way rather than writing an empty one.
assert.deepEqual(await (await fetch(`${h.api}/api/vitals?metric=step_count`, { method: 'DELETE', headers: cookie('u_h2') })).json(),
{ ok: true, metric: null, days: 0 });
assert.equal((await (await fetch(`${h.api}/api/vitals`, { headers: cookie('u_h2') })).json()).metrics.step_count, undefined);
// The phone syncs again and the metric is rebuilt from what it sends.
assert.equal((await ingest(h, key, payload())).status, 200);
const rebuilt = await (await fetch(`${h.api}/api/vitals`, { headers: cookie('u_h1') })).json();
assert.equal(rebuilt.metrics.step_count.days.length, 1);
assert.equal(rebuilt.metrics.step_count.days[0].f.qty.sum, 8000);
// ...and the whole-file delete still works when no metric is named.
assert.equal((await fetch(`${h.api}/api/vitals`, { method: 'DELETE', headers: cookie('u_h1') })).status, 200);
assert.deepEqual((await (await fetch(`${h.api}/api/vitals`, { headers: cookie('u_h1') })).json()).metrics, {});
});
test('DELETE /api/vitals wipes the health file and leaves the account, the key and the workouts alone', async t => {
const h = await startServer(t);
const key = (await (await fetch(`${h.api}/api/vitals/key`, { method: 'POST', headers: cookie('u_h1') })).json()).key;