Merge pull request #2618 from overleaf/ew-clear-root-doc-on-delete

Clear root doc on delete

GitOrigin-RevId: 4121d198f5253417bca2284c5f750c088debcb8c
This commit is contained in:
Eric Mc Sween
2020-02-27 07:46:37 -05:00
committed by Copybot
parent 17969c50ce
commit 1da929fcdb
4 changed files with 154 additions and 60 deletions
@@ -314,8 +314,12 @@ async function moveEntity(projectId, entityId, destFolderId, entityType) {
async function deleteEntity(projectId, entityId, entityType, callback) {
const project = await ProjectGetter.promises.getProjectWithoutLock(
projectId,
{ name: true, rootFolder: true, overleaf: true }
{ name: true, rootFolder: true, overleaf: true, rootDoc_id: true }
)
const deleteRootDoc =
project.rootDoc_id &&
entityId &&
project.rootDoc_id.toString() === entityId.toString()
const { element: entity, path } = await ProjectLocator.promises.findElement({
project,
element_id: entityId,
@@ -325,7 +329,8 @@ async function deleteEntity(projectId, entityId, entityType, callback) {
Project,
projectId,
path.mongo,
entityId
entityId,
deleteRootDoc
)
return { entity, path, projectBeforeDeletion: project, newProject }
}
@@ -414,19 +419,24 @@ async function _insertDeletedFileReference(projectId, fileRef) {
).exec()
}
async function _removeElementFromMongoArray(model, modelId, path, elementId) {
async function _removeElementFromMongoArray(
model,
modelId,
path,
elementId,
deleteRootDoc = false
) {
const nonArrayPath = path.slice(0, path.lastIndexOf('.'))
const newDoc = model
.findOneAndUpdate(
{ _id: modelId },
{
$pull: { [nonArrayPath]: { _id: elementId } },
$inc: { version: 1 }
},
{ new: true }
)
.exec()
return newDoc
const options = { new: true }
const query = { _id: modelId }
const update = {
$pull: { [nonArrayPath]: { _id: elementId } },
$inc: { version: 1 }
}
if (deleteRootDoc) {
update.$unset = { rootDoc_id: 1 }
}
return model.findOneAndUpdate(query, update, options).exec()
}
function _countElements(project) {