Skip to content

Commit

Permalink
General enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
marcdelalonde committed Apr 19, 2021
1 parent 792c2d5 commit 66e21ef
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 74 deletions.
17 changes: 6 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ const { isAuthorized, isAuthorizedIP } = require('./src/middlewares/auth');
// Controllers
const authController = require('./src/controllers/auth');
const installController = require('./src/controllers/install');
const modelsCtrl = require('./src/controllers/models');
const configCtrl = require('./src/controllers/config');
const customActionsCtrl = require('./src/controllers/customactions');
const segmentsCtrl = require('./src/controllers/segments');

const accessControl = (req, res, next) => {
const origin = global._amConfig.devMode ? 'http://localhost:3002' : 'https://my.adminmate.io';
Expand All @@ -22,13 +21,14 @@ const accessControl = (req, res, next) => {
// Endpoints prefix
const endpointPrefix = '/adminmate/api';

const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, authorizedIps, api }) => {
const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, charts, authorizedIps, api }) => {
global._amConfig = {};
global._amConfig.projectId = projectId;
global._amConfig.secretKey = secretKey;
global._amConfig.authKey = authKey;
global._amConfig.masterPassword = masterPassword;
global._amConfig.models = models;
global._amConfig.models = models || [];
global._amConfig.charts = charts || [];
global._amConfig.authorizedIps = authorizedIps || null;
global._amConfig.devMode = !!global.AM_DEV_MODE;

Expand All @@ -42,18 +42,13 @@ const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, auth
// Login
router.post(`${endpointPrefix}/login`, isAuthorizedIP, authController.login);

// Models
router.get(`${endpointPrefix}/models`, isAuthorizedIP, isAuthorized, modelsCtrl.getModels(api));
router.get(`${endpointPrefix}/models/properties`, isAuthorizedIP, isAuthorized, modelsCtrl.getModelsProperties(api));
// Config
router.get(`${endpointPrefix}/config`, isAuthorizedIP, isAuthorized, configCtrl.getConfig(api));

// Custom Actions
router.get(`${endpointPrefix}/models/customactions`, isAuthorizedIP, isAuthorized, customActionsCtrl.getAll(api));
router.get(`${endpointPrefix}/models/:model/customactions`, isAuthorizedIP, isAuthorized, customActionsCtrl.getMatching(api));
router.post(`${endpointPrefix}/models/:model/customactions/:ca`, isAuthorizedIP, isAuthorized, customActionsCtrl.execute);

// Segments
router.get(`${endpointPrefix}/models/segments`, isAuthorizedIP, isAuthorized, segmentsCtrl.getAll);

// CRUD endpoints
router.post(`${endpointPrefix}/models/:model`, isAuthorizedIP, isAuthorized, api.modelGetAll);
router.post(`${endpointPrefix}/models/:model/autocomplete`, isAuthorizedIP, isAuthorized, api.modelGetAutocomplete);
Expand Down
40 changes: 18 additions & 22 deletions src/controllers/models.js → src/controllers/config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
module.exports.getModelsProperties = api => {
module.exports.getConfig = api => {
return (req, res) => {
const modelsProperties = [];

global._amConfig.models.forEach(modelConfig => {
const modelProperties = api.getModelProperties(modelConfig.model);
modelProperties.map(property => {
modelsProperties.push({
model: modelConfig.slug,
path: property.path
});
});
});

res.json({ properties: modelsProperties });
};
};

module.exports.getModels = api => {
return (req, res) => {
let models = [];

// Models list
const models = [];
global._amConfig.models.forEach(modelConfig => {
const modelObject = {
slug: modelConfig.slug,
Expand All @@ -42,6 +24,20 @@ module.exports.getModels = api => {
models.push(modelObject);
});

res.json({ models });
// Charts list
const charts = [];
if (global._amConfig.charts.length) {
global._amConfig.charts.map(chartConfig => {
charts.push({
code: chartConfig.code,
label: chartConfig.label
});
});
}

res.json({
models,
charts
});
};
};
21 changes: 0 additions & 21 deletions src/controllers/customactions.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
const fnHelper = require('../helpers/functions');

module.exports.getAll = api => {
return (req, res) => {
const list = [];

global._amConfig.models.forEach(modelConfig => {
const currentModelCustomActions = fnHelper.getModelCustomActions(modelConfig.slug);
if (currentModelCustomActions && currentModelCustomActions.length) {
currentModelCustomActions.map(sa => {
list.push({
model: modelConfig.slug,
label: sa.label,
code: sa.code
});
});
}
});

res.json({ list });
};
};

module.exports.getMatching = api => {
return async (req, res) => {
const modelName = req.params.model;
Expand Down
20 changes: 0 additions & 20 deletions src/controllers/segments.js

This file was deleted.

0 comments on commit 66e21ef

Please sign in to comment.