page-group-relation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. const debug = require('debug')('crowi: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. /**
  41. * find all page and group relation
  42. *
  43. * @static
  44. * @returns {Promise<PageGroupRelation[]>}
  45. * @memberof PageGroupRelation
  46. */
  47. static findAllRelation() {
  48. return this
  49. .find()
  50. .populate('targetPage')
  51. .exec();
  52. }
  53. /**
  54. * find all page and group relation for UserGroup
  55. *
  56. * @static
  57. * @param {UserGroup} userGroup
  58. * @returns {Promise<PageGroupRelation[]>}
  59. * @memberof PageGroupRelation
  60. */
  61. static findAllRelationForUserGroup(userGroup) {
  62. debug('findAllRelationForUserGroup is called', userGroup);
  63. return this
  64. .find({ relatedGroup: userGroup.id })
  65. .populate('targetPage')
  66. .exec();
  67. }
  68. /**
  69. * find all entities with pagination
  70. *
  71. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  72. *
  73. * @static
  74. * @param {UserGroup} userGroup
  75. * @param {any} opts mongoose-paginate options object
  76. * @returns {Promise<any>} mongoose-paginate result object
  77. * @memberof UserGroupRelation
  78. */
  79. static findPageGroupRelationsWithPagination(userGroup, opts) {
  80. const query = { relatedGroup: userGroup };
  81. const options = Object.assign({}, opts);
  82. if (options.page == null) {
  83. options.page = 1;
  84. }
  85. if (options.limit == null) {
  86. options.limit = UserGroupRelation.PAGE_ITEMS;
  87. }
  88. return this.paginate(query, options)
  89. .catch((err) => {
  90. debug('Error on pagination:', err);
  91. });
  92. }
  93. /**
  94. * get if the page has relation for group
  95. *
  96. * @static
  97. * @param {Page} page
  98. * @param {UserGroup} userGroup
  99. * @returns {Promise<boolean>} is the relation for page and group exists(or not)
  100. * @memberof PageGroupRelation
  101. */
  102. static isExistsRelationForPageAndGroup(page, userGroup) {
  103. const query = { targetPage: page.id, relatedGroup: userGroup.id };
  104. return this
  105. .count(query)
  106. .then((count) => {
  107. return (0 < count);
  108. })
  109. .catch((err) => {
  110. debug('An Error occured.', err);
  111. return reject(err);
  112. });
  113. }
  114. /**
  115. * find page and group relation for Page
  116. *
  117. * @static
  118. * @param {Page} page
  119. * @returns {Promise<PageGroupRelation[]>}
  120. * @memberof PageGroupRelation
  121. */
  122. static findByPage(page) {
  123. return this
  124. .find({ targetPage: page.id })
  125. .populate('relatedGroup')
  126. .exec();
  127. }
  128. /**
  129. * create page and group relation
  130. *
  131. * @static
  132. * @param {any} userGroup
  133. * @param {any} page
  134. * @returns
  135. * @memberof PageGroupRelation
  136. */
  137. static createRelation(userGroup, page) {
  138. return this.create({
  139. relatedGroup: userGroup.id,
  140. targetPage: page.id,
  141. });
  142. };
  143. /**
  144. * remove all relation for UserGroup
  145. *
  146. * @static
  147. * @param {UserGroup} userGroup related group for remove
  148. * @returns {Promise<any>}
  149. * @memberof PageGroupRelation
  150. */
  151. static removeAllByUserGroup(userGroup) {
  152. return this.findAllRelationForUserGroup(userGroup)
  153. .then((relations) => {
  154. if (relations == null) {
  155. return;
  156. }
  157. else {
  158. relations.map((relation) => {
  159. relation.remove();
  160. });
  161. }
  162. });
  163. }
  164. /**
  165. * remove all relation for Page
  166. *
  167. * @static
  168. * @param {Page} page related page for remove
  169. * @returns {Promise<any>}
  170. * @memberof PageGroupRelation
  171. */
  172. static removeAllByPage(page) {
  173. return this.findByPage(page)
  174. .then((relations) => {
  175. debug('remove relations are ', relations);
  176. if (relations == null) {
  177. return;
  178. }
  179. else {
  180. relations.map((relation) => {
  181. relation.remove();
  182. });
  183. }
  184. });
  185. }
  186. /**
  187. * remove relation by id
  188. *
  189. * @static
  190. * @param {ObjectId} id for remove
  191. * @returns {Promise<any>}
  192. * @memberof PageGroupRelation
  193. */
  194. static removeById(id) {
  195. return this.findById(id)
  196. .then((relationData) => {
  197. if (relationData == null) {
  198. throw new Exception('PageGroupRelation data is not exists. id:', id);
  199. }
  200. else {
  201. relationData.remove();
  202. }
  203. })
  204. .catch((err) => {
  205. debug('Error on find a removing page-group-relation', err);
  206. return reject(err);
  207. });
  208. }
  209. }
  210. module.exports = function (crowi) {
  211. PageGroupRelation.crowi = crowi;
  212. schema.loadClass(PageGroupRelation);
  213. return mongoose.model('PageGroupRelation', schema);
  214. }