Skip to content

Commit

Permalink
refactor: added auto-capture alert url
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrishimeena committed Aug 30, 2024
1 parent d58adc6 commit 7c365cb
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 20 deletions.
56 changes: 56 additions & 0 deletions lib/main/server/controllers/appController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
]
});
}
};
2 changes: 2 additions & 0 deletions lib/main/server/routes/apiRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions lib/web/assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions lib/web/src/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};
}
95 changes: 75 additions & 20 deletions lib/web/src/components/Integrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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;
Expand All @@ -327,13 +371,14 @@ class Integrations extends React.Component {
}

const filteredOptions = allUsers.filter(o => !selectedUsers.includes(o));
const antIcon = <Icon type='loading' style={{ fontSize: 30 }} spin />;

return (
<>
<Row>
<Col span={20} offset={2}>
<Content className='all-users'>
<Content className='setting-user'>
<Content className='all-intergrations'>
<Content>
<h1>Integrations</h1>
<Divider />
<Tabs tabPosition='left' className='integrations'>
Expand Down Expand Up @@ -382,6 +427,16 @@ class Integrations extends React.Component {
</TabPane>
</Tabs>
</Content>
<br /><br />
<Divider />
<Spin indicator={antIcon} spinning={alertUrlLoading} delay={100}>
<Form layout='horizontal' style={{ textAlign: 'center' }}>
<Form.Item>
<b>Alert URL:</b> <Input value={alertUrl} style={{ width: '400px', cursor: 'not-allowed' }} name='Alert URL' placeholder='Alert URL' autocomplete='off' />
<Button style={{ marginLeft: '20px' }} loading={formLoader} type='primary' onClick={this.addAlertUrl.bind(this)}>Use current domain</Button>
</Form.Item>
</Form>
</Spin>
</Content>
</Col>
</Row>
Expand Down
9 changes: 9 additions & 0 deletions lib/web/src/services/httpServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('&');
Expand Down

0 comments on commit 7c365cb

Please sign in to comment.