user-group-relation.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. debug('findAllRelationForUserGroup is called', userGroup);
  78. return this
  79. .find({ relatedGroup: userGroup })
  80. .populate('relatedUser')
  81. .exec();
  82. }
  83. /**
  84. * find all user and group relation of UserGroups
  85. *
  86. * @static
  87. * @param {UserGroup[]} userGroups
  88. * @returns {Promise<UserGroupRelation[]>}
  89. * @memberof UserGroupRelation
  90. */
  91. static findAllRelationForUserGroups(userGroups) {
  92. return this
  93. .find({ relatedGroup: { $in: userGroups } })
  94. .populate('relatedUser')
  95. .exec();
  96. }
  97. /**
  98. * find all user and group relation of User
  99. *
  100. * @static
  101. * @param {User} user
  102. * @returns {Promise<UserGroupRelation[]>}
  103. * @memberof UserGroupRelation
  104. */
  105. static findAllRelationForUser(user) {
  106. return this
  107. .find({ relatedUser: user.id })
  108. .populate('relatedGroup')
  109. // filter documents only relatedGroup is not null
  110. .then((userGroupRelations) => {
  111. return userGroupRelations.filter((relation) => {
  112. return relation.relatedGroup != null;
  113. });
  114. });
  115. }
  116. /**
  117. * find all UserGroup IDs that related to specified User
  118. *
  119. * @static
  120. * @param {User} user
  121. * @returns {Promise<ObjectId[]>}
  122. */
  123. static async findAllUserGroupIdsRelatedToUser(user) {
  124. const relations = await this.find({ relatedUser: user._id })
  125. .select('relatedGroup')
  126. .exec();
  127. return relations.map((relation) => { return relation.relatedGroup });
  128. }
  129. /**
  130. * count by related group id and related user
  131. *
  132. * @static
  133. * @param {string} userGroupId find query param for relatedGroup
  134. * @param {User} userData find query param for relatedUser
  135. * @returns {Promise<number>}
  136. */
  137. static async countByGroupIdAndUser(userGroupId, userData) {
  138. const query = {
  139. relatedGroup: userGroupId,
  140. relatedUser: userData.id,
  141. };
  142. return this.count(query);
  143. }
  144. /**
  145. * find all "not" related user for UserGroup
  146. *
  147. * @static
  148. * @param {UserGroup} userGroup for find users not related
  149. * @returns {Promise<User>}
  150. * @memberof UserGroupRelation
  151. */
  152. static findUserByNotRelatedGroup(userGroup, queryOptions) {
  153. const User = UserGroupRelation.crowi.model('User');
  154. let searchWord = new RegExp(`${queryOptions.searchWord}`);
  155. switch (queryOptions.searchType) {
  156. case 'forward':
  157. searchWord = new RegExp(`^${queryOptions.searchWord}`);
  158. break;
  159. case 'backword':
  160. searchWord = new RegExp(`${queryOptions.searchWord}$`);
  161. break;
  162. }
  163. const searthField = [
  164. { username: searchWord },
  165. ];
  166. if (queryOptions.isAlsoMailSearched === 'true') { searthField.push({ email: searchWord }) }
  167. if (queryOptions.isAlsoNameSearched === 'true') { searthField.push({ name: searchWord }) }
  168. return this.findAllRelationForUserGroup(userGroup)
  169. .then((relations) => {
  170. const relatedUserIds = relations.map((relation) => {
  171. return relation.relatedUser.id;
  172. });
  173. const query = {
  174. _id: { $nin: relatedUserIds },
  175. status: User.STATUS_ACTIVE,
  176. $or: searthField,
  177. };
  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 {UserGroup} userGroup
  187. * @param {User} user
  188. * @returns {Promise<boolean>} is user related for group(or not)
  189. * @memberof UserGroupRelation
  190. */
  191. static isRelatedUserForGroup(userGroup, user) {
  192. const query = {
  193. relatedGroup: userGroup.id,
  194. relatedUser: user.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 (count > 0);
  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. static async createRelations(userGroupIds, user) {
  220. const documentsToInsertMany = userGroupIds.map((groupId) => {
  221. return {
  222. relatedGroup: groupId,
  223. relatedUser: user._id,
  224. createdAt: new Date(),
  225. };
  226. });
  227. return this.insertMany(documentsToInsertMany);
  228. }
  229. /**
  230. * remove all relation for UserGroup
  231. *
  232. * @static
  233. * @param {UserGroup} userGroup related group for remove
  234. * @returns {Promise<any>}
  235. * @memberof UserGroupRelation
  236. */
  237. static removeAllByUserGroups(groupsToDelete) {
  238. if (!Array.isArray(groupsToDelete)) {
  239. throw Error('groupsToDelete must be an array.');
  240. }
  241. return this.deleteMany({ relatedGroup: { $in: groupsToDelete } });
  242. }
  243. /**
  244. * remove relation by id
  245. *
  246. * @static
  247. * @param {ObjectId} id
  248. * @returns {Promise<any>}
  249. * @memberof UserGroupRelation
  250. */
  251. static removeById(id) {
  252. return this.findById(id)
  253. .then((relationData) => {
  254. if (relationData == null) {
  255. throw new Error('UserGroupRelation data is not exists. id:', id);
  256. }
  257. else {
  258. relationData.remove();
  259. }
  260. });
  261. }
  262. static async findUserIdsByGroupId(groupId) {
  263. const relations = await this.find({ relatedGroup: groupId }, { _id: 0, relatedUser: 1 }).lean().exec(); // .lean() to get not ObjectId but string
  264. return relations.map(relation => relation.relatedUser);
  265. }
  266. static async createByGroupIdsAndUserIds(groupIds, userIds) {
  267. const insertOperations = [];
  268. groupIds.forEach((groupId) => {
  269. userIds.forEach((userId) => {
  270. insertOperations.push({
  271. insertOne: {
  272. document: {
  273. relatedGroup: groupId,
  274. relatedUser: userId,
  275. },
  276. },
  277. });
  278. });
  279. });
  280. await this.bulkWrite(insertOperations);
  281. }
  282. }
  283. module.exports = function(crowi) {
  284. UserGroupRelation.crowi = crowi;
  285. schema.loadClass(UserGroupRelation);
  286. const model = mongoose.model('UserGroupRelation', schema);
  287. return model;
  288. };