page-group-relation.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 })
  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.findAllRelationForUserGroups = function (userGroups, callback) {
  52. debug('findAllRelations is called', userGroups);
  53. var PageGroupRelation = this;
  54. var groupRelations = new Map();
  55. return new Promise(function (resolve, reject) {
  56. PageGroupRelation
  57. .find({ relatedGroup: { $in: userGroups} })
  58. .populate('targetPage')
  59. .exec(function (err, pageGroupRelationData) {
  60. if (err) {
  61. return reject(err);
  62. }
  63. debug(pageGroupRelationData);
  64. return resolve(pageGroupRelationData);
  65. });
  66. });
  67. };
  68. // ページネーション利用の検索
  69. pageGroupRelationSchema.statics.findPageGroupRelationsWithPagination = function (userGroup, options, callback) {
  70. this.paginate({ relatedGroup: userGroup }, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  71. if (err) {
  72. debug('Error on pagination:', err);
  73. return callback(err, null);
  74. }
  75. return callback(err, result);
  76. });
  77. };
  78. // 関係性の生成
  79. pageGroupRelationSchema.statics.createRelation = function(userGroup, page, callback) {
  80. var PageGroupRelation = this
  81. , newPageGroupRelation = new PageGroupRelation();
  82. if (userGroup == null || page == null) {
  83. return callback(new Error('userGroup or page is null'));
  84. }
  85. newPageGroupRelation.relatedGroup = userGroup;
  86. newPageGroupRelation.targetPage = page;
  87. newPageGroupRelation.createdAt = Date.now();
  88. debug('create new page-group-relation ', newPageGroupRelation);
  89. newPageGroupRelation.save(function(err, pageGroupRelationData) {
  90. return callback(err, pageGroupRelationData);
  91. });
  92. };
  93. // グループに紐づく関係性の全削除
  94. pageGroupRelationSchema.statics.removeAllByUserGroup = function (userGroup, callback) {
  95. if (userGroup === null) { return callback(null); }
  96. var PageGroupRelation = this
  97. var relations = PageGroupRelation.findAllRelation(userGroup);
  98. // 関係性削除の実装
  99. relations.array.forEach(relation => {
  100. PageGroupRelation.removeById(relation.id, function(err) {
  101. if (err) { return callback(err); }
  102. });
  103. });
  104. return callback(null);
  105. }
  106. // ページに紐づくグループの関係性の削除
  107. pageGroupRelationSchema.statics.removeRelationByUserGroupAndPage = function (userGroup, page, callback) {
  108. if (userGroup == null) { return callback(null); }
  109. if (page == null) { return callback(null); }
  110. var PageGroupRelation = this
  111. var relation = PageGroupRelation.findOne()({relatedGroup: userGroup.id, targetPage: page.id });
  112. if (relation == null) { return callback(); }
  113. PageGroupRelation.removeById(relation.id, function (err) {
  114. if (err) { return callback(err); }
  115. });
  116. return callback(null);
  117. }
  118. // ユーザグループの関係性を削除
  119. pageGroupRelationSchema.statics.removeById = function (id, callback) {
  120. var PageGroupRelation = this
  121. PageGroupRelation.findById(id, function (err, relationData) {
  122. if (err) {
  123. debug('Error on find a removing user-group-relation', err);
  124. return callback(err);
  125. }
  126. debug('relationData is ', relationData);
  127. if (relationData == null || relationData == undefined) {
  128. debug('Cannot find user group relation by id', id);
  129. return callback(new Error('Cannot find user group relation by id'));
  130. }
  131. relationData.remove(function(err) {
  132. if (err) {
  133. return callback(err);
  134. }
  135. return callback(null);
  136. });
  137. });
  138. }
  139. pageGroupRelationSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  140. return mongoose.model('PageGroupRelation', pageGroupRelationSchema);
  141. };