Skip to content

Commit

Permalink
Add custom charts management
Browse files Browse the repository at this point in the history
  • Loading branch information
marcdelalonde committed Apr 20, 2021
1 parent 66e21ef commit cdfa141
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const authController = require('./src/controllers/auth');
const installController = require('./src/controllers/install');
const configCtrl = require('./src/controllers/config');
const customActionsCtrl = require('./src/controllers/customactions');
const chartsCtrl = require('./src/controllers/charts');

const accessControl = (req, res, next) => {
const origin = global._amConfig.devMode ? 'http://localhost:3002' : 'https://my.adminmate.io';
Expand Down Expand Up @@ -60,6 +61,9 @@ const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, char
// Custom query
router.post(`${endpointPrefix}/query`, isAuthorizedIP, isAuthorized, api.modelCustomQuery);

// Execute custom charts
router.post(`${endpointPrefix}/charts/:chart_code`, isAuthorizedIP, isAuthorized, chartsCtrl.execute);

return router;
};

Expand Down
28 changes: 28 additions & 0 deletions src/controllers/charts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports.execute = (req, res) => {
const chartCode = req.params.chart_code;

if (!chartCode) {
return res.status(403).json({ message: 'Invalid request' });
}

const matchingChart = global._amConfig.charts
.find(chart => chart.code === chartCode);

if (!matchingChart) {
return res.status(403).json({ message: 'Invalid model' });
}

if (!matchingChart.handler) {
return res.status(403).json({ message: 'This custom chart does not have any handler function' });
}

matchingChart.handler(
req.body.data,
json => {
res.json(json || {});
},
json => {
res.status(403).json(json || {});
}
);
};

0 comments on commit cdfa141

Please sign in to comment.