blob: 0871c8d855b0ce010f5233e36299d6008ea98e2c [file] [log] [blame]
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +00001import api, { getResponseCount } from '@/store/api';
2import i18n from '@/i18n';
3
4const SnmpAlertsStore = {
5 namespaced: true,
6 state: {
7 allSnmpDetails: [],
8 },
9 getters: {
10 allSnmpDetails(state) {
11 return state.allSnmpDetails;
12 },
13 },
14 mutations: {
15 setSnmpDetails(state, allSnmpDetails) {
16 state.allSnmpDetails = allSnmpDetails;
17 },
18 },
19 actions: {
20 async getSnmpAlertUrl() {
21 return await api
22 .get('/redfish/v1/')
23 .then((response) => api.get(response.data.EventService['@odata.id']))
24 .then((response) => api.get(response.data.Subscriptions['@odata.id']))
25 .then((response) => response.data['@odata.id'])
26 .catch((error) => console.log('Error', error));
27 },
28 async getSnmpDetails({ commit, dispatch }) {
29 const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
30 return await api
31 .get(snmpAlertUrl)
32 .then((response) =>
Ed Tanous81323992024-02-27 11:26:24 -080033 response.data.Members.map((user) => user['@odata.id']),
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000034 )
35 .then((userIds) => api.all(userIds.map((user) => api.get(user))))
36 .then((users) => {
37 const snmpDetailsData = users.map((user) => user.data);
38 commit('setSnmpDetails', snmpDetailsData);
39 })
40 .catch((error) => {
41 console.log(error);
42 const message = i18n.t('pageSnmpAlerts.toast.errorLoadSnmpDetails');
43 throw new Error(message);
44 });
45 },
46 async deleteDestination({ dispatch }, id) {
47 const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
48 return await api
49 .delete(`${snmpAlertUrl}/${id}`)
50 .then(() => dispatch('getSnmpDetails'))
51 .then(() =>
52 i18n.t('pageSnmpAlerts.toast.successDeleteDestination', {
53 id,
Ed Tanous81323992024-02-27 11:26:24 -080054 }),
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000055 )
56 .catch((error) => {
57 console.log(error);
58 const message = i18n.t(
59 'pageSnmpAlerts.toast.errorDeleteDestination',
60 {
61 id,
Ed Tanous81323992024-02-27 11:26:24 -080062 },
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000063 );
64 throw new Error(message);
65 });
66 },
67 async deleteMultipleDestinations({ dispatch }, destination) {
68 const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
69 const promises = destination.map(({ id }) => {
70 return api.delete(`${snmpAlertUrl}/${id}`).catch((error) => {
71 console.log(error);
72 return error;
73 });
74 });
75 return await api
76 .all(promises)
77 .then((response) => {
78 dispatch('getSnmpDetails');
79 return response;
80 })
81 .then(
82 api.spread((...responses) => {
83 const { successCount, errorCount } = getResponseCount(responses);
84 let toastMessages = [];
85
86 if (successCount) {
87 const message = i18n.tc(
88 'pageSnmpAlerts.toast.successBatchDelete',
Ed Tanous81323992024-02-27 11:26:24 -080089 successCount,
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000090 );
91 toastMessages.push({ type: 'success', message });
92 }
93
94 if (errorCount) {
95 const message = i18n.tc(
96 'pageSnmpAlerts.toast.errorBatchDelete',
Ed Tanous81323992024-02-27 11:26:24 -080097 errorCount,
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000098 );
99 toastMessages.push({ type: 'error', message });
100 }
101
102 return toastMessages;
Ed Tanous81323992024-02-27 11:26:24 -0800103 }),
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +0000104 );
105 },
106 async addDestination({ dispatch }, { data }) {
107 const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
108 return await api
109 .post(snmpAlertUrl, data)
110 .then(() => dispatch('getSnmpDetails'))
111 .then(() => i18n.t('pageSnmpAlerts.toast.successAddDestination'))
112 .catch((error) => {
113 console.log(error);
114 const message = i18n.t('pageSnmpAlerts.toast.errorAddDestination');
115 throw new Error(message);
116 });
117 },
118 },
119};
120
121export default SnmpAlertsStore;