NotificationsEditCtrl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import _ from 'lodash';
  2. import { appEvents, coreModule } from 'app/core/core';
  3. export class AlertNotificationEditCtrl {
  4. theForm: any;
  5. navModel: any;
  6. testSeverity = 'critical';
  7. notifiers: any;
  8. notifierTemplateId: string;
  9. isNew: boolean;
  10. model: any;
  11. defaults: any = {
  12. type: 'email',
  13. sendReminder: false,
  14. disableResolveMessage: false,
  15. frequency: '15m',
  16. settings: {
  17. httpMethod: 'POST',
  18. autoResolve: true,
  19. uploadImage: true,
  20. },
  21. isDefault: false,
  22. };
  23. getFrequencySuggestion: any;
  24. /** @ngInject */
  25. constructor(private $routeParams, private backendSrv, private $location, private $templateCache, navModelSrv) {
  26. this.navModel = navModelSrv.getNav('alerting', 'channels', 0);
  27. this.isNew = !this.$routeParams.id;
  28. this.getFrequencySuggestion = () => {
  29. return ['1m', '5m', '10m', '15m', '30m', '1h'];
  30. };
  31. this.backendSrv
  32. .get(`/api/alert-notifiers`)
  33. .then(notifiers => {
  34. this.notifiers = notifiers;
  35. // add option templates
  36. for (const notifier of this.notifiers) {
  37. this.$templateCache.put(this.getNotifierTemplateId(notifier.type), notifier.optionsTemplate);
  38. }
  39. if (!this.$routeParams.id) {
  40. this.navModel.breadcrumbs.push({ text: 'New channel' });
  41. this.navModel.node = { text: 'New channel' };
  42. return _.defaults(this.model, this.defaults);
  43. }
  44. return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => {
  45. this.navModel.breadcrumbs.push({ text: result.name });
  46. this.navModel.node = { text: result.name };
  47. result.settings = _.defaults(result.settings, this.defaults.settings);
  48. return result;
  49. });
  50. })
  51. .then(model => {
  52. this.model = model;
  53. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  54. });
  55. }
  56. save() {
  57. if (!this.theForm.$valid) {
  58. return;
  59. }
  60. if (this.model.id) {
  61. this.backendSrv
  62. .put(`/api/alert-notifications/${this.model.id}`, this.model)
  63. .then(res => {
  64. this.model = res;
  65. appEvents.emit('alert-success', ['Notification updated', '']);
  66. })
  67. .catch(err => {
  68. if (err.data && err.data.error) {
  69. appEvents.emit('alert-error', [err.data.error]);
  70. }
  71. });
  72. } else {
  73. this.backendSrv
  74. .post(`/api/alert-notifications`, this.model)
  75. .then(res => {
  76. appEvents.emit('alert-success', ['Notification created', '']);
  77. this.$location.path('alerting/notifications');
  78. })
  79. .catch(err => {
  80. if (err.data && err.data.error) {
  81. appEvents.emit('alert-error', [err.data.error]);
  82. }
  83. });
  84. }
  85. }
  86. getNotifierTemplateId(type) {
  87. return `notifier-options-${type}`;
  88. }
  89. typeChanged() {
  90. this.model.settings = _.defaults({}, this.defaults.settings);
  91. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  92. }
  93. testNotification() {
  94. if (!this.theForm.$valid) {
  95. return;
  96. }
  97. const payload = {
  98. name: this.model.name,
  99. type: this.model.type,
  100. frequency: this.model.frequency,
  101. settings: this.model.settings,
  102. };
  103. this.backendSrv.post(`/api/alert-notifications/test`, payload).then(res => {
  104. appEvents.emit('alert-success', ['Test notification sent', '']);
  105. });
  106. }
  107. }
  108. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);