page-group-relation.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. module.exports = function(crowi) {
  2. var debug = require('debug')('crowi:models:pageGroupRelation')
  3. , mongoose = require('mongoose')
  4. , mongoosePaginate = require('mongoose-paginate')
  5. , ObjectId = mongoose.Schema.Types.ObjectId
  6. , PAGE_ITEMS = 50
  7. , pageGroupRelationSchema;
  8. pageGroupRelationSchema = new mongoose.Schema({
  9. pageGroupRelationId: String,
  10. relatedGroup: { type: ObjectId, ref: 'UserGroup' },
  11. targetPage: { type: ObjectId, ref: 'Page' },
  12. createdAt: { type: Date, default: Date.now },
  13. },{
  14. toJSON: { getters: true },
  15. toObject: { getters: true }
  16. });
  17. pageGroupRelationSchema.plugin(mongoosePaginate);
  18. // すべてのグループ所属関係を取得
  19. pageGroupRelationSchema.statics.findAllRelation = function() {
  20. debug('findAllRelations is called');
  21. var PageGroupRelation = this;
  22. return new Promise(function(resolve, reject) {
  23. PageGroupRelation
  24. .find({ relatedGroup: group} )
  25. .populate('targetPage')
  26. .exec(function (err, pageGroupRelationData) {
  27. if (err) {
  28. return reject(err);
  29. }
  30. return resolve(pageGroupRelationData);
  31. });
  32. });
  33. };
  34. // 指定グループに対するすべてのグループ所属関係を取得
  35. pageGroupRelationSchema.statics.findAllRelationForUserGroup = function (userGroup) {
  36. debug('findAllRelation is called', userGroup);
  37. var PageGroupRelation = this;
  38. return new Promise(function (resolve, reject) {
  39. PageGroupRelation
  40. .find({ relatedGroup: userGroup.id })
  41. .populate('targetPage')
  42. .exec(function (err, pageGroupRelationData) {
  43. if (err) {
  44. return reject(err);
  45. }
  46. return resolve(pageGroupRelationData);
  47. });
  48. });
  49. };
  50. // ページネーション利用の検索
  51. pageGroupRelationSchema.statics.findPageGroupRelationsWithPagination = function (userGroup, options, callback) {
  52. this.paginate({ relatedGroup: userGroup }, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  53. if (err) {
  54. debug('Error on pagination:', err);
  55. return callback(err, null);
  56. }
  57. return callback(err, result);
  58. });
  59. };
  60. // ページとグループを元に関係性を取得
  61. pageGroupRelationSchema.statics.checkIsExistsRelationForPageAndGroup = function (page, userGroup) {
  62. var PageGroupRelation = this
  63. return new Promise(function (resolve, reject) {
  64. PageGroupRelation
  65. .count({ targetPage: page.id, relatedGroup: userGroup.id })
  66. .exec(function (err, count) {
  67. if (err) {
  68. return reject(err);
  69. }
  70. return resolve(0 < count);
  71. });
  72. });
  73. };
  74. // ページを元に関係性を取得
  75. pageGroupRelationSchema.statics.findByPage = function(page) {
  76. var PageGroupRelation = this
  77. return new Promise(function (resolve, reject) {
  78. PageGroupRelation
  79. .findOne({ targetPage: page.id })
  80. .exec(function (err, data) {
  81. if (err) {
  82. return reject(err);
  83. }
  84. return resolve(data);
  85. });
  86. });
  87. };
  88. // 関係性の生成
  89. pageGroupRelationSchema.statics.createRelation = function(userGroup, page, callback) {
  90. var PageGroupRelation = this
  91. , newPageGroupRelation = new PageGroupRelation();
  92. if (userGroup == null || page == null) {
  93. return callback(new Error('userGroup or page is null'));
  94. }
  95. debug('create new page-group-relation for group ', userGroup);
  96. newPageGroupRelation.relatedGroup = userGroup.id;
  97. newPageGroupRelation.targetPage = page.id;
  98. newPageGroupRelation.createdAt = Date.now();
  99. debug('create new page-group-relation ', newPageGroupRelation);
  100. newPageGroupRelation.save(function(err, pageGroupRelationData) {
  101. return callback(err, pageGroupRelationData);
  102. });
  103. };
  104. // グループに紐づく関係性の全削除
  105. pageGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup, callback) {
  106. if (userGroup === null) { return callback(null); }
  107. var PageGroupRelation = this
  108. var relations = PageGroupRelation.findAllRelation(userGroup);
  109. // 関係性削除の実装
  110. relations.array.forEach(relation => {
  111. PageGroupRelation.removeById(relation.id, function(err) {
  112. if (err) { return callback(err); }
  113. });
  114. });
  115. return callback(null);
  116. }
  117. // ページに紐づく関係性の全削除
  118. pageGroupRelationSchema.statics.removeAllByPage = function (page, callback) {
  119. if (page === null) { return callback(null); }
  120. var PageGroupRelation = this
  121. var relations = PageGroupRelation.findByPage(page);
  122. if (relations != null && relations.length > 0) {
  123. // 関係性削除の実装
  124. relations.array.forEach(relation => {
  125. PageGroupRelation.removeById(relation.id, function (err) {
  126. if (err) { return callback(err); }
  127. });
  128. });
  129. }
  130. return callback(null);
  131. }
  132. // ページに紐づくグループの関係性の削除
  133. pageGroupRelationSchema.statics.removeRelationByUserGroupAndPage = function (userGroup, page, callback) {
  134. if (userGroup == null) { return callback(null); }
  135. if (page == null) { return callback(null); }
  136. var PageGroupRelation = this
  137. var relation = PageGroupRelation.findOne()({relatedGroup: userGroup.id, targetPage: page.id });
  138. if (relation == null) { return callback(); }
  139. PageGroupRelation.removeById(relation.id, function (err) {
  140. if (err) { return callback(err); }
  141. });
  142. return callback(null);
  143. }
  144. // ユーザグループの関係性を削除
  145. pageGroupRelationSchema.statics.removeById = function (id, callback) {
  146. var PageGroupRelation = this
  147. PageGroupRelation.findById(id, function (err, relationData) {
  148. if (err) {
  149. debug('Error on find a removing user-group-relation', err);
  150. return callback(err);
  151. }
  152. debug('relationData is ', relationData);
  153. if (relationData == null || relationData == undefined) {
  154. debug('Cannot find user group relation by id', id);
  155. return callback(new Error('Cannot find user group relation by id'));
  156. }
  157. relationData.remove(function(err) {
  158. if (err) {
  159. return callback(err);
  160. }
  161. return callback(null);
  162. });
  163. });
  164. }
  165. pageGroupRelationSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  166. return mongoose.model('PageGroupRelation', pageGroupRelationSchema);
  167. };