1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const ChassisStore = {
5  namespaced: true,
6  state: {
7    chassis: [],
8  },
9  getters: {
10    chassis: (state) => state.chassis,
11  },
12  mutations: {
13    setChassisInfo: (state, data) => {
14      state.chassis = data.map((chassis) => {
15        const {
16          Id,
17          Status = {},
18          PartNumber,
19          SerialNumber,
20          ChassisType,
21          Manufacturer,
22          PowerState,
23          LocationIndicatorActive,
24          AssetTag,
25          Model,
26          MaxPowerWatts,
27          MinPowerWatts,
28          Name,
29          Location,
30        } = chassis;
31
32        return {
33          id: Id,
34          health: Status.Health,
35          partNumber: PartNumber,
36          serialNumber: SerialNumber,
37          chassisType: ChassisType,
38          manufacturer: Manufacturer,
39          powerState: PowerState,
40          statusState: Status.State,
41          healthRollup: Status.HealthRollup,
42          assetTag: AssetTag,
43          model: Model,
44          maxPowerWatts: MaxPowerWatts,
45          minPowerWatts: MinPowerWatts,
46          name: Name,
47          identifyLed: LocationIndicatorActive,
48          uri: chassis['@odata.id'],
49          locationNumber: Location?.PartLocation?.ServiceLabel,
50        };
51      });
52    },
53  },
54  actions: {
55    async getChassisInfo({ commit }) {
56      return await api
57        .get('/redfish/v1/Chassis')
58        .then(({ data: { Members = [] } }) =>
59          Members.map((member) => api.get(member['@odata.id'])),
60        )
61        .then((promises) => api.all(promises))
62        .then((response) => {
63          const data = response.map(({ data }) => data);
64          commit('setChassisInfo', data);
65        })
66        .catch((error) => console.log(error));
67    },
68    async updateIdentifyLedValue({ dispatch }, led) {
69      const uri = led.uri;
70      const updatedIdentifyLedValue = {
71        LocationIndicatorActive: led.identifyLed,
72      };
73      return await api
74        .patch(uri, updatedIdentifyLedValue)
75        .then(() => {
76          dispatch('getChassisInfo');
77          if (led.identifyLed) {
78            return i18n.global.t(
79              'pageInventory.toast.successEnableIdentifyLed',
80            );
81          } else {
82            return i18n.global.t(
83              'pageInventory.toast.successDisableIdentifyLed',
84            );
85          }
86        })
87        .catch((error) => {
88          dispatch('getChassisInfo');
89          console.log('error', error);
90          if (led.identifyLed) {
91            throw new Error(
92              i18n.global.t('pageInventory.toast.errorEnableIdentifyLed'),
93            );
94          } else {
95            throw new Error(
96              i18n.global.t('pageInventory.toast.errorDisableIdentifyLed'),
97            );
98          }
99        });
100    },
101  },
102};
103
104export default ChassisStore;
105