mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
fix(nutrients): bound the manual water writes and put nutrient totals in the backup
The water quick-add validated calendar realism but never the upper day bound the batch route applies, so a far-future day was accepted and — every read filters `day >= since` with no ceiling — became the permanent latest day on the settings card and the dashboard tile. Hoist the bound into one shared helper both routes use. `mode: "add"` bounded a single request to the catalog's plausible daily max but nothing across requests, so repeated quick-adds drove the stored total arbitrarily high while the batch route enforced the same cap strictly. Clamp after the increment so the write stays atomic. Thiamin's cap sat at exactly 1000 mg while its PRI is exactly 1.0 mg, so a microgram-read-as-milligram sample landed on precisely the cap and the `>` comparison let it through. 500 still clears any real supplement dose. NutrientIntakeDay was in no export path, which contradicted the schema's own reason for denormalising the unit column. Carry it in the full backup with its unit and source — source is part of the composite key, so without it a restore cannot tell a manual entry from a synced day total.
This commit is contained in:
1 parent
8ead442d44
commit
94157a50c7
16 files changed
+389
-90
No files matched your search
@@ -7,7 +7,8 @@
|
||||
* user's IANA timezone (the `stats:` day-key contract — the server
|
||||
* trusts the string after regex + calendar sanity, no re-derivation).
|
||||
*
|
||||
* Upsert key = the composite PK (userId, day, nutrient). Re-post
|
||||
* Upsert key = the composite PK (userId, day, nutrient, source), and this
|
||||
* route always owns the APPLE_HEALTH row. Re-post
|
||||
* replaces amount / unit / externalSourceVersion — last-writer-wins,
|
||||
* the day-total contract; per-entry status is `updated` then, never
|
||||
* `duplicate`. Validation failures (`unit_mismatch` | `value_out_of_range`
|
||||
@@ -42,6 +43,7 @@ import { checkRateLimit } from "@/lib/rate-limit";
|
||||
import { requireModuleEnabled } from "@/lib/modules/gate";
|
||||
import { invalidateUserDashboardSnapshot } from "@/lib/cache/invalidate";
|
||||
import { NUTRIENT_CATALOG } from "@/lib/nutrients/catalog";
|
||||
import { maxAcceptableNutrientDay } from "@/lib/nutrients/day-bounds";
|
||||
import {
|
||||
MAX_NUTRIENT_ENTRIES_PER_BATCH,
|
||||
nutrientBatchSchema,
|
||||
@@ -64,16 +66,6 @@ function isRealCalendarDay(day: string): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upper day bound with timezone slack: "tomorrow" in the most-ahead
|
||||
* IANA zone (UTC+14) is at most the UTC calendar date + 2 days, so a
|
||||
* key beyond that cannot be a legitimate local day and is corruption.
|
||||
*/
|
||||
function maxAcceptableDay(now: Date): string {
|
||||
const limit = new Date(now.getTime() + 2 * 24 * 60 * 60 * 1000);
|
||||
return limit.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
type EntryStatus = "inserted" | "updated" | "skipped";
|
||||
interface EntryResult {
|
||||
index: number;
|
||||
@@ -133,7 +125,7 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
}
|
||||
|
||||
const { entries } = parsed.data;
|
||||
const dayCeiling = maxAcceptableDay(new Date());
|
||||
const dayCeiling = maxAcceptableNutrientDay(new Date());
|
||||
|
||||
const results: EntryResult[] = new Array(entries.length);
|
||||
const skipped: Array<{ index: number; reason: string }> = [];
|
||||
|
||||
Reference in new issue
Block a user