2018-12-04 06:19:48 -05:00
|
|
|
ObjectId = require("mongojs").ObjectId
|
2018-11-14 09:42:21 -05:00
|
|
|
Project = require("../../models/Project").Project
|
|
|
|
|
|
|
|
module.exports = ProjectCollabratecDetailsHandler =
|
2018-12-04 06:19:48 -05:00
|
|
|
initializeCollabratecProject: (project_id, user_id, collabratec_document_id, collabratec_privategroup_id, callback=(err)->) ->
|
2018-12-18 09:56:40 -05:00
|
|
|
ProjectCollabratecDetailsHandler.setCollabratecUsers project_id, [ { user_id, collabratec_document_id, collabratec_privategroup_id } ], callback
|
2018-12-04 06:19:48 -05:00
|
|
|
|
|
|
|
isLinkedCollabratecUserProject: (project_id, user_id, callback=(err, isLinked)->) ->
|
|
|
|
try
|
2018-12-18 09:56:40 -05:00
|
|
|
project_id = ObjectId project_id
|
|
|
|
user_id = ObjectId user_id
|
2018-12-04 06:19:48 -05:00
|
|
|
catch err
|
|
|
|
return callback err
|
|
|
|
query =
|
|
|
|
_id: project_id
|
|
|
|
collabratecUsers: $elemMatch:
|
|
|
|
user_id: user_id
|
|
|
|
Project.findOne query, {_id: 1}, (err, project) ->
|
|
|
|
callback err if err?
|
|
|
|
callback null, project?
|
|
|
|
|
|
|
|
linkCollabratecUserProject: (project_id, user_id, collabratec_document_id, callback=(err)->) ->
|
|
|
|
try
|
2018-12-18 09:56:40 -05:00
|
|
|
project_id = ObjectId project_id
|
|
|
|
user_id = ObjectId user_id
|
2018-12-04 06:19:48 -05:00
|
|
|
catch err
|
|
|
|
return callback err
|
|
|
|
query =
|
|
|
|
_id: project_id
|
|
|
|
collabratecUsers: $not: $elemMatch:
|
|
|
|
collabratec_document_id: collabratec_document_id
|
|
|
|
user_id: user_id
|
|
|
|
update = $push: collabratecUsers:
|
|
|
|
collabratec_document_id: collabratec_document_id
|
|
|
|
user_id: user_id
|
|
|
|
Project.update query, update, callback
|
|
|
|
|
2018-12-18 09:56:40 -05:00
|
|
|
setCollabratecUsers: (project_id, collabratec_users, callback=(err)->) ->
|
|
|
|
try
|
|
|
|
project_id = ObjectId project_id
|
|
|
|
catch err
|
|
|
|
return callback err
|
|
|
|
callback(new Error "collabratec_users must be array") unless Array.isArray(collabratec_users)
|
|
|
|
for collabratec_user in collabratec_users
|
|
|
|
try
|
|
|
|
collabratec_user.user_id = ObjectId(collabratec_user.user_id)
|
|
|
|
catch err
|
|
|
|
return callback err
|
|
|
|
update = $set: { collabratecUsers: collabratec_users }
|
|
|
|
Project.update { _id: project_id }, update, callback
|
|
|
|
|
2018-12-04 06:19:48 -05:00
|
|
|
unlinkCollabratecUserProject: (project_id, user_id, callback=(err)->) ->
|
|
|
|
try
|
2018-12-18 09:56:40 -05:00
|
|
|
project_id = ObjectId project_id
|
|
|
|
user_id = ObjectId user_id
|
2018-12-04 06:19:48 -05:00
|
|
|
catch err
|
|
|
|
return callback err
|
|
|
|
query =
|
|
|
|
_id: project_id
|
|
|
|
update = $pull: collabratecUsers:
|
|
|
|
user_id: user_id
|
|
|
|
Project.update query, update, callback
|
2018-12-18 09:56:40 -05:00
|
|
|
|
|
|
|
updateCollabratecUserIds: (old_user_id, new_user_id, callback=(err)->) ->
|
|
|
|
try
|
|
|
|
old_user_id = ObjectId old_user_id
|
|
|
|
new_user_id = ObjectId new_user_id
|
|
|
|
catch err
|
|
|
|
return callback err
|
|
|
|
query = "collabratecUsers.user_id": old_user_id
|
|
|
|
update = $set: "collabratecUsers.$.user_id": new_user_id
|
|
|
|
options = multi: true
|
|
|
|
Project.update query, update, options, callback
|