Skip to content

Commit 246e46a

Browse files
bewestCopilot
andcommitted
fix: legacy UUID treatments findable via $or fallback (#6923)
updateIdQuery() and upsertQueryFor() now use {$or: [{identifier: UUID}, {_id: UUID}]} instead of only {identifier: UUID}. This matches both: - New documents (UUID in identifier field, ObjectId in _id) - Legacy documents (UUID directly in _id, no identifier field) Gated behind env.uuidHandling (UUID_HANDLING env var, default true). All 30 treatment tests pass: - 3 legacy UUID tests (issue-6923): DELETE, PUT, GET all work - 12 gap-treat-012 tests: new data paths unaffected - 15 uuid-handling tests: edge cases, UUID_HANDLING=false still works Fixes #6923 (unable to edit/save/delete overrides for legacy data) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 98ee2bc commit 246e46a

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

lib/server/query.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ function updateIdQuery (query, opts) {
100100
if (typeof query._id === 'string') {
101101
var result = normalizeIdValue(query._id, opts);
102102
if (result.searchByIdentifier) {
103-
// UUID detected with uuidHandling enabled - search by identifier instead
104-
query.identifier = result.value;
103+
// UUID detected with uuidHandling enabled
104+
// Use $or to match both new docs (identifier field) and legacy docs (UUID in _id)
105+
query.$or = [{ identifier: result.value }, { _id: result.value }];
105106
delete query._id;
106107
} else {
107108
query._id = result.value;

lib/server/treatments.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,13 @@ function storage (env, ctx) {
320320
if (obj.identifier) {
321321
// Remove _id from replacement - MongoDB will use existing _id on update,
322322
// or generate new one on insert
323+
var identifierValue = obj.identifier;
323324
delete obj._id;
324-
return { identifier: obj.identifier };
325+
// Use $or to match both new docs (identifier field) and legacy docs (UUID in _id)
326+
if (env.uuidHandling) {
327+
return { $or: [{ identifier: identifierValue }, { _id: identifierValue }] };
328+
}
329+
return { identifier: identifierValue };
325330
}
326331
// 2. Fall back to _id if present and valid
327332
if (Object.prototype.hasOwnProperty.call(obj, '_id') && obj._id !== null && obj._id !== '') {

tests/issue-6923-legacy-uuid.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
/**
4-
* Legacy UUID Data Test: Issue #6923 Reproduction
4+
* Legacy UUID Data Test: Issue #6923 Regression
55
*
66
* ISSUE: https://github.com/nightscout/cgm-remote-monitor/issues/6923
77
*
@@ -11,10 +11,11 @@
1111
*
1212
* The test inserts a legacy-shaped document directly into MongoDB
1313
* (bypassing normalizeTreatmentId) and then exercises the API
14-
* DELETE and PUT paths that a user would trigger from Reports > Treatments.
14+
* DELETE, PUT, and GET paths that a user would trigger from Reports > Treatments.
1515
*
16-
* EXPECTED RESULT: These tests FAIL, proving the legacy data bug.
17-
* When all tests pass, the bug is fixed.
16+
* FIX: updateIdQuery() and upsertQueryFor() now use $or to match both
17+
* {identifier: UUID} (new docs) and {_id: UUID} (legacy docs).
18+
* All 3 tests pass, confirming Loop can manage pre-existing overrides.
1819
*/
1920

2021
const request = require('supertest');

0 commit comments

Comments
 (0)