mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
AAPS V1 NSClient sync only ever calls nsAdd("profile", ...) — there is no
nsUpdate path for profiles (DataSyncSelectorV1.processChangedProfileStore).
Every profile edit reaches the server's websocket dbAdd handler.
Previously the dbAdd handler had no dedup branch for the 'profile' collection
and fell through to the generic else, which called insertOne() unconditionally
and then silently swallowed any insertion error via console.log + return [].
Result: each AAPS edit either created a duplicate profile document or failed
silently if the source JSONObject still carried an _id (E11000 dup key), so
users perceived their profile updates as not taking effect.
Changes:
- websocket.js: add a profile dedup branch — match on NSCLIENT_ID if present,
otherwise on startDate, and replaceOne in place rather than insertOne.
Returns the existing _id so the AAPS ack worker sees a stable identifier.
- websocket.js: upgrade the silent 'insertion error' console.log to
console.warn for both the profile branch and the generic fallback so
MongoDB write failures are visible in server logs.
- profile.js: add _id as a secondary sort key in last() so duplicate
startDate values resolve deterministically (newest insert wins) for any
legacy duplicates already present.
- tests/websocket.shape-handling.test.js: regression coverage for the
AAPS-shaped profile flow — first insert, repeated dbAdd with same
startDate (expect replace, not duplicate), and distinct startDate
(expect insert + last() returns newest).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>