notification_edit_ctrl.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import config from 'app/core/config';
  5. import {appEvents, coreModule} from 'app/core/core';
  6. export class AlertNotificationEditCtrl {
  7. theForm: any;
  8. testSeverity: string = "critical";
  9. notifiers: any;
  10. notifierTemplateId: string;
  11. model: any;
  12. defaults: any = {
  13. type: 'email',
  14. settings: {
  15. httpMethod: 'POST',
  16. autoResolve: true,
  17. },
  18. isDefault: false
  19. };
  20. /** @ngInject */
  21. constructor(private $routeParams, private backendSrv, private $location, private $templateCache) {
  22. this.backendSrv.get(`/api/alert-notifiers`).then(notifiers => {
  23. this.notifiers = notifiers;
  24. // add option templates
  25. for (let notifier of this.notifiers) {
  26. this.$templateCache.put(this.getNotifierTemplateId(notifier.type), notifier.optionsTemplate);
  27. }
  28. if (!this.$routeParams.id) {
  29. return this.model;
  30. }
  31. return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => {
  32. return result;
  33. });
  34. }).then(model => {
  35. this.model = model;
  36. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  37. });
  38. }
  39. save() {
  40. if (!this.theForm.$valid) {
  41. return;
  42. }
  43. if (this.model.id) {
  44. this.backendSrv.put(`/api/alert-notifications/${this.model.id}`, this.model).then(res => {
  45. this.model = res;
  46. appEvents.emit('alert-success', ['Notification updated', '']);
  47. });
  48. } else {
  49. this.backendSrv.post(`/api/alert-notifications`, this.model).then(res => {
  50. appEvents.emit('alert-success', ['Notification created', '']);
  51. this.$location.path('alerting/notifications');
  52. });
  53. }
  54. }
  55. getNotifierTemplateId(type) {
  56. return `notifier-options-${type}`;
  57. }
  58. typeChanged() {
  59. this.model.settings = {};
  60. this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
  61. }
  62. testNotification() {
  63. if (!this.theForm.$valid) {
  64. return;
  65. }
  66. var payload = {
  67. name: this.model.name,
  68. type: this.model.type,
  69. settings: this.model.settings,
  70. };
  71. this.backendSrv.post(`/api/alert-notifications/test`, payload)
  72. .then(res => {
  73. appEvents.emit('alert-succes', ['Test notification sent', '']);
  74. });
  75. }
  76. }
  77. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);