adminmate-express-mongoose 1.3.3 → 1.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,23 +1,39 @@
1
- const { init, isAuthorized } = require(global.AM_DEV_MODE ? '../adminmate-express-core' : 'adminmate-express-core');
2
-
3
- // Helpers
4
- const fnHelper = require('./src/helpers/functions');
5
-
6
- // CRUD
7
- const { getAll } = require('./src/controllers/model-getall');
8
- const { getIn } = require('./src/controllers/model-getin');
9
- const { getOne } = require('./src/controllers/model-getone');
10
- const { getRefs } = require('./src/controllers/model-getrefs');
11
- const { postOne } = require('./src/controllers/model-postone');
12
- const { putOne } = require('./src/controllers/model-putone');
13
- const { deleteSome } = require('./src/controllers/model-deletesome');
14
- const { getAutocomplete } = require('./src/controllers/model-autocomplete');
15
- const { customQuery } = require('./src/controllers/model-query');
16
-
17
- const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, charts, authorizedIps }) => {
1
+ const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, charts, authorizedIps, devMode = false, testMode = false }) => {
2
+ const _conf = {};
3
+ _conf.projectId = projectId;
4
+ _conf.secretKey = secretKey;
5
+ _conf.authKey = authKey;
6
+ _conf.masterPassword = masterPassword;
7
+ _conf.models = models || [];
8
+ _conf.charts = charts || [];
9
+ _conf.authorizedIps = authorizedIps || null;
10
+ _conf.devMode = devMode;
11
+ _conf.testMode = testMode;
12
+
13
+ const amCore = require(_conf.devMode ? '../adminmate-express-core' : 'adminmate-express-core');
14
+
15
+ // Helpers
16
+ const fnHelper = require('./src/helpers/functions')(_conf);
17
+
18
+ // CRUD
19
+ const getAll = require('./src/controllers/model-getall')(_conf);
20
+ const getIn = require('./src/controllers/model-getin')(_conf);
21
+ const getOne = require('./src/controllers/model-getone')(_conf);
22
+ const getRefs = require('./src/controllers/model-getrefs')(_conf);
23
+ const postOne = require('./src/controllers/model-postone')(_conf);
24
+ const putOne = require('./src/controllers/model-putone')(_conf);
25
+ const deleteSome = require('./src/controllers/model-deletesome')(_conf);
26
+ const getAutocomplete = require('./src/controllers/model-autocomplete')(_conf);
27
+ const customQuery = require('./src/controllers/model-query')(_conf);
28
+
18
29
  const api = {
19
30
  // App config
20
31
  getAppConfig: fnHelper.getAppConfig,
32
+ getAvailableFeatures: () => {
33
+ return {
34
+ charts: ['single_value', 'pie', 'bar', 'line', 'objective', 'ranking']
35
+ };
36
+ },
21
37
 
22
38
  // General
23
39
  getModelProperties: fnHelper.getModelProperties,
@@ -38,19 +54,16 @@ const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, char
38
54
  modelCustomQuery: customQuery
39
55
  };
40
56
 
41
- return init({
42
- projectId,
43
- secretKey,
44
- authKey,
45
- masterPassword,
46
- models,
47
- charts,
48
- authorizedIps,
57
+ if (testMode === true) {
58
+ return api;
59
+ }
60
+
61
+ return amCore.init({
62
+ config: _conf,
49
63
  api
50
64
  });
51
65
  };
52
66
 
53
67
  module.exports = {
54
- init: Adminmate,
55
- isAuthorized,
56
- };
68
+ init: Adminmate
69
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adminmate-express-mongoose",
3
- "version": "1.3.3",
3
+ "version": "1.3.4",
4
4
  "description": "Adminmate Express/Mongoose connector",
5
5
  "author": "Marc Delalonde",
6
6
  "homepage": "http://adminmate.io",
@@ -23,8 +23,8 @@
23
23
  "url": "https://github.com/Adminmate/adminmate-express-mongoose.git"
24
24
  },
25
25
  "dependencies": {
26
+ "adminmate-express-core": "~1.2.1",
26
27
  "joi": "^17.6.0",
27
- "adminmate-express-core": "~1.2.0",
28
28
  "lodash": "^4.17.21",
29
29
  "moment": "^2.29.1",
30
30
  "mongoose": "~5.9.7",
@@ -1,64 +1,86 @@
1
1
  const Joi = require('joi');
2
2
 
3
- module.exports = async (currentModel, data) => {
4
- const paramsSchema = Joi.object({
5
- type: Joi.string().required(),
6
- model: Joi.string().required(),
7
- operation: Joi.string().required(),
8
- group_by: Joi.string().required(),
9
- field: Joi.alternatives().conditional('operation', {
10
- not: 'count',
11
- then: Joi.string().required(),
12
- otherwise: Joi.string().optional()
13
- }),
14
- limit: Joi.number().optional()
15
- });
3
+ module.exports = _conf => {
4
+ const fnHelper = require('../helpers/functions')(_conf);
16
5
 
17
- // Validate params
18
- const { error } = paramsSchema.validate(data);
19
- if (error) {
20
- return {
21
- success: false,
22
- message: error.details[0].message
23
- };
24
- }
6
+ const chartPie = async (currentModel, data) => {
7
+ const paramsSchema = Joi.object({
8
+ type: Joi.string().required(),
9
+ model: Joi.string().required(),
10
+ operation: Joi.string().required(),
11
+ group_by: Joi.string().required(),
12
+ field: Joi.alternatives().conditional('operation', {
13
+ not: 'count',
14
+ then: Joi.string().required(),
15
+ otherwise: Joi.string().optional()
16
+ }),
17
+ limit: Joi.number().optional(),
18
+ filters: Joi.object({
19
+ operator: Joi.string().valid('and', 'or').required(),
20
+ list: Joi.array().required()
21
+ })
22
+ });
25
23
 
26
- let _value = 1;
27
- if (data.field && ['sum', 'avg'].includes(data.operation)) {
28
- _value = `$${data.field}`;
29
- }
24
+ // Validate params
25
+ const { error } = paramsSchema.validate(data);
26
+ if (error) {
27
+ return {
28
+ success: false,
29
+ message: error.details[0].message
30
+ };
31
+ }
30
32
 
31
- try {
32
- const repartitionData = await currentModel
33
- .aggregate([
34
- {
35
- $group: {
36
- _id: `$${data.group_by}`,
37
- count: data.operation === 'avg' ? { $avg: _value } : { $sum: _value },
38
- }
39
- },
40
- {
41
- $project: {
42
- key: '$_id',
43
- value: '$count',
44
- _id: false
33
+ let _value = 1;
34
+ if (data.field && ['sum', 'avg'].includes(data.operation)) {
35
+ _value = `$${data.field}`;
36
+ }
37
+
38
+ // Filters
39
+ let findParams = {};
40
+ if (data.filters) {
41
+ const filtersQuery = fnHelper.constructQuery(data.filters);
42
+ if (filtersQuery) {
43
+ findParams = filtersQuery;
44
+ }
45
+ }
46
+
47
+ try {
48
+ const repartitionData = await currentModel
49
+ .aggregate([
50
+ {
51
+ $match: findParams
52
+ },
53
+ {
54
+ $group: {
55
+ _id: `$${data.group_by}`,
56
+ count: data.operation === 'avg' ? { $avg: _value } : { $sum: _value },
57
+ }
58
+ },
59
+ {
60
+ $project: {
61
+ key: '$_id',
62
+ value: '$count',
63
+ _id: false
64
+ }
45
65
  }
66
+ ])
67
+ .sort({ key: 1 });
68
+
69
+ return {
70
+ success: true,
71
+ data: {
72
+ config: null,
73
+ data: repartitionData
46
74
  }
47
- ])
48
- .sort({ key: 1 });
75
+ };
76
+ }
77
+ catch(e) {
78
+ return {
79
+ success: false,
80
+ message: e.message
81
+ };
82
+ }
83
+ };
49
84
 
50
- return {
51
- success: true,
52
- data: {
53
- config: null,
54
- data: repartitionData
55
- }
56
- };
57
- }
58
- catch(e) {
59
- return {
60
- success: false,
61
- message: e.message
62
- };
63
- }
64
- };
85
+ return chartPie;
86
+ };
@@ -1,89 +1,110 @@
1
1
  const Joi = require('joi');
2
- const fnHelper = require('../helpers/functions');
3
2
 
4
- module.exports = async (currentModel, data) => {
5
- const paramsSchema = Joi.object({
6
- type: Joi.string().required(),
7
- model: Joi.string().required(),
8
- field: Joi.string().required(),
9
- relationship_model: Joi.string().required(),
10
- relationship_model_ref_field: Joi.string().required(),
11
- relationship_operation: Joi.string().required(),
12
- relationship_field: Joi.alternatives().conditional('relationship_operation', {
13
- not: 'count',
14
- then: Joi.string().required(),
15
- otherwise: Joi.string()
16
- }),
17
- limit: Joi.number().optional(),
18
- });
3
+ module.exports = _conf => {
4
+ const fnHelper = require('../helpers/functions')(_conf);
19
5
 
20
- // Validate params
21
- const { error } = paramsSchema.validate(data);
22
- if (error) {
23
- return {
24
- success: false,
25
- message: error.details[0].message
26
- };
27
- }
6
+ const chartRanking = async (currentModel, data) => {
7
+ const paramsSchema = Joi.object({
8
+ type: Joi.string().required(),
9
+ model: Joi.string().required(),
10
+ field: Joi.string().required(),
11
+ relationship_model: Joi.string().required(),
12
+ relationship_model_ref_field: Joi.string().required(),
13
+ relationship_operation: Joi.string().required(),
14
+ relationship_field: Joi.alternatives().conditional('relationship_operation', {
15
+ not: 'count',
16
+ then: Joi.string().required(),
17
+ otherwise: Joi.string()
18
+ }),
19
+ limit: Joi.number().optional(),
20
+ filters: Joi.object({
21
+ operator: Joi.string().valid('and', 'or').required(),
22
+ list: Joi.array().required()
23
+ })
24
+ });
28
25
 
29
- // Get relationship model
30
- const relationshipModel = fnHelper.getModelObject(data.relationship_model);
31
- if (!relationshipModel) {
32
- return res.status(403).json({ message: 'Invalid request' });
33
- }
26
+ // Validate params
27
+ const { error } = paramsSchema.validate(data);
28
+ if (error) {
29
+ return {
30
+ success: false,
31
+ message: error.details[0].message
32
+ };
33
+ }
34
34
 
35
- // Default limit
36
- let limit = data.limit || 10;
35
+ // Get relationship model
36
+ const relationshipModel = fnHelper.getModelObject(data.relationship_model);
37
+ if (!relationshipModel) {
38
+ return res.status(403).json({ message: 'Invalid request' });
39
+ }
37
40
 
38
- let _value = 1;
39
- if (data.relationship_field && ['sum', 'avg'].includes(data.relationship_operation)) {
40
- _value = `$${data.relationship_field}`;
41
- }
41
+ // Default limit
42
+ let limit = data.limit || 10;
42
43
 
43
- try {
44
- const repartitionData = await relationshipModel
45
- .aggregate([
46
- {
47
- $group: {
48
- _id: `$${data.relationship_model_ref_field}`,
49
- count: data.operation === 'avg' ? { $avg: _value } : { $sum: _value },
50
- }
51
- },
52
- {
53
- $project: {
54
- key: '$_id',
55
- value: '$count',
56
- _id: false
44
+ let _value = 1;
45
+ if (data.relationship_field && ['sum', 'avg'].includes(data.relationship_operation)) {
46
+ _value = `$${data.relationship_field}`;
47
+ }
48
+
49
+ // Filters
50
+ let findParams = {};
51
+ if (data.filters) {
52
+ const filtersQuery = fnHelper.constructQuery(data.filters);
53
+ if (filtersQuery) {
54
+ findParams = filtersQuery;
55
+ }
56
+ }
57
+
58
+ try {
59
+ const repartitionData = await relationshipModel
60
+ .aggregate([
61
+ {
62
+ $match: findParams
63
+ },
64
+ {
65
+ $group: {
66
+ _id: `$${data.relationship_model_ref_field}`,
67
+ count: data.operation === 'avg' ? { $avg: _value } : { $sum: _value },
68
+ }
69
+ },
70
+ {
71
+ $project: {
72
+ key: '$_id',
73
+ value: '$count',
74
+ _id: false
75
+ }
57
76
  }
58
- }
59
- ])
60
- .limit(limit)
61
- .sort({ value: -1 });
77
+ ])
78
+ .limit(limit)
79
+ .sort({ value: -1 });
62
80
 
63
- const parentIds = repartitionData.map(d => d.key);
64
- const parentData = await currentModel.find({ _id: parentIds }).select(data.field).lean();
81
+ const parentIds = repartitionData.map(d => d.key);
82
+ const parentData = await currentModel.find({ _id: parentIds }).select(data.field).lean();
65
83
 
66
- repartitionData.forEach(d => {
67
- d.item_model = data.model;
68
- d.item_id = d.key;
69
- const parent = parentData.find(p => p._id.toString() === d.key.toString());
70
- if (parent) {
71
- d.key = parent[data.field];
72
- }
73
- });
84
+ repartitionData.forEach(d => {
85
+ d.item_model = data.model;
86
+ d.item_id = d.key;
87
+ const parent = parentData.find(p => p._id.toString() === d.key.toString());
88
+ if (parent) {
89
+ d.key = parent[data.field];
90
+ }
91
+ });
74
92
 
75
- return {
76
- success: true,
77
- data: {
78
- config: null,
79
- data: repartitionData
80
- }
81
- };
82
- }
83
- catch(e) {
84
- return {
85
- success: false,
86
- message: e.message
87
- };
88
- }
89
- };
93
+ return {
94
+ success: true,
95
+ data: {
96
+ config: null,
97
+ data: repartitionData
98
+ }
99
+ };
100
+ }
101
+ catch(e) {
102
+ return {
103
+ success: false,
104
+ message: e.message
105
+ };
106
+ }
107
+ };
108
+
109
+ return chartRanking;
110
+ };