1import api, { getResponseCount } from '@/store/api';
2import i18n from '@/i18n';
3
4const SessionsStore = {
5  namespaced: true,
6  state: {
7    allConnections: [],
8  },
9  getters: {
10    allConnections: (state) => state.allConnections,
11  },
12  mutations: {
13    setAllConnections: (state, allConnections) =>
14      (state.allConnections = allConnections),
15  },
16  actions: {
17    async getSessionsData({ commit }) {
18      return await api
19        .get('/redfish/v1/SessionService/Sessions')
20        .then((response) =>
21          response.data.Members.map((sessionLogs) => sessionLogs['@odata.id'])
22        )
23        .then((sessionUris) =>
24          api.all(sessionUris.map((sessionUri) => api.get(sessionUri)))
25        )
26        .then((sessionUris) => {
27          const allConnectionsData = sessionUris.map((sessionUri) => {
28            return {
29              clientID: sessionUri.data?.Oem?.OpenBMC.ClientID,
30              username: sessionUri.data?.UserName,
31              ipAddress: sessionUri.data?.ClientOriginIPAddress,
32              uri: sessionUri.data['@odata.id'],
33            };
34          });
35          commit('setAllConnections', allConnectionsData);
36        })
37        .catch((error) => {
38          console.log('Client Session Data:', error);
39        });
40    },
41    async disconnectSessions({ dispatch }, uris = []) {
42      const promises = uris.map((uri) =>
43        api.delete(uri).catch((error) => {
44          console.log(error);
45          return error;
46        })
47      );
48      return await api
49        .all(promises)
50        .then((response) => {
51          dispatch('getSessionsData');
52          return response;
53        })
54        .then(
55          api.spread((...responses) => {
56            const { successCount, errorCount } = getResponseCount(responses);
57            const toastMessages = [];
58
59            if (successCount) {
60              const message = i18n.tc(
61                'pageSessions.toast.successDelete',
62                successCount
63              );
64              toastMessages.push({ type: 'success', message });
65            }
66
67            if (errorCount) {
68              const message = i18n.tc(
69                'pageSessions.toast.errorDelete',
70                errorCount
71              );
72              toastMessages.push({ type: 'error', message });
73            }
74            return toastMessages;
75          })
76        );
77    },
78  },
79};
80export default SessionsStore;
81