From 7c365cbd1abc15890011a5188659d3ca6e756971 Mon Sep 17 00:00:00 2001 From: Rishi Kumar <1720744+mrrishimeena@users.noreply.github.com> Date: Fri, 30 Aug 2024 12:59:12 +0530 Subject: [PATCH] refactor: added auto-capture alert url --- lib/main/server/controllers/appController.js | 56 ++++++++++++ lib/main/server/routes/apiRoutes.js | 2 + lib/web/assets/css/app.css | 8 ++ lib/web/src/actions/appActions.js | 38 ++++++++ lib/web/src/components/Integrations.js | 95 +++++++++++++++----- lib/web/src/services/httpServices.js | 9 ++ 6 files changed, 188 insertions(+), 20 deletions(-) diff --git a/lib/main/server/controllers/appController.js b/lib/main/server/controllers/appController.js index 8cacd3a..ff99c58 100644 --- a/lib/main/server/controllers/appController.js +++ b/lib/main/server/controllers/appController.js @@ -406,3 +406,59 @@ exports.testEmailNotification = async (req, res) => { }); } }; + +exports.getAlertUrlDetails = async (req, res) => { + try { + const storageConnection = getStorageConnection(); + const data = await storageConnection.getConfig('alertUrl'); + if (data && data.item) { + data.item.value = JSON.parse(data.item.value); + } + res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, data.item || {})); + } catch (error) { + console.error(error); + res.status(500).send({ + errors: [ + { + error: 'Internal Server Error', + message: 'An unexpected error occurred' + } + ] + }); + } +}; + +exports.addAlertUrlDetails = async (req, res) => { + try { + const { url } = helpers.extractAttributes(req.body); + const storageConnection = getStorageConnection(); + const details = { url }; + const result = await storageConnection.setConfig( + 'alertUrl', + JSON.stringify(details) + ); + if (result && result.item) { + result.item.value = JSON.parse(result.item.value); + res.send(Jsonapi.Serializer.serialize(Jsonapi.AppType, result.item)); + } else { + res.status(500).send({ + errors: [ + { + error: 'Internal Server Error', + message: 'An unexpected error occurred' + } + ] + }); + } + } catch (error) { + console.error(error); + res.status(500).send({ + errors: [ + { + error: 'Internal Server Error', + message: 'An unexpected error occurred' + } + ] + }); + } +}; diff --git a/lib/main/server/routes/apiRoutes.js b/lib/main/server/routes/apiRoutes.js index a6df383..fb6226f 100644 --- a/lib/main/server/routes/apiRoutes.js +++ b/lib/main/server/routes/apiRoutes.js @@ -30,6 +30,8 @@ router.post('/api/apps/integrations/email', auth.authenticateTokenWithAdmin, app router.patch('/api/apps/integrations/email', auth.authenticateTokenWithAdmin, appController.updateEmailDetails); router.delete('/api/apps/integrations/email', auth.authenticateTokenWithAdmin, appController.deleteEmailDetails); router.post('/api/apps/integrations/email/test', auth.authenticateTokenWithAdmin, appController.testEmailNotification); +router.get('/api/apps/integrations/alert-url', auth.authenticateTokenWithAdmin, appController.getAlertUrlDetails); +router.post('/api/apps/integrations/alert-url', auth.authenticateTokenWithAdmin, appController.addAlertUrlDetails); router.get('/api/logs', auth.authenticateToken, logController.getLogs); router.get('/api/logs/hostnames', auth.authenticateToken, logController.getHostnames); diff --git a/lib/web/assets/css/app.css b/lib/web/assets/css/app.css index 514f247..e381eac 100644 --- a/lib/web/assets/css/app.css +++ b/lib/web/assets/css/app.css @@ -219,6 +219,14 @@ body { border-radius: 5px; } +.all-intergrations { + background: white; + padding: 25px; + margin-top: 70px; + border: 1px solid #dedede; + border-radius: 5px; +} + .submenu ul { background: #001628 !important; box-shadow: none !important; diff --git a/lib/web/src/actions/appActions.js b/lib/web/src/actions/appActions.js index 3c8b417..ace3c28 100644 --- a/lib/web/src/actions/appActions.js +++ b/lib/web/src/actions/appActions.js @@ -222,3 +222,41 @@ export function clearConsoleLogs () { type: 'CLEAR_CONSOLE_LOGS' }; } + +export function getAlertUrl (callback) { + return (dispatch) => { + http.getAlertUrl() + .then((response) => { + const details = response.data; + callback(null, details); + }) + .catch((error) => { + console.log(error); + if (error.response) { + Notifications.showErrors(error); + } else { + Notifications.showErrors(error); + } + callback(error); + }); + }; +} + +export function addAlertUrl (data, callback) { + return (dispatch) => { + http.addAlertUrl(data) + .then((response) => { + const details = response.data; + callback(null, details); + }) + .catch((error) => { + console.log(error); + if (error.response) { + Notifications.showErrors(error); + } else { + Notifications.showErrors(error); + } + callback(error); + }); + }; +} diff --git a/lib/web/src/components/Integrations.js b/lib/web/src/components/Integrations.js index f06aa4d..5aeac7a 100644 --- a/lib/web/src/components/Integrations.js +++ b/lib/web/src/components/Integrations.js @@ -8,7 +8,7 @@ import * as appActions from 'actions/appActions.js'; import * as userActions from 'actions/userActions.js'; /* Ante UI */ -import { Layout, Divider, Form, Input, Row, Col, Button, Icon, Tabs, Result, Select, message } from 'antd'; +import { Layout, Divider, Form, Input, Row, Col, Button, Icon, Tabs, Result, Select, Spin, message } from 'antd'; const { Content } = Layout; const { TabPane } = Tabs; @@ -39,23 +39,7 @@ class Integrations extends React.Component { this.getSlackDetails(); this.getEmailDetails(); this.getAllUsers(); - this.getAlertURL(); - } - - getAlertURL () { - // const self = this; - // this.props.appActions.getAlertURL(function (err, response) { - // if (!err && response) { - // const users = response.map((user) => { return user.attributes.url; }); - // self.setState({ - // allUsers: users - // }); - // } else { - // self.setState({ - // loadingStatus: false - // }); - // } - // }); + this.getAlertUrl(); } getAllUsers () { @@ -300,11 +284,71 @@ class Integrations extends React.Component { } } + getAlertUrl () { + const self = this; + this.setState({ + alertUrlLoading: true + }); + this.props.appActions.getAlertUrl(function (err, result) { + if (!err) { + if (result.data) { + const alertUrlValue = result.data.attributes.value; + if (alertUrlValue) { + const alertUrl = alertUrlValue.url; + self.setState({ + alertUrl + }); + } + } else { + self.addAlertUrl(); + } + } else { + message.error('Failed to retrieve the alert url'); + } + self.setState({ + alertUrlLoading: false + }); + }); + } + + addAlertUrl () { + const self = this; + this.setState({ + alertUrlLoading: true + }); + const baseUrl = window.location.origin + window.location.pathname; + const data = { + url: baseUrl + }; + this.props.appActions.addAlertUrl(data, function (err, result) { + if (!err) { + if (result.data) { + const alertUrlValue = result.data.attributes.value; + if (alertUrlValue) { + const alertUrl = alertUrlValue.url; + self.setState({ + alertUrl + }); + } + } else { + message.error('Failed to set the alert url'); + } + } else { + message.error('Failed to retrieve the alert url'); + } + self.setState({ + alertUrlLoading: false + }); + }); + } + render () { const slackWebhookURL = this.state.slackWebhookURL || ''; const formLoader = this.state.formLoader || false; const slackDetails = this.state.slackDetails || null; const emailDetails = this.state.emailDetails || null; + const alertUrl = this.state.alertUrl || null; + const alertUrlLoading = this.state.alertUrlLoading || false; const sender = this.state.sender !== undefined ? this.state.sender : emailDetails?.sender; const host = this.state.host !== undefined ? this.state.host : emailDetails?.host; @@ -327,13 +371,14 @@ class Integrations extends React.Component { } const filteredOptions = allUsers.filter(o => !selectedUsers.includes(o)); + const antIcon = ; return ( <> - - + +

Integrations

@@ -382,6 +427,16 @@ class Integrations extends React.Component {
+

+ + +
+ + Alert URL: + + +
+
diff --git a/lib/web/src/services/httpServices.js b/lib/web/src/services/httpServices.js index 6eaa61b..0f4caa8 100644 --- a/lib/web/src/services/httpServices.js +++ b/lib/web/src/services/httpServices.js @@ -164,6 +164,15 @@ http.testEmailNotification = async () => { return axios.post(`${endPoints.base}/apps/integrations/email/test`); }; +http.getAlertUrl = async () => { + return axios.get(`${endPoints.base}/apps/integrations/alert-url`); +}; + +http.addAlertUrl = async (urlData) => { + const data = format(urlData, 'apps'); + return axios.post(`${endPoints.base}/apps/integrations/alert-url`, data); +}; + function makeQueryRequest (query) { if (query && Object.keys(query).length !== 0) { const queryRequest = Object.keys(query).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(query[key])}`).join('&');