1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const SystemStore = {
5  namespaced: true,
6  state: {
7    systems: [],
8  },
9  getters: {
10    systems: (state) => state.systems,
11  },
12  mutations: {
13    setSystemInfo: (state, data) => {
14      const system = {};
15      system.assetTag = data.AssetTag;
16      system.description = data.Description;
17      system.firmwareVersion = data.BiosVersion;
18      system.hardwareType = data.Name;
19      system.health = data.Status?.Health;
20      system.totalSystemMemoryGiB = data.MemorySummary?.TotalSystemMemoryGiB;
21      system.id = data.Id;
22      system.locationIndicatorActive = data.LocationIndicatorActive;
23      system.locationNumber = data.Location?.PartLocation?.ServiceLabel;
24      system.manufacturer = data.Manufacturer;
25      system.model = data.Model;
26      system.processorSummaryCount = data.ProcessorSummary?.Count;
27      system.processorSummaryCoreCount = data.ProcessorSummary?.CoreCount;
28      system.powerState = data.PowerState;
29      system.serialNumber = data.SerialNumber;
30      system.healthRollup = data.Status?.HealthRollup;
31      system.subModel = data.SubModel;
32      system.statusState = data.Status?.State;
33      system.systemType = data.SystemType;
34      state.systems = [system];
35    },
36  },
37  actions: {
38    async getSystem({ commit }) {
39      return await api
40        .get(`${await this.dispatch('global/getSystemPath')}`)
41        .then(({ data }) => commit('setSystemInfo', data))
42        .catch((error) => console.log(error));
43    },
44    async changeIdentifyLedState({ commit }, ledState) {
45      return await api
46        .patch(`${await this.dispatch('global/getSystemPath')}`, {
47          LocationIndicatorActive: ledState,
48        })
49        .then(() => {
50          if (ledState) {
51            return i18n.t('pageInventory.toast.successEnableIdentifyLed');
52          } else {
53            return i18n.t('pageInventory.toast.successDisableIdentifyLed');
54          }
55        })
56        .catch((error) => {
57          commit('setSystemInfo', this.state.system.systems[0]);
58          console.log('error', error);
59          if (ledState) {
60            throw new Error(
61              i18n.t('pageInventory.toast.errorEnableIdentifyLed'),
62            );
63          } else {
64            throw new Error(
65              i18n.t('pageInventory.toast.errorDisableIdentifyLed'),
66            );
67          }
68        });
69    },
70  },
71};
72
73export default SystemStore;
74