mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-11 00:57:18 +00:00
return project from _removeElementFromMongoArray and _putElement
This commit is contained in:
parent
929b9996d3
commit
607f0125fc
2 changed files with 42 additions and 40 deletions
services/web
app/coffee/Features/Project
test/UnitTests/coffee/Project
|
@ -492,17 +492,15 @@ module.exports = ProjectEntityHandler =
|
|||
|
||||
async.series jobs, callback
|
||||
|
||||
_removeElementFromMongoArray : (model, model_id, path, callback)->
|
||||
conditons = {_id:model_id}
|
||||
_removeElementFromMongoArray : (model, model_id, path, callback = (err, project) ->)->
|
||||
conditions = {_id:model_id}
|
||||
update = {"$unset":{}}
|
||||
update["$unset"][path] = 1
|
||||
model.update conditons, update, {}, (err)->
|
||||
model.update conditions, update, {}, (err)->
|
||||
pullUpdate = {"$pull":{}}
|
||||
nonArrayPath = path.slice(0, path.lastIndexOf("."))
|
||||
pullUpdate["$pull"][nonArrayPath] = null
|
||||
model.update conditons, pullUpdate, {}, (err)->
|
||||
if callback?
|
||||
callback(err)
|
||||
model.findOneAndUpdate conditions, pullUpdate, {"new": true}, callback
|
||||
|
||||
_insertDeletedDocReference: (project_id, doc, callback = (error) ->) ->
|
||||
Project.update {
|
||||
|
@ -539,8 +537,7 @@ module.exports = ProjectEntityHandler =
|
|||
|
||||
countFolder project.rootFolder[0], callback
|
||||
|
||||
_putElement: (project, folder_id, element, type, callback = (err, path)->)->
|
||||
|
||||
_putElement: (project, folder_id, element, type, callback = (err, path, project)->)->
|
||||
sanitizeTypeOfElement = (elementType)->
|
||||
lastChar = elementType.slice -1
|
||||
if lastChar != "s"
|
||||
|
@ -581,11 +578,11 @@ module.exports = ProjectEntityHandler =
|
|||
update = "$push":{}
|
||||
update["$push"][mongopath] = element
|
||||
logger.log project_id: project._id, element_id: element._id, fileType: type, folder_id: folder_id, mongopath:mongopath, "adding element to project"
|
||||
Project.update conditions, update, {}, (err)->
|
||||
Project.findOneAndUpdate conditions, update, {"new": true}, (err, project)->
|
||||
if err?
|
||||
logger.err err: err, project_id: project._id, 'error saving in putElement project'
|
||||
return callback(err)
|
||||
callback(err, {path:newPath})
|
||||
callback(err, {path:newPath}, project)
|
||||
|
||||
|
||||
confirmFolder = (project, folder_id, callback)->
|
||||
|
|
|
@ -361,23 +361,29 @@ describe 'ProjectEntityHandler', ->
|
|||
.calledWith(new Error("destination folder is a child folder of me"))
|
||||
.should.equal true
|
||||
|
||||
describe 'removing element from mongo array', ->
|
||||
it 'should call update with log the path', (done)->
|
||||
mongoPath = "folders[0].folders[5]"
|
||||
id = "12344"
|
||||
firstUpdate = true
|
||||
model =
|
||||
update: (conditions, update, opts, callback)->
|
||||
if firstUpdate
|
||||
conditions._id.should.equal id
|
||||
update.$unset[mongoPath].should.equal 1
|
||||
firstUpdate = false
|
||||
callback()
|
||||
else
|
||||
conditions._id.should.equal id
|
||||
assert.deepEqual update, { '$pull': { 'folders[0]': null } }
|
||||
done()
|
||||
@ProjectEntityHandler._removeElementFromMongoArray model, id, mongoPath, ->
|
||||
describe '_removeElementFromMongoArray ', ->
|
||||
beforeEach ->
|
||||
@mongoPath = "folders[0].folders[5]"
|
||||
@id = "12344"
|
||||
@project = 'a project'
|
||||
@ProjectModel.update = sinon.stub().callsArg(3)
|
||||
@ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
|
||||
@ProjectEntityHandler._removeElementFromMongoArray @ProjectModel, @id, @mongoPath, @callback
|
||||
|
||||
it 'should unset', ->
|
||||
update = { '$unset': { } }
|
||||
update['$unset'][@mongoPath] = 1
|
||||
@ProjectModel.update
|
||||
.calledWith({ _id: @id }, update, {})
|
||||
.should.equal true
|
||||
|
||||
it 'should pull', ->
|
||||
@ProjectModel.findOneAndUpdate
|
||||
.calledWith({ _id: @id }, { '$pull': { 'folders[0]': null } }, {'new': true})
|
||||
.should.equal true
|
||||
|
||||
it 'should call the callback', ->
|
||||
@callback.calledWith(null, @project).should.equal true
|
||||
|
||||
describe 'getDoc', ->
|
||||
beforeEach ->
|
||||
|
@ -1100,27 +1106,28 @@ describe 'ProjectEntityHandler', ->
|
|||
@path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
|
||||
@ProjectGetter.getProject.callsArgWith(2, null, @project)
|
||||
@projectLocator.findElement.callsArgWith(1, null, @folder, @path)
|
||||
@ProjectUpdateStub.callsArgWith(3)
|
||||
|
||||
@ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
|
||||
|
||||
describe "updating the project", ->
|
||||
|
||||
|
||||
it "should use the correct mongo path", (done)->
|
||||
@ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
|
||||
@ProjectModel.update.args[0][0]._id.should.equal @project._id
|
||||
assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
|
||||
@ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
|
||||
assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
|
||||
done()
|
||||
|
||||
it "should return the project in the callback", (done)->
|
||||
@ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
|
||||
expect(project).to.equal @project
|
||||
done()
|
||||
|
||||
it "should add an s onto the type if not included", (done)->
|
||||
@ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
|
||||
assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
|
||||
assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
|
||||
done()
|
||||
|
||||
|
||||
it "should not call update if elemenet is null", (done)->
|
||||
it "should not call update if element is null", (done)->
|
||||
@ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
|
||||
@ProjectModel.update.called.should.equal false
|
||||
@ProjectModel.findOneAndUpdate.called.should.equal false
|
||||
done()
|
||||
|
||||
it "should default to root folder insert", (done)->
|
||||
|
@ -1132,11 +1139,9 @@ describe 'ProjectEntityHandler', ->
|
|||
doc =
|
||||
name:"something"
|
||||
@ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
|
||||
@ProjectModel.update.called.should.equal false
|
||||
@ProjectModel.findOneAndUpdate.called.should.equal false
|
||||
done()
|
||||
|
||||
|
||||
|
||||
describe "_countElements", ->
|
||||
|
||||
beforeEach ->
|
||||
|
|
Loading…
Add table
Reference in a new issue