user-group-relation.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. const debug = require('debug')('growi:models:userGroupRelation');
  2. const mongoose = require('mongoose');
  3. const mongoosePaginate = require('mongoose-paginate');
  4. const ObjectId = mongoose.Schema.Types.ObjectId;
  5. /*
  6. * define schema
  7. */
  8. const schema = new mongoose.Schema({
  9. relatedGroup: { type: ObjectId, ref: 'UserGroup', required: true },
  10. relatedUser: { type: ObjectId, ref: 'User', required: true },
  11. createdAt: { type: Date, default: Date.now, required: true },
  12. });
  13. schema.plugin(mongoosePaginate);
  14. /**
  15. * UserGroupRelation Class
  16. *
  17. * @class UserGroupRelation
  18. */
  19. class UserGroupRelation {
  20. /**
  21. * limit items num for pagination
  22. *
  23. * @readonly
  24. * @static
  25. * @memberof UserGroupRelation
  26. */
  27. static get PAGE_ITEMS() {
  28. return 50;
  29. }
  30. static set crowi(crowi) {
  31. this._crowi = crowi;
  32. }
  33. static get crowi() {
  34. return this._crowi;
  35. }
  36. static init() {
  37. this.removeAllInvalidRelations();
  38. }
  39. /**
  40. * remove all invalid relations that has reference to unlinked document
  41. */
  42. static removeAllInvalidRelations() {
  43. return this.findAllRelation()
  44. .then(relations => {
  45. // filter invalid documents
  46. return relations.filter(relation => {
  47. return relation.relatedUser == null || relation.relatedGroup == null;
  48. });
  49. })
  50. .then(invalidRelations => {
  51. const ids = invalidRelations.map(relation => relation._id);
  52. return this.deleteMany({ _id: { $in: ids }});
  53. });
  54. }
  55. /**
  56. * find all user and group relation
  57. *
  58. * @static
  59. * @returns {Promise<UserGroupRelation[]>}
  60. * @memberof UserGroupRelation
  61. */
  62. static findAllRelation() {
  63. return this
  64. .find()
  65. .populate('relatedUser')
  66. .populate('relatedGroup')
  67. .exec();
  68. }
  69. /**
  70. * find all user and group relation of UserGroup
  71. *
  72. * @static
  73. * @param {UserGroup} userGroup
  74. * @returns {Promise<UserGroupRelation[]>}
  75. * @memberof UserGroupRelation
  76. */
  77. static findAllRelationForUserGroup(userGroup) {
  78. debug('findAllRelationForUserGroup is called', userGroup);
  79. return this
  80. .find({ relatedGroup: userGroup })
  81. .populate('relatedUser')
  82. .exec();
  83. }
  84. /**
  85. * find all user and group relation of UserGroups
  86. *
  87. * @static
  88. * @param {UserGroup[]} userGroups
  89. * @returns {Promise<UserGroupRelation[]>}
  90. * @memberof UserGroupRelation
  91. */
  92. static findAllRelationForUserGroups(userGroups) {
  93. return this
  94. .find({ relatedGroup: { $in: userGroups } })
  95. .populate('relatedUser')
  96. .exec();
  97. }
  98. /**
  99. * find all user and group relation of User
  100. *
  101. * @static
  102. * @param {User} user
  103. * @returns {Promise<UserGroupRelation[]>}
  104. * @memberof UserGroupRelation
  105. */
  106. static findAllRelationForUser(user) {
  107. return this
  108. .find({ relatedUser: user.id })
  109. .populate('relatedGroup')
  110. // filter documents only relatedGroup is not null
  111. .then(userGroupRelations => {
  112. return userGroupRelations.filter(relation => {
  113. return relation.relatedGroup != null;
  114. });
  115. });
  116. }
  117. /**
  118. * find all entities with pagination
  119. *
  120. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  121. *
  122. * @static
  123. * @param {UserGroup} userGroup
  124. * @param {any} opts mongoose-paginate options object
  125. * @returns {Promise<any>} mongoose-paginate result object
  126. * @memberof UserGroupRelation
  127. */
  128. static findUserGroupRelationsWithPagination(userGroup, opts) {
  129. const query = { relatedGroup: userGroup };
  130. const options = Object.assign({}, opts);
  131. if (options.page == null) {
  132. options.page = 1;
  133. }
  134. if (options.limit == null) {
  135. options.limit = UserGroupRelation.PAGE_ITEMS;
  136. }
  137. return this.paginate(query, options)
  138. .catch((err) => {
  139. debug('Error on pagination:', err);
  140. });
  141. }
  142. /**
  143. * find one result by related group id and related user
  144. *
  145. * @static
  146. * @param {string} userGroupId find query param for relatedGroup
  147. * @param {User} userData find query param for relatedUser
  148. * @returns {Promise<UserGroupRelation>}
  149. * @memberof UserGroupRelation
  150. */
  151. static findByGroupIdAndUser(userGroupId, userData) {
  152. const query = {
  153. relatedGroup: userGroupId,
  154. relatedUser: userData.id
  155. };
  156. return this
  157. .findOne(query)
  158. .populate('relatedUser')
  159. .populate('relatedGroup')
  160. .exec();
  161. }
  162. /**
  163. * find all "not" related user for UserGroup
  164. *
  165. * @static
  166. * @param {UserGroup} userGroup for find users not related
  167. * @returns {Promise<User>}
  168. * @memberof UserGroupRelation
  169. */
  170. static findUserByNotRelatedGroup(userGroup) {
  171. const User = UserGroupRelation.crowi.model('User');
  172. return this.findAllRelationForUserGroup(userGroup)
  173. .then((relations) => {
  174. const relatedUserIds = relations.map((relation) => {
  175. return relation.relatedUser.id;
  176. });
  177. const query = { _id: { $nin: relatedUserIds }, status: User.STATUS_ACTIVE };
  178. debug('findUserByNotRelatedGroup ', query);
  179. return User.find(query).exec();
  180. });
  181. }
  182. /**
  183. * get if the user has relation for group
  184. *
  185. * @static
  186. * @param {User} userData
  187. * @param {UserGroup} userGroup
  188. * @returns {Promise<boolean>} is user related for group(or not)
  189. * @memberof UserGroupRelation
  190. */
  191. static isRelatedUserForGroup(userData, userGroup) {
  192. const query = {
  193. relatedGroup: userGroup.id,
  194. relatedUser: userData.id
  195. };
  196. return this
  197. .count(query)
  198. .exec()
  199. .then((count) => {
  200. // return true or false of the relation is exists(not count)
  201. return (0 < count);
  202. });
  203. }
  204. /**
  205. * create user and group relation
  206. *
  207. * @static
  208. * @param {UserGroup} userGroup
  209. * @param {User} user
  210. * @returns {Promise<UserGroupRelation>} created relation
  211. * @memberof UserGroupRelation
  212. */
  213. static createRelation(userGroup, user) {
  214. return this.create({
  215. relatedGroup: userGroup.id,
  216. relatedUser: user.id
  217. });
  218. }
  219. /**
  220. * remove all relation for UserGroup
  221. *
  222. * @static
  223. * @param {UserGroup} userGroup related group for remove
  224. * @returns {Promise<any>}
  225. * @memberof UserGroupRelation
  226. */
  227. static removeAllByUserGroup(userGroup) {
  228. return this.deleteMany({ relatedGroup: userGroup });
  229. }
  230. /**
  231. * remove relation by id
  232. *
  233. * @static
  234. * @param {ObjectId} id
  235. * @returns {Promise<any>}
  236. * @memberof UserGroupRelation
  237. */
  238. static removeById(id) {
  239. return this.findById(id)
  240. .then((relationData) => {
  241. if (relationData == null) {
  242. throw new Exception('UserGroupRelation data is not exists. id:', id);
  243. }
  244. else {
  245. relationData.remove();
  246. }
  247. });
  248. }
  249. }
  250. module.exports = function(crowi) {
  251. UserGroupRelation.crowi = crowi;
  252. schema.loadClass(UserGroupRelation);
  253. const model = mongoose.model('UserGroupRelation', schema);
  254. model.init();
  255. return model;
  256. };