NotificationsEditCtrl.ts 3.8 KB

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