ProjectCollabratecDetailsHandler.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* eslint-disable
  2. camelcase,
  3. node/handle-callback-err,
  4. max-len,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let ProjectCollabratecDetailsHandler
  16. const { ObjectId } = require('mongodb')
  17. const { Project } = require('../../models/Project')
  18. module.exports = ProjectCollabratecDetailsHandler = {
  19. initializeCollabratecProject(
  20. project_id,
  21. user_id,
  22. collabratec_document_id,
  23. collabratec_privategroup_id,
  24. callback
  25. ) {
  26. if (callback == null) {
  27. callback = function(err) {}
  28. }
  29. return ProjectCollabratecDetailsHandler.setCollabratecUsers(
  30. project_id,
  31. [{ user_id, collabratec_document_id, collabratec_privategroup_id }],
  32. callback
  33. )
  34. },
  35. isLinkedCollabratecUserProject(project_id, user_id, callback) {
  36. if (callback == null) {
  37. callback = function(err, isLinked) {}
  38. }
  39. try {
  40. project_id = ObjectId(project_id)
  41. user_id = ObjectId(user_id)
  42. } catch (error) {
  43. const err = error
  44. return callback(err)
  45. }
  46. const query = {
  47. _id: project_id,
  48. collabratecUsers: {
  49. $elemMatch: {
  50. user_id
  51. }
  52. }
  53. }
  54. return Project.findOne(query, { _id: 1 }, function(err, project) {
  55. if (err != null) {
  56. callback(err)
  57. }
  58. return callback(null, project != null)
  59. })
  60. },
  61. linkCollabratecUserProject(
  62. project_id,
  63. user_id,
  64. collabratec_document_id,
  65. callback
  66. ) {
  67. if (callback == null) {
  68. callback = function(err) {}
  69. }
  70. try {
  71. project_id = ObjectId(project_id)
  72. user_id = ObjectId(user_id)
  73. } catch (error) {
  74. const err = error
  75. return callback(err)
  76. }
  77. const query = {
  78. _id: project_id,
  79. collabratecUsers: {
  80. $not: {
  81. $elemMatch: {
  82. collabratec_document_id,
  83. user_id
  84. }
  85. }
  86. }
  87. }
  88. const update = {
  89. $push: {
  90. collabratecUsers: {
  91. collabratec_document_id,
  92. user_id
  93. }
  94. }
  95. }
  96. return Project.updateOne(query, update, callback)
  97. },
  98. setCollabratecUsers(project_id, collabratec_users, callback) {
  99. let err
  100. if (callback == null) {
  101. callback = function(err) {}
  102. }
  103. try {
  104. project_id = ObjectId(project_id)
  105. } catch (error) {
  106. err = error
  107. return callback(err)
  108. }
  109. if (!Array.isArray(collabratec_users)) {
  110. callback(new Error('collabratec_users must be array'))
  111. }
  112. for (let collabratec_user of Array.from(collabratec_users)) {
  113. try {
  114. collabratec_user.user_id = ObjectId(collabratec_user.user_id)
  115. } catch (error1) {
  116. err = error1
  117. return callback(err)
  118. }
  119. }
  120. const update = { $set: { collabratecUsers: collabratec_users } }
  121. return Project.updateOne({ _id: project_id }, update, callback)
  122. },
  123. unlinkCollabratecUserProject(project_id, user_id, callback) {
  124. if (callback == null) {
  125. callback = function(err) {}
  126. }
  127. try {
  128. project_id = ObjectId(project_id)
  129. user_id = ObjectId(user_id)
  130. } catch (error) {
  131. const err = error
  132. return callback(err)
  133. }
  134. const query = { _id: project_id }
  135. const update = {
  136. $pull: {
  137. collabratecUsers: {
  138. user_id
  139. }
  140. }
  141. }
  142. return Project.updateOne(query, update, callback)
  143. },
  144. updateCollabratecUserIds(old_user_id, new_user_id, callback) {
  145. if (callback == null) {
  146. callback = function(err) {}
  147. }
  148. try {
  149. old_user_id = ObjectId(old_user_id)
  150. new_user_id = ObjectId(new_user_id)
  151. } catch (error) {
  152. const err = error
  153. return callback(err)
  154. }
  155. const query = { 'collabratecUsers.user_id': old_user_id }
  156. const update = { $set: { 'collabratecUsers.$.user_id': new_user_id } }
  157. return Project.updateMany(query, update, callback)
  158. }
  159. }