page-group-relation.js 6.0 KB

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