2014-02-12 05:23:40 -05:00
|
|
|
Project = require('../../models/Project').Project
|
2016-02-26 03:13:41 -05:00
|
|
|
ProjectGetter = require("./ProjectGetter")
|
2014-02-12 05:23:40 -05:00
|
|
|
logger = require('logger-sharelatex')
|
2014-04-07 08:47:10 -04:00
|
|
|
documentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
|
|
|
|
tagsHandler = require("../Tags/TagsHandler")
|
|
|
|
async = require("async")
|
|
|
|
FileStoreHandler = require("../FileStore/FileStoreHandler")
|
2016-03-07 07:27:40 -05:00
|
|
|
CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-06-04 07:45:04 -04:00
|
|
|
module.exports = ProjectDeleter =
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-10-09 04:13:38 -04:00
|
|
|
markAsDeletedByExternalSource : (project_id, callback = (error) ->)->
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log project_id:project_id, "marking project as deleted by external data source"
|
|
|
|
conditions = {_id:project_id}
|
|
|
|
update = {deletedByExternalDataSource:true}
|
|
|
|
|
|
|
|
Project.update conditions, update, {}, (err)->
|
2014-04-07 08:47:10 -04:00
|
|
|
require('../Editor/EditorController').notifyUsersProjectHasBeenDeletedOrRenamed project_id, ->
|
2014-02-12 05:23:40 -05:00
|
|
|
callback()
|
2014-10-09 04:13:38 -04:00
|
|
|
|
2014-11-07 07:31:47 -05:00
|
|
|
unmarkAsDeletedByExternalSource: (project_id, callback = (error) ->) ->
|
|
|
|
logger.log project_id: project_id, "removing flag marking project as deleted by external data source"
|
|
|
|
conditions = {_id:project_id.toString()}
|
|
|
|
update = {deletedByExternalDataSource: false}
|
|
|
|
Project.update conditions, update, {}, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-06-30 09:21:44 -04:00
|
|
|
deleteUsersProjects: (user_id, callback)->
|
|
|
|
logger.log {user_id}, "deleting users projects"
|
|
|
|
Project.remove owner_ref:user_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
CollaboratorsHandler.removeUserFromAllProjets user_id, callback
|
2014-04-07 08:47:10 -04:00
|
|
|
|
2014-06-04 07:45:04 -04:00
|
|
|
deleteProject: (project_id, callback = (error) ->) ->
|
|
|
|
# archiveProject takes care of the clean-up
|
|
|
|
ProjectDeleter.archiveProject project_id, (error) ->
|
|
|
|
logger.log project_id: project_id, "deleting project"
|
|
|
|
Project.remove _id: project_id, callback
|
|
|
|
|
2014-06-03 12:35:44 -04:00
|
|
|
archiveProject: (project_id, callback = (error) ->)->
|
2016-03-08 09:38:23 -05:00
|
|
|
logger.log project_id:project_id, "archived project from user request"
|
2016-02-29 08:05:17 -05:00
|
|
|
ProjectGetter.getProject project_id, {owner_ref:true, collaberator_refs:true, readOnly_refs:true}, (err, project)=>
|
2014-04-07 08:47:10 -04:00
|
|
|
if err? or !project?
|
2016-03-08 09:38:23 -05:00
|
|
|
logger.err err:err, project_id:project_id, "error getting project to archived it"
|
2014-04-07 08:47:10 -04:00
|
|
|
callback(err)
|
|
|
|
else
|
|
|
|
async.series [
|
|
|
|
(cb)->
|
|
|
|
documentUpdaterHandler.flushProjectToMongoAndDelete project_id, cb
|
|
|
|
(cb)->
|
2016-03-07 07:27:40 -05:00
|
|
|
CollaboratorsHandler.getMemberIds project_id, (error, member_ids = []) ->
|
|
|
|
for member_id in member_ids
|
|
|
|
tagsHandler.removeProjectFromAllTags member_id, project_id, (err)->
|
2014-04-16 12:44:57 -04:00
|
|
|
cb() #doesn't matter if this fails or the order it happens in
|
2014-04-07 08:47:10 -04:00
|
|
|
(cb)->
|
2014-04-28 07:53:43 -04:00
|
|
|
Project.update {_id:project_id}, { $set: { archived: true }}, cb
|
2014-04-16 12:44:57 -04:00
|
|
|
], (err)->
|
|
|
|
if err?
|
2016-03-08 09:38:23 -05:00
|
|
|
logger.err err:err, "problem archived project"
|
|
|
|
return callback(err)
|
|
|
|
logger.log project_id:project_id, "succesfully archived project from user request"
|
|
|
|
callback()
|
2014-06-03 12:35:44 -04:00
|
|
|
|
|
|
|
restoreProject: (project_id, callback = (error) ->) ->
|
|
|
|
Project.update {_id:project_id}, { $unset: { archived: true }}, callback
|