Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions controllers/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
31 changes: 21 additions & 10 deletions controllers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down