diff --git a/services/web/app/coffee/Features/Docstore/DocstoreManager.coffee b/services/web/app/coffee/Features/Docstore/DocstoreManager.coffee index ea9ecdb721..c3ceeb5a35 100644 --- a/services/web/app/coffee/Features/Docstore/DocstoreManager.coffee +++ b/services/web/app/coffee/Features/Docstore/DocstoreManager.coffee @@ -28,4 +28,21 @@ module.exports = DocstoreManager = else error = new Error("docstore api responded with non-success code: #{res.statusCode}") logger.error err: error, project_id: project_id, "error getting all docs in docstore" - callback(error) \ No newline at end of file + callback(error) + + updateDoc: (project_id, doc_id, lines, version, callback = (error, rev) ->) -> + logger.log project_id: project_id, doc_id: doc_id, version: version, "updating doc in docstore api" + url = "#{settings.apis.docstore.url}/project/#{project_id}/doc/#{doc_id}" + request.post { + url: url + json: + lines: lines + version: version + }, (error, res, body) -> + return callback(error) if error? + if 200 <= res.statusCode < 300 + callback(null) + else + error = new Error("docstore api responded with non-success code: #{res.statusCode}") + logger.error err: error, project_id: project_id, doc_id: doc_id, "error updating doc in docstore" + callback(error) diff --git a/services/web/app/coffee/Features/Project/ProjectEntityHandler.coffee b/services/web/app/coffee/Features/Project/ProjectEntityHandler.coffee index ccf8a0870b..ffcf0e0d3e 100644 --- a/services/web/app/coffee/Features/Project/ProjectEntityHandler.coffee +++ b/services/web/app/coffee/Features/Project/ProjectEntityHandler.coffee @@ -117,10 +117,20 @@ module.exports = ProjectEntityHandler = logger.log sl_req_id: sl_req_id, project: project._id, folder_id: folder_id, doc_name: docName, "adding doc" return callback(err) if err? confirmFolder project, folder_id, (folder_id)=> - doc = new Doc name: docName, lines: docLines + doc = new Doc name: docName Project.putElement project._id, folder_id, doc, "doc", (err, result)=> - tpdsUpdateSender.addDoc {project_id:project._id, docLines:docLines, path:result.path.fileSystem, project_name:project.name, rev:doc.rev}, sl_req_id, -> - callback(err, doc, folder_id) + return callback(err) if err? + DocstoreManager.updateDoc project._id.toString(), doc._id.toString(), docLines, 0, (err, rev) -> + return callback(err) if err? + tpdsUpdateSender.addDoc { + project_id: project._id, + docLines: docLines, + path: result.path.fileSystem, + project_name: project.name, + rev: 0 + }, sl_req_id, (err) -> + return callback(err) if err? + callback(null, doc, folder_id) addFile: (project_or_id, folder_id, fileName, path, sl_req_id, callback = (error, fileRef, folder_id) ->)-> {callback, sl_req_id} = slReqIdHelper.getCallbackAndReqId(callback, sl_req_id) diff --git a/services/web/test/UnitTests/coffee/Docstore/DocstoreManagerTests.coffee b/services/web/test/UnitTests/coffee/Docstore/DocstoreManagerTests.coffee index b16dad0541..f54bde7832 100644 --- a/services/web/test/UnitTests/coffee/Docstore/DocstoreManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Docstore/DocstoreManagerTests.coffee @@ -52,6 +52,45 @@ describe "DocstoreManager", -> }, "error deleting doc in docstore") .should.equal true + describe "updateDoc", -> + beforeEach -> + @lines = ["mock", "doc", "lines"] + @version = 42 + + describe "with a successful response code", -> + beforeEach -> + @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204, "") + @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @callback + + it "should update the doc in the docstore api", -> + @request.post + .calledWith({ + url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}" + json: + lines: @lines + version: @version + }) + .should.equal true + + it "should call the callback without an error", -> + @callback.calledWith(null).should.equal true + + describe "with a failed response code", -> + beforeEach -> + @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, "") + @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @callback + + it "should call the callback with an error", -> + @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true + + it "should log the error", -> + @logger.error + .calledWith({ + err: new Error("docstore api responded with a non-success code: 500") + project_id: @project_id + doc_id: @doc_id + }, "error updating doc in docstore") + .should.equal true describe "getAllDocs", -> describe "with a successful response code", -> diff --git a/services/web/test/UnitTests/coffee/Project/ProjectEntityHandlerTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectEntityHandlerTests.coffee index cd86eb4e8e..89ac18cd71 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectEntityHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectEntityHandlerTests.coffee @@ -1,6 +1,7 @@ chai = require('chai') assert = require('chai').assert should = chai.should() +expect = chai.expect sinon = require 'sinon' modulePath = "../../../../app/js/Features/Project/ProjectEntityHandler" SandboxedModule = require('sandboxed-module') @@ -34,7 +35,8 @@ describe 'ProjectEntityHandler', -> rootFolder:[@rootFolder] @DocModel = class Doc constructor:(options)-> - {@name, @lines} = options + {@name, @lines} = options + @_id = "mock-id" @rev = 0 @FileModel = class File constructor:(options)-> @@ -277,42 +279,47 @@ describe 'ProjectEntityHandler', -> done() @ProjectEntityHandler._removeElementFromMongoArray model, id, mongoPath, -> - describe 'adding doc', -> - docName = "some new doc" - docLines = ['1234','abc'] + describe 'addDoc', -> + beforeEach -> + @name = "some new doc" + @lines = ['1234','abc'] + @path = "/path/to/doc" - it 'should call put element', (done)-> - @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedDoc, passedType, callback)-> - passedProject_id.should.equal project_id - passedFolder_id.should.equal folder_id - passedDoc.name.should.equal docName - passedDoc.lines[0].should.equal docLines[0] - passedDoc.lines[1].should.equal docLines[1] - passedType.should.equal 'doc' - done() - @ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "", (err, doc, parentFolder)-> + @ProjectModel.putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}}) + @callback = sinon.stub() + @tpdsUpdateSender.addDoc = sinon.stub().callsArg(2) + @DocstoreManager.updateDoc = sinon.stub().callsArgWith(4, null, 0) - it 'should return doc and parent folder', (done)-> - @ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "", (err, doc, parentFolder)-> - parentFolder.should.equal folder_id - doc.name.should.equal docName - done() + @ProjectEntityHandler.addDoc project_id, folder_id, @name, @lines, @callback - it 'should call third party data store', (done)-> - fileSystemPath = "/somehwere/#{docName}" - @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:fileSystemPath}}) + # Created doc + @doc = @ProjectModel.putElement.args[0][2] + @doc.name.should.equal @name + expect(@doc.lines).to.be.undefined - @tpdsUpdateSender.addDoc = (options)=> - options.project_id.should.equal project_id - options.docLines.should.equal docLines - options.path.should.equal fileSystemPath - options.project_name.should.equal @project.name - options.rev.should.equal 0 - done() + it 'should call put element', -> + @ProjectModel.putElement + .calledWith(project_id, folder_id, @doc) + .should.equal true - @ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "",-> + it 'should return doc and parent folder', -> + @callback.calledWith(null, @doc, folder_id).should.equal true - + it 'should call third party data store', -> + @tpdsUpdateSender.addDoc + .calledWith({ + project_id: project_id + docLines: @lines + path: @path + project_name: @project.name + rev: 0 + }) + .should.equal true + + it "should send the doc lines to the doc store", -> + @DocstoreManager.updateDoc + .calledWith(project_id, @doc._id.toString(), @lines, 0) + .should.equal true describe 'adding file', -> fileName = "something.jpg"