GlobalNotificationSetting.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const debug = require('debug')('growi:models:GlobalNotificationSetting');
  2. const mongoose = require('mongoose');
  3. /**
  4. * parent schema in this model
  5. */
  6. const notificationSchema = new mongoose.Schema({
  7. isEnabled: { type: Boolean, required: true, default: true },
  8. triggerPath: { type: String, required: true },
  9. triggerEvents: { type: [String] },
  10. });
  11. /**
  12. * create child schemas inherited from parentSchema
  13. * all child schemas are stored in globalnotificationsettings collection
  14. * @link{http://url.com module_name}
  15. * @param {object} parentSchema
  16. * @param {string} modelName
  17. * @param {string} discriminatorKey
  18. */
  19. const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
  20. const Notification = mongoose.model(modelName, parentSchema);
  21. const mailNotification = Notification.discriminator('mail', new mongoose.Schema({
  22. toEmail: String,
  23. }, {discriminatorKey: discriminatorKey}));
  24. const slackNotification = Notification.discriminator('slack', new mongoose.Schema({
  25. slackChannels: String,
  26. }, {discriminatorKey: discriminatorKey}));
  27. return {
  28. Mail: mailNotification,
  29. Slack: slackNotification,
  30. };
  31. };
  32. /**
  33. * GlobalNotificationSetting Class
  34. * @class GlobalNotificationSetting
  35. */
  36. class GlobalNotificationSetting {
  37. constructor(crowi) {
  38. this.crowi = crowi;
  39. }
  40. /**
  41. * enable notification setting
  42. * @param {string} id
  43. */
  44. static enable(id) {
  45. // return new Promise((resolve, reject) => {
  46. // save
  47. // return resolve(Notification)
  48. //}
  49. }
  50. /**
  51. * disable notification setting
  52. * @param {string} id
  53. */
  54. static disable(id) {
  55. // return new Promise((resolve, reject) => {
  56. // save
  57. // return resolve(Notification)
  58. //}
  59. }
  60. /**
  61. * find a list of notification settings by path and a list of events
  62. * @param {string} path
  63. * @param {string} event
  64. * @param {boolean} enabled
  65. */
  66. static findSettingByPathAndEvent(path, event, enabled) {
  67. // return new Promise((resolve, reject) => {
  68. // if(enabled == null) {
  69. // find all
  70. // }
  71. // else {
  72. // find only enabled/disabled
  73. // }
  74. // sort by path in mongoDB
  75. // return resolve([Notification])
  76. //}
  77. }
  78. // DELETEME
  79. test(s) {
  80. console.log(s)
  81. }
  82. }
  83. module.exports = function(crowi) {
  84. GlobalNotificationSetting.crowi = crowi;
  85. notificationSchema.loadClass(GlobalNotificationSetting);
  86. return createChildSchemas(
  87. notificationSchema,
  88. 'GlobalNotificationSetting',
  89. 'type',
  90. );
  91. };