user-group-relation.js 7.0 KB

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