page-group-relation.js 6.1 KB

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