blob: 0aabf4ee9cc99bbba0994c9e9bd63d76aadc5bfc [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);
Surya Vde23ea22024-07-11 15:19:46 +053042 const message = i18n.global.t(
43 'pageSnmpAlerts.toast.errorLoadSnmpDetails',
44 );
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000045 throw new Error(message);
46 });
47 },
48 async deleteDestination({ dispatch }, id) {
49 const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
50 return await api
51 .delete(`${snmpAlertUrl}/${id}`)
52 .then(() => dispatch('getSnmpDetails'))
53 .then(() =>
Surya Vde23ea22024-07-11 15:19:46 +053054 i18n.global.t('pageSnmpAlerts.toast.successDeleteDestination', {
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000055 id,
Ed Tanous81323992024-02-27 11:26:24 -080056 }),
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000057 )
58 .catch((error) => {
59 console.log(error);
Surya Vde23ea22024-07-11 15:19:46 +053060 const message = i18n.global.t(
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000061 'pageSnmpAlerts.toast.errorDeleteDestination',
62 {
63 id,
Ed Tanous81323992024-02-27 11:26:24 -080064 },
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000065 );
66 throw new Error(message);
67 });
68 },
69 async deleteMultipleDestinations({ dispatch }, destination) {
70 const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
71 const promises = destination.map(({ id }) => {
72 return api.delete(`${snmpAlertUrl}/${id}`).catch((error) => {
73 console.log(error);
74 return error;
75 });
76 });
77 return await api
78 .all(promises)
79 .then((response) => {
80 dispatch('getSnmpDetails');
81 return response;
82 })
83 .then(
84 api.spread((...responses) => {
85 const { successCount, errorCount } = getResponseCount(responses);
86 let toastMessages = [];
87
88 if (successCount) {
Surya Vde23ea22024-07-11 15:19:46 +053089 const message = i18n.global.t(
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000090 'pageSnmpAlerts.toast.successBatchDelete',
Ed Tanous81323992024-02-27 11:26:24 -080091 successCount,
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000092 );
93 toastMessages.push({ type: 'success', message });
94 }
95
96 if (errorCount) {
Surya Vde23ea22024-07-11 15:19:46 +053097 const message = i18n.global.t(
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +000098 'pageSnmpAlerts.toast.errorBatchDelete',
Ed Tanous81323992024-02-27 11:26:24 -080099 errorCount,
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +0000100 );
101 toastMessages.push({ type: 'error', message });
102 }
103
104 return toastMessages;
Ed Tanous81323992024-02-27 11:26:24 -0800105 }),
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +0000106 );
107 },
108 async addDestination({ dispatch }, { data }) {
109 const snmpAlertUrl = await dispatch('getSnmpAlertUrl');
110 return await api
111 .post(snmpAlertUrl, data)
112 .then(() => dispatch('getSnmpDetails'))
Surya Vde23ea22024-07-11 15:19:46 +0530113 .then(() => i18n.global.t('pageSnmpAlerts.toast.successAddDestination'))
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +0000114 .catch((error) => {
115 console.log(error);
Surya Vde23ea22024-07-11 15:19:46 +0530116 const message = i18n.global.t(
117 'pageSnmpAlerts.toast.errorAddDestination',
118 );
Konstantin Aladyshev7c1cfe72023-05-16 09:03:25 +0000119 throw new Error(message);
120 });
121 },
122 },
123};
124
125export default SnmpAlertsStore;