|
@@ -310,20 +310,15 @@ module.exports = {
|
|
subtitle: content.meta.subtitle || '',
|
|
subtitle: content.meta.subtitle || '',
|
|
parentTitle: content.parent.title || '',
|
|
parentTitle: content.parent.title || '',
|
|
parentPath: parentPath,
|
|
parentPath: parentPath,
|
|
- isDirectory: false
|
|
|
|
|
|
+ isDirectory: false,
|
|
|
|
+ isEntry: true
|
|
}, {
|
|
}, {
|
|
new: true,
|
|
new: true,
|
|
upsert: true
|
|
upsert: true
|
|
})
|
|
})
|
|
}).then(result => {
|
|
}).then(result => {
|
|
- return db.Entry.distinct('parentPath', { parentPath: { $ne: '' } }).then(allPaths => {
|
|
|
|
- if (allPaths.length > 0) {
|
|
|
|
- return db.Entry.updateMany({ _id: { $in: allPaths } }, { $set: { isDirectory: true } }).then(() => {
|
|
|
|
- return result
|
|
|
|
- })
|
|
|
|
- } else {
|
|
|
|
- return result
|
|
|
|
- }
|
|
|
|
|
|
+ return self.updateTreeInfo().then(() => {
|
|
|
|
+ return result
|
|
})
|
|
})
|
|
}).catch(err => {
|
|
}).catch(err => {
|
|
winston.error(err)
|
|
winston.error(err)
|
|
@@ -331,6 +326,28 @@ module.exports = {
|
|
})
|
|
})
|
|
},
|
|
},
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Update tree info for all directory and parent entries
|
|
|
|
+ *
|
|
|
|
+ * @returns {Promise<Boolean>} Promise of the operation
|
|
|
|
+ */
|
|
|
|
+ updateTreeInfo () {
|
|
|
|
+ return db.Entry.distinct('parentPath', { parentPath: { $ne: '' } }).then(allPaths => {
|
|
|
|
+ if (allPaths.length > 0) {
|
|
|
|
+ return Promise.map(allPaths, pathItem => {
|
|
|
|
+ let parentPath = _.chain(pathItem).split('/').initial().join('/').value()
|
|
|
|
+ let guessedTitle = _.chain(pathItem).split('/').last().startCase().value()
|
|
|
|
+ return db.Entry.update({ _id: pathItem }, {
|
|
|
|
+ $set: { isDirectory: true },
|
|
|
|
+ $setOnInsert: { isEntry: false, title: guessedTitle, parentPath }
|
|
|
|
+ }, { upsert: true })
|
|
|
|
+ })
|
|
|
|
+ } else {
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Create a new document
|
|
* Create a new document
|
|
*
|
|
*
|
|
@@ -428,6 +445,6 @@ module.exports = {
|
|
* @return {Promise<Array>} List of entries
|
|
* @return {Promise<Array>} List of entries
|
|
*/
|
|
*/
|
|
getFromTree (basePath) {
|
|
getFromTree (basePath) {
|
|
- return db.Entry.find({ parentPath: basePath })
|
|
|
|
|
|
+ return db.Entry.find({ parentPath: basePath }, 'title parentPath isDirectory isEntry').sort({ title: 'asc' })
|
|
}
|
|
}
|
|
}
|
|
}
|