Refactor ProjectDeleter to use CollaboratorHandler

This commit is contained in:
James Allen 2016-03-07 12:27:40 +00:00
parent 6d93076d51
commit 2ba2b72fd1
4 changed files with 27 additions and 15 deletions

View file

@ -21,6 +21,11 @@ module.exports = CollaboratorsHandler =
members.push { id: member_id, privilegeLevel: "readAndWrite" }
return callback null, members
getMemberIds: (project_id, callback = (error, member_ids) ->) ->
CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members) ->
return callback(error) if error?
return callback null, members.map (m) -> m.id
getMembersWithPrivilegeLevels: (project_id, callback = (error, members) ->) ->
CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members) ->
return callback(error) if error?

View file

@ -4,6 +4,7 @@ documentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
tagsHandler = require("../Tags/TagsHandler")
async = require("async")
FileStoreHandler = require("../FileStore/FileStoreHandler")
CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
module.exports = ProjectDeleter =
@ -43,16 +44,10 @@ module.exports = ProjectDeleter =
(cb)->
documentUpdaterHandler.flushProjectToMongoAndDelete project_id, cb
(cb)->
tagsHandler.removeProjectFromAllTags project.owner_ref, project_id, (err)->
CollaboratorsHandler.getMemberIds project_id, (error, member_ids = []) ->
for member_id in member_ids
tagsHandler.removeProjectFromAllTags member_id, project_id, (err)->
cb() #doesn't matter if this fails or the order it happens in
(cb)->
project.collaberator_refs.forEach (collaberator_ref)->
tagsHandler.removeProjectFromAllTags collaberator_ref, project_id, ->
cb()
(cb)->
project.readOnly_refs.forEach (readOnly_ref)->
tagsHandler.removeProjectFromAllTags readOnly_ref, project_id, ->
cb()
(cb)->
Project.update {_id:project_id}, { $set: { archived: true }}, cb
], (err)->

View file

@ -44,6 +44,19 @@ describe "CollaboratorsHandler", ->
])
.should.equal true
describe "getMemberIds", ->
beforeEach ->
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub()
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels
.withArgs(@project_id)
.yields(null, [{id: "member-id-1"}, {id: "member-id-2"}])
@CollaboratorHandler.getMemberIds @project_id, @callback
it "should return the ids", ->
@callback
.calledWith(null, ["member-id-1", "member-id-2"])
.should.equal true
describe "getMembersWithPrivilegeLevels", ->
beforeEach ->
@CollaboratorHandler.getMemberIdsWithPrivilegeLevels = sinon.stub()

View file

@ -31,6 +31,7 @@ describe 'ProjectDeleter', ->
'../DocumentUpdater/DocumentUpdaterHandler': @documentUpdaterHandler
"../Tags/TagsHandler":@TagsHandler
"../FileStore/FileStoreHandler": @FileStoreHandler = {}
"../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
'logger-sharelatex':
log:->
@ -89,6 +90,8 @@ describe 'ProjectDeleter', ->
describe "archiveProject", ->
beforeEach ->
@CollaboratorsHandler.getMemberIds = sinon.stub()
@CollaboratorsHandler.getMemberIds.withArgs(@project_id).yields(null, ["member-id-1", "member-id-2"])
@Project.update.callsArgWith(2)
it "should flushProjectToMongoAndDelete in doc updater", (done)->
@ -107,12 +110,8 @@ describe 'ProjectDeleter', ->
it "should removeProjectFromAllTags", (done)->
@deleter.archiveProject @project_id, =>
@TagsHandler.removeProjectFromAllTags.calledWith(@project.owner_ref, @project_id).should.equal true
@TagsHandler.removeProjectFromAllTags.calledWith(@project.collaberator_refs[0], @project_id).should.equal true
@TagsHandler.removeProjectFromAllTags.calledWith(@project.collaberator_refs[1], @project_id).should.equal true
@TagsHandler.removeProjectFromAllTags.calledWith(@project.readOnly_refs[0], @project_id).should.equal true
@TagsHandler.removeProjectFromAllTags.calledWith(@project.readOnly_refs[1], @project_id).should.equal true
@TagsHandler.removeProjectFromAllTags.calledWith("member-id-1", @project_id).should.equal true
@TagsHandler.removeProjectFromAllTags.calledWith("member-id-2", @project_id).should.equal true
done()
describe "restoreProject", ->