user-group-relation.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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) => { return 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 UserGroup IDs that related to specified User
  119. *
  120. * @static
  121. * @param {User} user
  122. * @returns {Promise<ObjectId[]>}
  123. */
  124. static async findAllUserGroupIdsRelatedToUser(user) {
  125. const relations = await this.find({ relatedUser: user.id })
  126. .select('relatedGroup')
  127. .exec();
  128. return relations.map((relation) => { return relation.relatedGroup });
  129. }
  130. /**
  131. * find all entities with pagination
  132. *
  133. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  134. *
  135. * @static
  136. * @param {UserGroup} userGroup
  137. * @param {any} opts mongoose-paginate options object
  138. * @returns {Promise<any>} mongoose-paginate result object
  139. * @memberof UserGroupRelation
  140. */
  141. static findUserGroupRelationsWithPagination(userGroup, opts) {
  142. const query = { relatedGroup: userGroup };
  143. const options = Object.assign({}, opts);
  144. if (options.page == null) {
  145. options.page = 1;
  146. }
  147. if (options.limit == null) {
  148. options.limit = UserGroupRelation.PAGE_ITEMS;
  149. }
  150. return this.paginate(query, options)
  151. .catch((err) => {
  152. debug('Error on pagination:', err);
  153. });
  154. }
  155. /**
  156. * count by related group id and related user
  157. *
  158. * @static
  159. * @param {string} userGroupId find query param for relatedGroup
  160. * @param {User} userData find query param for relatedUser
  161. * @returns {Promise<number>}
  162. */
  163. static async countByGroupIdAndUser(userGroupId, userData) {
  164. const query = {
  165. relatedGroup: userGroupId,
  166. relatedUser: userData.id,
  167. };
  168. return this.count(query);
  169. }
  170. /**
  171. * find all "not" related user for UserGroup
  172. *
  173. * @static
  174. * @param {UserGroup} userGroup for find users not related
  175. * @returns {Promise<User>}
  176. * @memberof UserGroupRelation
  177. */
  178. static findUserByNotRelatedGroup(userGroup) {
  179. const User = UserGroupRelation.crowi.model('User');
  180. return this.findAllRelationForUserGroup(userGroup)
  181. .then((relations) => {
  182. const relatedUserIds = relations.map((relation) => {
  183. return relation.relatedUser.id;
  184. });
  185. const query = { _id: { $nin: relatedUserIds }, status: User.STATUS_ACTIVE };
  186. debug('findUserByNotRelatedGroup ', query);
  187. return User.find(query).exec();
  188. });
  189. }
  190. /**
  191. * get if the user has relation for group
  192. *
  193. * @static
  194. * @param {User} userData
  195. * @param {UserGroup} userGroup
  196. * @returns {Promise<boolean>} is user related for group(or not)
  197. * @memberof UserGroupRelation
  198. */
  199. static isRelatedUserForGroup(userData, userGroup) {
  200. const query = {
  201. relatedGroup: userGroup.id,
  202. relatedUser: userData.id,
  203. };
  204. return this
  205. .count(query)
  206. .exec()
  207. .then((count) => {
  208. // return true or false of the relation is exists(not count)
  209. return (count > 0);
  210. });
  211. }
  212. /**
  213. * create user and group relation
  214. *
  215. * @static
  216. * @param {UserGroup} userGroup
  217. * @param {User} user
  218. * @returns {Promise<UserGroupRelation>} created relation
  219. * @memberof UserGroupRelation
  220. */
  221. static createRelation(userGroup, user) {
  222. return this.create({
  223. relatedGroup: userGroup.id,
  224. relatedUser: user.id,
  225. });
  226. }
  227. /**
  228. * remove all relation for UserGroup
  229. *
  230. * @static
  231. * @param {UserGroup} userGroup related group for remove
  232. * @returns {Promise<any>}
  233. * @memberof UserGroupRelation
  234. */
  235. static removeAllByUserGroup(userGroup) {
  236. return this.deleteMany({ relatedGroup: userGroup });
  237. }
  238. /**
  239. * remove relation by id
  240. *
  241. * @static
  242. * @param {ObjectId} id
  243. * @returns {Promise<any>}
  244. * @memberof UserGroupRelation
  245. */
  246. static removeById(id) {
  247. return this.findById(id)
  248. .then((relationData) => {
  249. if (relationData == null) {
  250. throw new Error('UserGroupRelation data is not exists. id:', id);
  251. }
  252. else {
  253. relationData.remove();
  254. }
  255. });
  256. }
  257. }
  258. module.exports = function(crowi) {
  259. UserGroupRelation.crowi = crowi;
  260. schema.loadClass(UserGroupRelation);
  261. const model = mongoose.model('UserGroupRelation', schema);
  262. model.init();
  263. return model;
  264. };