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              sessionID: sessionUri.data?.Id,
30              context: sessionUri.data?.Context
31                ? sessionUri.data?.Context
32                : '-',
33              username: sessionUri.data?.UserName,
34              ipAddress: sessionUri.data?.ClientOriginIPAddress,
35              uri: sessionUri.data['@odata.id'],
36            };
37          });
38          commit('setAllConnections', allConnectionsData);
39        })
40        .catch((error) => {
41          console.log('Client Session Data:', error);
42        });
43    },
44    async disconnectSessions({ dispatch }, uris = []) {
45      const promises = uris.map((uri) =>
46        api.delete(uri).catch((error) => {
47          console.log(error);
48          return error;
49        }),
50      );
51      return await api
52        .all(promises)
53        .then((response) => {
54          dispatch('getSessionsData');
55          return response;
56        })
57        .then(
58          api.spread((...responses) => {
59            const { successCount, errorCount } = getResponseCount(responses);
60            const toastMessages = [];
61
62            if (successCount) {
63              const message = i18n.global.t(
64                'pageSessions.toast.successDelete',
65                successCount,
66              );
67              toastMessages.push({ type: 'success', message });
68            }
69
70            if (errorCount) {
71              const message = i18n.global.t(
72                'pageSessions.toast.errorDelete',
73                errorCount,
74              );
75              toastMessages.push({ type: 'error', message });
76            }
77            return toastMessages;
78          }),
79        );
80    },
81  },
82};
83export default SessionsStore;
84