2
0

user-group-relation.js 7.9 KB

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