2014-04-28 11:45:59 -04:00
|
|
|
MongoManager = require "./MongoManager"
|
2014-04-28 12:43:19 -04:00
|
|
|
Errors = require "./Errors"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
_ = require "underscore"
|
2015-01-20 12:09:51 -05:00
|
|
|
async = require "async"
|
2014-04-28 11:45:59 -04:00
|
|
|
|
|
|
|
module.exports = DocManager =
|
2015-01-22 10:05:48 -05:00
|
|
|
getDoc: (project_id, doc_id, callback = (error, doc, mongoPath) ->) ->
|
|
|
|
MongoManager.findDoc doc_id, (err, docFromDocCollection)->
|
|
|
|
return callback(err) if err?
|
|
|
|
MongoManager.findProject project_id, (error, project) ->
|
2014-04-28 11:45:59 -04:00
|
|
|
return callback(error) if error?
|
2015-01-22 10:05:48 -05:00
|
|
|
return callback new Errors.NotFoundError("No such project: #{project_id}") if !project?
|
|
|
|
DocManager.findDocInProject project, doc_id, (error, doc, mongoPath) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if docFromDocCollection?
|
|
|
|
return callback null, docFromDocCollection, mongoPath
|
|
|
|
else if doc?
|
2015-02-23 08:17:55 -05:00
|
|
|
if doc?.lines?.length > 0
|
|
|
|
logger.warn project_id:project_id, doc_id:doc_id, "doc just used from project collection, why?"
|
2015-01-22 10:05:48 -05:00
|
|
|
return callback null, doc, mongoPath
|
2014-06-05 09:10:44 -04:00
|
|
|
else
|
|
|
|
return callback new Errors.NotFoundError("No such doc: #{project_id}")
|
2014-04-28 12:43:19 -04:00
|
|
|
|
2014-04-30 08:06:12 -04:00
|
|
|
getAllDocs: (project_id, callback = (error, docs) ->) ->
|
|
|
|
MongoManager.findProject project_id, (error, project) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback new Errors.NotFoundError("No such project: #{project_id}") if !project?
|
|
|
|
DocManager.findAllDocsInProject project, (error, docs) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback null, docs
|
|
|
|
|
2014-05-16 08:06:35 -04:00
|
|
|
updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) ->
|
2014-04-28 12:43:19 -04:00
|
|
|
DocManager.getDoc project_id, doc_id, (error, doc, mongoPath) ->
|
|
|
|
return callback(error) if error?
|
2015-01-22 10:05:48 -05:00
|
|
|
return callback new Errors.NotFoundError("No such project/doc to update: #{project_id}/#{doc_id}") if !doc? or !mongoPath?
|
2014-04-28 12:43:19 -04:00
|
|
|
|
2014-05-16 08:06:35 -04:00
|
|
|
if _.isEqual(doc.lines, lines)
|
2014-04-28 12:43:19 -04:00
|
|
|
logger.log {
|
2014-05-16 08:06:35 -04:00
|
|
|
project_id: project_id, doc_id: doc_id, rev: doc.rev
|
|
|
|
}, "doc lines have not changed"
|
2014-05-08 10:43:08 -04:00
|
|
|
return callback null, false, doc.rev
|
2014-04-28 12:43:19 -04:00
|
|
|
else
|
|
|
|
logger.log {
|
2014-05-08 10:43:08 -04:00
|
|
|
project_id: project_id
|
|
|
|
doc_id: doc_id,
|
|
|
|
oldDocLines: doc.lines
|
|
|
|
newDocLines: lines
|
|
|
|
rev: doc.rev
|
2014-04-28 12:43:19 -04:00
|
|
|
}, "updating doc lines"
|
2015-01-20 12:09:51 -05:00
|
|
|
async.series [
|
|
|
|
(cb)->
|
|
|
|
MongoManager.upsertIntoDocCollection project_id, doc_id, lines, doc.rev, cb
|
2015-01-27 10:05:35 -05:00
|
|
|
(cb)->
|
|
|
|
MongoManager.updateDoc project_id, mongoPath, lines, cb
|
2015-01-20 12:09:51 -05:00
|
|
|
], (error)->
|
2014-04-28 12:43:19 -04:00
|
|
|
return callback(error) if error?
|
2014-05-08 10:43:08 -04:00
|
|
|
callback null, true, doc.rev + 1 # rev will have been incremented in mongo by MongoManager.updateDoc
|
2014-04-28 11:45:59 -04:00
|
|
|
|
2014-04-29 10:07:22 -04:00
|
|
|
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
|
|
|
|
DocManager.getDoc project_id, doc_id, (error, doc) ->
|
|
|
|
return callback(error) if error?
|
2015-01-22 10:05:48 -05:00
|
|
|
return callback new Errors.NotFoundError("No such project/doc to delete: #{project_id}/#{doc_id}") if !doc?
|
2015-01-20 12:09:51 -05:00
|
|
|
MongoManager.upsertIntoDocCollection project_id, doc_id, doc.lines, doc.rev, (error) ->
|
2015-01-21 07:31:06 -05:00
|
|
|
return callback(error) if error?
|
2015-01-20 12:09:51 -05:00
|
|
|
MongoManager.markDocAsDeleted doc_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
2014-04-29 10:07:22 -04:00
|
|
|
|
2014-04-30 08:06:12 -04:00
|
|
|
findAllDocsInProject: (project, callback = (error, docs) ->) ->
|
|
|
|
callback null, @_findAllDocsInFolder project.rootFolder[0]
|
|
|
|
|
2014-04-28 11:45:59 -04:00
|
|
|
findDocInProject: (project, doc_id, callback = (error, doc, mongoPath) ->) ->
|
|
|
|
result = @_findDocInFolder project.rootFolder[0], doc_id, "rootFolder.0"
|
|
|
|
if result?
|
|
|
|
callback null, result.doc, result.mongoPath
|
|
|
|
else
|
|
|
|
callback null, null, null
|
|
|
|
|
2014-05-29 09:21:46 -04:00
|
|
|
_findDocInFolder: (folder = {}, doc_id, currentPath) ->
|
2014-04-28 11:45:59 -04:00
|
|
|
for doc, i in folder.docs or []
|
2014-06-05 05:20:56 -04:00
|
|
|
if doc?._id? and doc._id.toString() == doc_id.toString()
|
2014-04-28 11:45:59 -04:00
|
|
|
return {
|
|
|
|
doc: doc
|
|
|
|
mongoPath: "#{currentPath}.docs.#{i}"
|
|
|
|
}
|
|
|
|
|
|
|
|
for childFolder, i in folder.folders or []
|
|
|
|
result = @_findDocInFolder childFolder, doc_id, "#{currentPath}.folders.#{i}"
|
|
|
|
return result if result?
|
|
|
|
|
2014-04-30 08:06:12 -04:00
|
|
|
return null
|
|
|
|
|
2014-05-29 09:21:46 -04:00
|
|
|
_findAllDocsInFolder: (folder = {}) ->
|
2014-04-30 08:06:12 -04:00
|
|
|
docs = folder.docs or []
|
|
|
|
for childFolder in folder.folders or []
|
|
|
|
docs = docs.concat @_findAllDocsInFolder childFolder
|
|
|
|
return docs
|
|
|
|
|