diff --git a/controllers/delete.js b/controllers/delete.js index 403319c..12aec2a 100644 --- a/controllers/delete.js +++ b/controllers/delete.js @@ -6,7 +6,7 @@ */ import { newID, isValidID, db } from '../database/index.js' import utils from '../utils.js' -import { createExpressError, getAgentClaim, parseDocumentID } from './utils.js' +import { createExpressError, getAgentClaim, parseDocumentID, getAllVersions, getAllDescendants } from './utils.js' /** * Mark an object as deleted in the database. @@ -218,37 +218,6 @@ async function newTreePrime(obj) { return true } -async function getAllVersions(obj) { - let ls_versions - let primeID = obj?.__rerum.history.prime - let rootObj = ( primeID === "root") - ? JSON.parse(JSON.stringify(obj)) - : await db.findOne({ "@id": primeID }) - ls_versions = await db.find({ "__rerum.history.prime": rootObj['@id'] }).toArray() - ls_versions.unshift(rootObj) - return ls_versions -} - -function getAllDescendants(ls_versions, keyObj, discoveredDescendants) { - let nextIDarr = [] - if (keyObj.__rerum.history.next.length === 0) { - //essentially, do nothing. This branch is done. - } - else { - nextIDarr = keyObj.__rerum.history.next - } - for (let nextID of nextIDarr) { - for (let v of ls_versions) { - if (v["@id"] === nextID) { - discoveredDescendants.push(v) - getAllDescendants(ls_versions, v, discoveredDescendants) - break - } - } - } - return discoveredDescendants -} - export { deleteObj } diff --git a/controllers/utils.js b/controllers/utils.js index 9de0c01..9da47ce 100644 --- a/controllers/utils.js +++ b/controllers/utils.js @@ -198,16 +198,27 @@ async function alterHistoryNext(objToUpdate, newNextID) { async function getAllVersions(obj) { let ls_versions let primeID = obj?.__rerum.history.prime - let rootObj = ( primeID === "root") - ? //The obj passed in is root. So it is the rootObj we need. - JSON.parse(JSON.stringify(obj)) - : //The obj passed in knows the ID of root, grab it from Mongo - await db.findOne({ "@id": primeID }) - /** - * Note that if you attempt the following code, it will cause Cannot convert undefined or null to object in getAllVersions. - * rootObj = await db.findOne({"$or":[{"_id": primeID}, {"__rerum.slug": primeID}]}) - * This is the because some of the @ids have different RERUM URL patterns on them. - **/ + let rootObj + if (primeID === "root") { + //The obj passed in is root. So it is the rootObj we need. + rootObj = JSON.parse(JSON.stringify(obj)) + } else if (primeID) { + //The obj passed in knows the ID of root, grab it from Mongo + //Use _id for indexed query performance instead of @id + let primeHexId + try { + primeHexId = parseDocumentID(primeID) + } catch (error) { + throw new Error(`Invalid history.prime value '${primeID}': ${error.message}`) + } + rootObj = await db.findOne({"$or":[{"_id": primeHexId}, {"__rerum.slug": primeHexId}]}) + if (!rootObj) { + throw new Error(`Root object with id '${primeID}' not found in database`) + } + } else { + //primeID is undefined or null, cannot proceed + throw new Error("Object has no valid history.prime value") + } //All the children of this object will have its @id in __rerum.history.prime ls_versions = await db.find({ "__rerum.history.prime": rootObj['@id'] }).toArray() //The root object is a version, prepend it in