notificationChannels.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import memoizeOne from 'memoize-one';
  2. import { SelectableValue } from '@grafana/data';
  3. import { config } from '@grafana/runtime';
  4. import { NotificationChannelDTO, NotificationChannelType } from 'app/types';
  5. export const defaultValues: NotificationChannelDTO = {
  6. id: -1,
  7. name: '',
  8. type: { value: 'email', label: 'Email' },
  9. sendReminder: false,
  10. disableResolveMessage: false,
  11. frequency: '15m',
  12. settings: {
  13. uploadImage: config.rendererAvailable,
  14. autoResolve: true,
  15. httpMethod: 'POST',
  16. severity: 'critical',
  17. },
  18. secureSettings: {},
  19. secureFields: {},
  20. isDefault: false,
  21. };
  22. export const mapChannelsToSelectableValue = memoizeOne(
  23. (notificationChannels: NotificationChannelType[], includeDescription: boolean): Array<SelectableValue<string>> => {
  24. return notificationChannels.map((channel) => {
  25. if (includeDescription) {
  26. return {
  27. value: channel.value,
  28. label: channel.label,
  29. description: channel.description,
  30. };
  31. }
  32. return {
  33. value: channel.value,
  34. label: channel.label,
  35. };
  36. });
  37. }
  38. );
  39. export const transformSubmitData = (formData: NotificationChannelDTO) => {
  40. /*
  41. Some settings can be options in a select, in order to not save a SelectableValue<T>
  42. we need to use check if it is a SelectableValue and use its value.
  43. */
  44. const settings = Object.fromEntries(
  45. Object.entries(formData.settings).map(([key, value]) => {
  46. return [key, value && value.hasOwnProperty('value') ? value.value : value];
  47. })
  48. );
  49. return {
  50. ...defaultValues,
  51. ...formData,
  52. frequency: formData.frequency === '' ? defaultValues.frequency : formData.frequency,
  53. type: formData.type.value,
  54. settings: { ...defaultValues.settings, ...settings },
  55. secureSettings: { ...formData.secureSettings },
  56. };
  57. };
  58. export const transformTestData = (formData: NotificationChannelDTO) => {
  59. return {
  60. name: formData.name,
  61. type: formData.type.value,
  62. frequency: formData.frequency ?? defaultValues.frequency,
  63. settings: { ...Object.assign(defaultValues.settings, formData.settings) },
  64. secureSettings: { ...formData.secureSettings },
  65. };
  66. };