import api from '@/store/api'; import i18n from '@/i18n'; const SmtpStore = { namespaced: true, state: { settings: {}, subscribers: [], }, getters: { settings: (state) => state.settings, subscribers: (state) => state.subscribers, }, mutations: { saveSettings: (state, data) => (state.settings = data), setSubscribers: (state, data) => (state.subscribers = data), }, actions: { async deleteSubscriber({ dispatch }, email) { return await api .get(`/redfish/v1/Smtp/DeleteMails&${email}`) .then(async () => { await dispatch('getSubscribers'); return i18n.t('pageTransfer.smtp.deleteSubscriberSuсcess'); }) .catch((error) => { console.log(error); throw new Error(i18n.t('pageTransfer.smtp.deleteSubscriberError')); }); }, async addSubscriber({ dispatch }, payload) { return await api .get(`/redfish/v1/Smtp/AddMails&${payload.email}`) .then(async () => { await dispatch('getSubscribers'); return i18n.t('pageTransfer.smtp.saveSubscriberSuсcess'); }) .catch((error) => { console.log(error); throw new Error(i18n.t('pageTransfer.smtp.saveSubscriberError')); }); }, async getSubscribers({ commit }) { return await api .get('/redfish/v1/Smtp/GetMails') .then(({ data: { mails = [] } = {} }) => mails.map((host) => { return { host, }; }) ) .then((subscribers) => commit('setSubscribers', subscribers)) .catch((error) => { console.log(error); throw new Error(i18n.t('pageUserManagement.toast.errorLoadUsers')); }); }, async setSettings({ dispatch }, payload) { let url = `/redfish/v1/Smtp/SetSettings`; for (let key in payload) { url += `&${key}=${payload[key]}`; } return await api .get(url) .then(async () => { await dispatch('getSettings'); return i18n.t('pageTransfer.saveSmtpSuсcess'); }) .catch((error) => { console.log(error); throw new Error(i18n.t('pageTransfer.saveSmtpError')); }); }, async getSettings({ commit }) { return await api .get('/redfish/v1/Smtp/GetSettings') .then(({ data = {} }) => { commit('saveSettings', data); }) .catch((error) => console.log(error)); }, async sendTestMessage(_, payload) { let url = `/redfish/v1/Smtp/SendMail`; for (let key in payload) { url += `&${key}=${payload[key]}`; } return await api.get(url); }, }, }; export default SmtpStore;