Add SNMP alerts page and test hooks

This page will be included in Configuration section of the primary
navigation. The user will be able to delete and add alert destination.

Change-Id: I396d19a54ea11724f2c5aec67e20ba9abff947d3
Signed-off-by: Konstantin Aladyshev <aladyshev22@gmail.com>
diff --git a/src/store/index.js b/src/store/index.js
index c0b7894..8b1ed07 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -28,6 +28,7 @@
 import PostCodeLogsStore from './modules/Logs/PostCodeLogsStore';
 import PoliciesStore from './modules/SecurityAndAccess/PoliciesStore';
 import FactoryResetStore from './modules/Operations/FactoryResetStore';
+import SnmpAlertsStore from './modules/Settings/SnmpAlertsStore';
 import KeyClearStore from './modules/Operations/KeyClearStore';
 
 import WebSocketPlugin from './plugins/WebSocketPlugin';
@@ -58,6 +59,7 @@
     dumps: DumpsStore,
     sensors: SensorsStore,
     serverLed: ServerLedStore,
+    snmpAlerts: SnmpAlertsStore,
     certificates: CertificatesStore,
     system: SystemStore,
     memory: MemoryStore,
diff --git a/src/store/modules/Settings/SnmpAlertsStore.js b/src/store/modules/Settings/SnmpAlertsStore.js
new file mode 100644
index 0000000..f945ee3
--- /dev/null
+++ b/src/store/modules/Settings/SnmpAlertsStore.js
@@ -0,0 +1,121 @@
+import api, { getResponseCount } from '@/store/api';
+import i18n from '@/i18n';
+
+const SnmpAlertsStore = {
+  namespaced: true,
+  state: {
+    allSnmpDetails: [],
+  },
+  getters: {
+    allSnmpDetails(state) {
+      return state.allSnmpDetails;
+    },
+  },
+  mutations: {
+    setSnmpDetails(state, allSnmpDetails) {
+      state.allSnmpDetails = allSnmpDetails;
+    },
+  },
+  actions: {
+    async getSnmpAlertUrl() {
+      return await api
+        .get('/redfish/v1/')
+        .then((response) => api.get(response.data.EventService['@odata.id']))
+        .then((response) => api.get(response.data.Subscriptions['@odata.id']))
+        .then((response) => response.data['@odata.id'])
+        .catch((error) => console.log('Error', error));
+    },
+    async getSnmpDetails({ commit, dispatch }) {
+      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
+      return await api
+        .get(snmpAlertUrl)
+        .then((response) =>
+          response.data.Members.map((user) => user['@odata.id'])
+        )
+        .then((userIds) => api.all(userIds.map((user) => api.get(user))))
+        .then((users) => {
+          const snmpDetailsData = users.map((user) => user.data);
+          commit('setSnmpDetails', snmpDetailsData);
+        })
+        .catch((error) => {
+          console.log(error);
+          const message = i18n.t('pageSnmpAlerts.toast.errorLoadSnmpDetails');
+          throw new Error(message);
+        });
+    },
+    async deleteDestination({ dispatch }, id) {
+      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
+      return await api
+        .delete(`${snmpAlertUrl}/${id}`)
+        .then(() => dispatch('getSnmpDetails'))
+        .then(() =>
+          i18n.t('pageSnmpAlerts.toast.successDeleteDestination', {
+            id,
+          })
+        )
+        .catch((error) => {
+          console.log(error);
+          const message = i18n.t(
+            'pageSnmpAlerts.toast.errorDeleteDestination',
+            {
+              id,
+            }
+          );
+          throw new Error(message);
+        });
+    },
+    async deleteMultipleDestinations({ dispatch }, destination) {
+      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
+      const promises = destination.map(({ id }) => {
+        return api.delete(`${snmpAlertUrl}/${id}`).catch((error) => {
+          console.log(error);
+          return error;
+        });
+      });
+      return await api
+        .all(promises)
+        .then((response) => {
+          dispatch('getSnmpDetails');
+          return response;
+        })
+        .then(
+          api.spread((...responses) => {
+            const { successCount, errorCount } = getResponseCount(responses);
+            let toastMessages = [];
+
+            if (successCount) {
+              const message = i18n.tc(
+                'pageSnmpAlerts.toast.successBatchDelete',
+                successCount
+              );
+              toastMessages.push({ type: 'success', message });
+            }
+
+            if (errorCount) {
+              const message = i18n.tc(
+                'pageSnmpAlerts.toast.errorBatchDelete',
+                errorCount
+              );
+              toastMessages.push({ type: 'error', message });
+            }
+
+            return toastMessages;
+          })
+        );
+    },
+    async addDestination({ dispatch }, { data }) {
+      const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
+      return await api
+        .post(snmpAlertUrl, data)
+        .then(() => dispatch('getSnmpDetails'))
+        .then(() => i18n.t('pageSnmpAlerts.toast.successAddDestination'))
+        .catch((error) => {
+          console.log(error);
+          const message = i18n.t('pageSnmpAlerts.toast.errorAddDestination');
+          throw new Error(message);
+        });
+    },
+  },
+};
+
+export default SnmpAlertsStore;