GlobalNotificationSetting.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  79. module.exports = function(crowi) {
  80. GlobalNotificationSetting.crowi = crowi;
  81. notificationSchema.loadClass(GlobalNotificationSetting);
  82. return createChildSchemas(notificationSchema, 'GlobalNotificationSetting', 'type');
  83. };