adminmate-express-mongoose 1.3.3 → 1.3.5
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 +41 -28
- package/package.json +2 -2
- package/src/controllers/chart-pie.js +78 -56
- package/src/controllers/chart-ranking.js +99 -78
- package/src/controllers/chart-time.js +143 -127
- package/src/controllers/chart-value.js +104 -69
- package/src/controllers/model-autocomplete.js +52 -47
- package/src/controllers/model-deletesome.js +21 -18
- package/src/controllers/model-getall.js +123 -96
- package/src/controllers/model-getin.js +16 -12
- package/src/controllers/model-getone.js +41 -38
- package/src/controllers/model-getrefs.js +54 -50
- package/src/controllers/model-postone.js +25 -22
- package/src/controllers/model-putone.js +43 -38
- package/src/controllers/model-query.js +53 -49
- package/src/helpers/functions.js +453 -420
|
@@ -1,105 +1,132 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const keys = fnHelper.getModelProperties(currentModel);
|
|
24
|
-
|
|
25
|
-
// Ordering config
|
|
26
|
-
const orderConfig = fnHelper.validateOrderStructure(order) ? order : defaultOrdering;
|
|
27
|
-
const orderSafe = fnHelper.getCleanOrderStructure(orderConfig);
|
|
28
|
-
|
|
29
|
-
// Construct default fields to fetch
|
|
30
|
-
const defaultFieldsToFetch = keys
|
|
31
|
-
.filter(key => !key.path.includes('.'))
|
|
32
|
-
.map(key => key.path);
|
|
33
|
-
const fieldsToFetchSafe = Array.isArray(fieldsToFetch) && fieldsToFetch.length ? fieldsToFetch : defaultFieldsToFetch;
|
|
34
|
-
|
|
35
|
-
// Construct default fields to search in (only String type)
|
|
36
|
-
const defaultFieldsToSearchIn = keys
|
|
37
|
-
.filter(key => ['String'].includes(key.type))
|
|
38
|
-
.map(key => key.path);
|
|
39
|
-
const fieldsToSearchInSafe = Array.isArray(fieldsToSearchIn) && fieldsToSearchIn.length ? fieldsToSearchIn : defaultFieldsToSearchIn;
|
|
40
|
-
|
|
41
|
-
// Build ref fields for the model (for mongoose population purpose)
|
|
42
|
-
const fieldsToPopulate = fnHelper.getFieldsToPopulate(keys, fieldsToFetchSafe, refFields);
|
|
43
|
-
|
|
44
|
-
const queriesArray = [];
|
|
45
|
-
|
|
46
|
-
// Search -----------------------------------------------------------------------------
|
|
47
|
-
|
|
48
|
-
if (search) {
|
|
49
|
-
const searchQuery = fnHelper.constructSearch(search, fieldsToSearchInSafe, fieldsToPopulate);
|
|
50
|
-
queriesArray.push(searchQuery);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Filters ----------------------------------------------------------------------------
|
|
54
|
-
|
|
55
|
-
if (filters) {
|
|
56
|
-
const filtersQuery = fnHelper.constructQuery(filters);
|
|
57
|
-
if (filtersQuery) {
|
|
58
|
-
queriesArray.push(filtersQuery);
|
|
1
|
+
const { intersection } = require('lodash');
|
|
2
|
+
|
|
3
|
+
module.exports = _conf => {
|
|
4
|
+
const fnHelper = require('../helpers/functions')(_conf);
|
|
5
|
+
|
|
6
|
+
const getAll = async (req, res) => {
|
|
7
|
+
const modelName = req.params.model;
|
|
8
|
+
const segment = req.query.segment;
|
|
9
|
+
const search = (req.query.search || '').trim();
|
|
10
|
+
const filters = req.query.filters;
|
|
11
|
+
const fieldsToFetch = req.headers['am-model-fields'] || [];
|
|
12
|
+
const refFields = req.headers['am-ref-fields'] || {};
|
|
13
|
+
const inlineActions = req.headers['am-inline-actions'] || [];
|
|
14
|
+
const fieldsToSearchIn = req.query.search_in_fields || [];
|
|
15
|
+
const page = parseInt(req.query.page || 1);
|
|
16
|
+
const nbItemPerPage = 10;
|
|
17
|
+
const defaultOrdering = [ ['_id', 'DESC'] ];
|
|
18
|
+
const order = req.query.order || null;
|
|
19
|
+
|
|
20
|
+
const currentModel = fnHelper.getModelObject(modelName);
|
|
21
|
+
if (!currentModel) {
|
|
22
|
+
return res.status(403).json({ message: 'Invalid request' });
|
|
59
23
|
}
|
|
60
|
-
}
|
|
61
24
|
|
|
62
|
-
|
|
25
|
+
// Model actions
|
|
26
|
+
const currentModelActions = fnHelper.getModelActions(modelName);
|
|
63
27
|
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
28
|
+
// Get model properties
|
|
29
|
+
const keys = fnHelper.getModelProperties(currentModel);
|
|
30
|
+
|
|
31
|
+
// Ordering config
|
|
32
|
+
const orderConfig = fnHelper.validateOrderStructure(order) ? order : defaultOrdering;
|
|
33
|
+
const orderSafe = fnHelper.getCleanOrderStructure(orderConfig);
|
|
34
|
+
|
|
35
|
+
// Construct default fields to fetch
|
|
36
|
+
let fieldsToFetchSafe = keys
|
|
37
|
+
.filter(key => !key.path.includes('.'))
|
|
38
|
+
.map(key => key.path);
|
|
39
|
+
|
|
40
|
+
// If we get specific fields to display
|
|
41
|
+
if (Array.isArray(fieldsToFetch) && fieldsToFetch.length > 0) {
|
|
42
|
+
const flatKeys = keys.map(key => key.path);
|
|
43
|
+
const validKeys = intersection(fieldsToFetch, flatKeys);
|
|
44
|
+
if (validKeys.length > 0) {
|
|
45
|
+
fieldsToFetchSafe = validKeys;
|
|
46
|
+
}
|
|
68
47
|
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const findParams = queriesArray.length ? { $and: queriesArray } : {};
|
|
72
|
-
|
|
73
|
-
const data = await currentModel
|
|
74
|
-
.find(findParams)
|
|
75
|
-
.select(fieldsToFetchSafe)
|
|
76
|
-
.populate(fieldsToPopulate)
|
|
77
|
-
.sort(orderSafe)
|
|
78
|
-
.skip(nbItemPerPage * (page - 1))
|
|
79
|
-
.limit(nbItemPerPage)
|
|
80
|
-
.lean()
|
|
81
|
-
.catch(e => {
|
|
82
|
-
res.status(403).json({ message: e.message });
|
|
83
|
-
});
|
|
84
48
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
49
|
+
// Construct default fields to search in (only String type)
|
|
50
|
+
const defaultFieldsToSearchIn = keys
|
|
51
|
+
.filter(key => ['String'].includes(key.type))
|
|
52
|
+
.map(key => key.path);
|
|
53
|
+
const fieldsToSearchInSafe = Array.isArray(fieldsToSearchIn) && fieldsToSearchIn.length ? fieldsToSearchIn : defaultFieldsToSearchIn;
|
|
54
|
+
|
|
55
|
+
// Build ref fields for the model (for mongoose population purpose)
|
|
56
|
+
const fieldsToPopulate = fnHelper.getFieldsToPopulate(keys, fieldsToFetchSafe, refFields);
|
|
57
|
+
|
|
58
|
+
const queriesArray = [];
|
|
88
59
|
|
|
89
|
-
|
|
90
|
-
|
|
60
|
+
// Search -----------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
if (search) {
|
|
63
|
+
const searchQuery = fnHelper.constructSearch(search, fieldsToSearchInSafe, fieldsToPopulate);
|
|
64
|
+
queriesArray.push(searchQuery);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Filters ----------------------------------------------------------------------------
|
|
68
|
+
|
|
69
|
+
if (filters) {
|
|
70
|
+
const filtersQuery = fnHelper.constructQuery(filters);
|
|
71
|
+
if (filtersQuery) {
|
|
72
|
+
queriesArray.push(filtersQuery);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
91
75
|
|
|
92
|
-
|
|
93
|
-
const formattedData = data.map(item => {
|
|
94
|
-
return fnHelper.refFields(item, fieldsToPopulate);
|
|
95
|
-
});
|
|
76
|
+
// Segments ---------------------------------------------------------------------------
|
|
96
77
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
count: nbPage
|
|
78
|
+
if (segment && segment.type === 'code' && segment.data) {
|
|
79
|
+
const modelSegment = fnHelper.getModelSegment(modelName, segment.data);
|
|
80
|
+
if (modelSegment) {
|
|
81
|
+
queriesArray.push(modelSegment.query);
|
|
82
|
+
}
|
|
103
83
|
}
|
|
104
|
-
|
|
105
|
-
};
|
|
84
|
+
|
|
85
|
+
const findParams = queriesArray.length ? { $and: queriesArray } : {};
|
|
86
|
+
|
|
87
|
+
const data = await currentModel
|
|
88
|
+
.find(findParams)
|
|
89
|
+
.select(fieldsToFetchSafe)
|
|
90
|
+
.populate(fieldsToPopulate)
|
|
91
|
+
.sort(orderSafe)
|
|
92
|
+
.skip(nbItemPerPage * (page - 1))
|
|
93
|
+
.limit(nbItemPerPage)
|
|
94
|
+
.lean()
|
|
95
|
+
.catch(e => {
|
|
96
|
+
res.status(403).json({ message: e.message });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (!data) {
|
|
100
|
+
return res.status(403).json();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const dataCount = await currentModel.countDocuments(findParams);
|
|
104
|
+
const nbPage = Math.ceil(dataCount / nbItemPerPage);
|
|
105
|
+
|
|
106
|
+
// Make ref fields appeared as link in the dashboard
|
|
107
|
+
const formattedData = data.map(item => {
|
|
108
|
+
return fnHelper.refFields(item, fieldsToPopulate);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Inline actions button
|
|
112
|
+
const _inlineActions = currentModelActions.filter(action => inlineActions.includes(action.code));
|
|
113
|
+
if (_inlineActions.length) {
|
|
114
|
+
formattedData.forEach(item => {
|
|
115
|
+
item._am_inline_actions = _inlineActions
|
|
116
|
+
.filter(action => typeof action.filter === 'undefined' || action.filter(item))
|
|
117
|
+
.map(action => action.code);
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
res.json({
|
|
122
|
+
data: formattedData,
|
|
123
|
+
count: dataCount,
|
|
124
|
+
pagination: {
|
|
125
|
+
current: page,
|
|
126
|
+
count: nbPage
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
return getAll;
|
|
132
|
+
};
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
module.exports = _conf => {
|
|
2
|
+
const fnHelper = require('../helpers/functions')(_conf);
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const getIn = async (modelName, ids) => {
|
|
5
|
+
const currentModel = fnHelper.getModelObject(modelName);
|
|
6
|
+
if (!currentModel) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
// Get corresponding items
|
|
11
|
+
const items = await currentModel
|
|
12
|
+
.find({ _id: { $in: ids } })
|
|
13
|
+
.lean();
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
};
|
|
15
|
+
return items;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return getIn;
|
|
19
|
+
};
|
|
@@ -1,40 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
const fnHelper = require('../helpers/functions');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
module.exports = _conf => {
|
|
2
|
+
const fnHelper = require('../helpers/functions')(_conf);
|
|
3
|
+
|
|
4
|
+
const getOne = async (req, res) => {
|
|
5
|
+
const modelName = req.params.model;
|
|
6
|
+
const modelItemId = req.params.id;
|
|
7
|
+
const fieldsToFetch = req.headers['am-model-fields'] || [];
|
|
8
|
+
const refFields = req.headers['am-ref-fields'] || {};
|
|
9
|
+
|
|
10
|
+
const currentModel = fnHelper.getModelObject(modelName);
|
|
11
|
+
if (!currentModel) {
|
|
12
|
+
return res.status(403).json({ message: 'Invalid request' });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const keys = fnHelper.getModelProperties(currentModel);
|
|
16
|
+
const defaultFieldsToFetch = keys.map(key => key.path);
|
|
17
|
+
const fieldsToFetchSafe = Array.isArray(fieldsToFetch) && fieldsToFetch.length ? fieldsToFetch : defaultFieldsToFetch;
|
|
18
|
+
|
|
19
|
+
// Build ref fields for the model (for mongoose population purpose)
|
|
20
|
+
const fieldsToPopulate = fnHelper.getFieldsToPopulate(keys, fieldsToFetchSafe, refFields);
|
|
21
|
+
|
|
22
|
+
let data = await currentModel
|
|
23
|
+
.findById(modelItemId)
|
|
24
|
+
.select(fieldsToFetchSafe)
|
|
25
|
+
.populate(fieldsToPopulate)
|
|
26
|
+
.lean()
|
|
27
|
+
.catch(e => {
|
|
28
|
+
res.status(403).json({ message: e.message });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (!data) {
|
|
32
|
+
return res.status(403).json();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
data = fnHelper.refFields(data, fieldsToPopulate);
|
|
36
|
+
|
|
37
|
+
res.json({
|
|
38
|
+
data
|
|
29
39
|
});
|
|
40
|
+
};
|
|
30
41
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
data = fnHelper.refFields(data, fieldsToPopulate);
|
|
36
|
-
|
|
37
|
-
res.json({
|
|
38
|
-
data
|
|
39
|
-
});
|
|
40
|
-
};
|
|
42
|
+
return getOne;
|
|
43
|
+
};
|
|
@@ -1,53 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
module.exports = _conf => {
|
|
2
|
+
const fnHelper = require('../helpers/functions')(_conf);
|
|
3
|
+
|
|
4
|
+
const getRefs = async (req, res) => {
|
|
5
|
+
const modelName = req.params.model;
|
|
6
|
+
const ids = req.query.ids;
|
|
7
|
+
const refFields = req.headers['am-ref-fields'] || {};
|
|
8
|
+
const nbItemPerPage = 20;
|
|
9
|
+
|
|
10
|
+
if (!ids) {
|
|
11
|
+
return res.status(403).json({ message: 'Missing parameter ids' });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const fieldsToFetchSafe = refFields[modelName];
|
|
15
|
+
|
|
16
|
+
// If no ref fields, return default response
|
|
17
|
+
if (!fieldsToFetchSafe) {
|
|
18
|
+
return res.json({
|
|
19
|
+
data: ids.map(id => ({ value: id, label: id }))
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const currentModel = fnHelper.getModelObject(modelName);
|
|
24
|
+
if (!currentModel) {
|
|
25
|
+
return res.status(403).json({ message: 'Invalid request' });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Find parameters
|
|
29
|
+
const findParams = { _id: ids };
|
|
30
|
+
|
|
31
|
+
const data = await currentModel
|
|
32
|
+
.find(findParams)
|
|
33
|
+
.select(fieldsToFetchSafe)
|
|
34
|
+
.limit(nbItemPerPage)
|
|
35
|
+
.lean()
|
|
36
|
+
.catch(e => {
|
|
37
|
+
res.status(403).json({ message: e.message });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
if (!data) {
|
|
41
|
+
return res.status(403).json();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Format the response
|
|
45
|
+
const formattedData = ids.map(_id => {
|
|
46
|
+
const match = data.find(d => d._id.toString() === _id.toString());
|
|
47
|
+
const label = match ? fnHelper.fieldsToValues(fieldsToFetchSafe, match) : _id;
|
|
48
|
+
return { value: _id, label };
|
|
19
49
|
});
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (!currentModel) {
|
|
24
|
-
return res.status(403).json({ message: 'Invalid request' });
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Find parameters
|
|
28
|
-
const findParams = { _id: ids };
|
|
29
|
-
|
|
30
|
-
const data = await currentModel
|
|
31
|
-
.find(findParams)
|
|
32
|
-
.select(fieldsToFetchSafe)
|
|
33
|
-
.limit(nbItemPerPage)
|
|
34
|
-
.lean()
|
|
35
|
-
.catch(e => {
|
|
36
|
-
res.status(403).json({ message: e.message });
|
|
50
|
+
|
|
51
|
+
res.json({
|
|
52
|
+
data: formattedData
|
|
37
53
|
});
|
|
54
|
+
};
|
|
38
55
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Format the response
|
|
44
|
-
const formattedData = ids.map(_id => {
|
|
45
|
-
const match = data.find(d => d._id.toString() === _id.toString());
|
|
46
|
-
const label = match ? fnHelper.fieldsToValues(fieldsToFetchSafe, match) : _id;
|
|
47
|
-
return { value: _id, label };
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
res.json({
|
|
51
|
-
data: formattedData
|
|
52
|
-
});
|
|
53
|
-
};
|
|
56
|
+
return getRefs;
|
|
57
|
+
};
|
|
@@ -1,26 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
const fnHelper = require('../helpers/functions');
|
|
1
|
+
module.exports = _conf => {
|
|
2
|
+
const fnHelper = require('../helpers/functions')(_conf);
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const postOne = async (req, res) => {
|
|
5
|
+
const modelName = req.params.model;
|
|
6
|
+
const data = req.body.data;
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const currentModel = fnHelper.getModelObject(modelName);
|
|
9
|
+
if (!currentModel) {
|
|
10
|
+
return res.status(403).json({ message: 'Invalid request' });
|
|
11
|
+
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
if (newSavedItem) {
|
|
20
|
-
res.json({
|
|
21
|
-
data: {
|
|
22
|
-
id: newSavedItem._id // id and not _id to be generic
|
|
23
|
-
}
|
|
13
|
+
const newItem = new currentModel(data);
|
|
14
|
+
const newSavedItem = await newItem.save().catch(e => {
|
|
15
|
+
const errorObject = fnHelper.buildError(e, 'An error occured when saving the item');
|
|
16
|
+
res.status(403).json(errorObject);
|
|
24
17
|
});
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
|
|
19
|
+
if (newSavedItem) {
|
|
20
|
+
res.json({
|
|
21
|
+
data: {
|
|
22
|
+
id: newSavedItem._id // id and not _id to be generic
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return postOne;
|
|
29
|
+
};
|
|
@@ -1,40 +1,45 @@
|
|
|
1
|
-
const _ = require('lodash');
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
// const { model, itemEditableKeys } = models[modelName];
|
|
15
|
-
|
|
16
|
-
// Only keep authorized keys
|
|
17
|
-
// const cleanData = {};
|
|
18
|
-
// updatableFields.forEach(updatableField => {
|
|
19
|
-
// const fieldValue = _.get(data, updatableField);
|
|
20
|
-
// if (fieldValue) {
|
|
21
|
-
// _.set(cleanData, updatableField, fieldValue)
|
|
22
|
-
// }
|
|
23
|
-
// });
|
|
24
|
-
|
|
25
|
-
const cleanData = data;
|
|
26
|
-
|
|
27
|
-
if (Object.keys(cleanData).length) {
|
|
28
|
-
try {
|
|
29
|
-
await currentModel.findByIdAndUpdate(modelItemId, cleanData, { runValidators: true });
|
|
30
|
-
res.json({ data: cleanData });
|
|
1
|
+
// const _ = require('lodash');
|
|
2
|
+
|
|
3
|
+
module.exports = _conf => {
|
|
4
|
+
const fnHelper = require('../helpers/functions')(_conf);
|
|
5
|
+
|
|
6
|
+
const putOne = async (req, res) => {
|
|
7
|
+
const modelName = req.params.model;
|
|
8
|
+
const modelItemId = req.params.id;
|
|
9
|
+
const data = req.body.data;
|
|
10
|
+
|
|
11
|
+
const currentModel = fnHelper.getModelObject(modelName);
|
|
12
|
+
if (!currentModel) {
|
|
13
|
+
return res.status(403).json({ message: 'Invalid request' });
|
|
31
14
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
15
|
+
|
|
16
|
+
// const { model, itemEditableKeys } = models[modelName];
|
|
17
|
+
|
|
18
|
+
// Only keep authorized keys
|
|
19
|
+
// const cleanData = {};
|
|
20
|
+
// updatableFields.forEach(updatableField => {
|
|
21
|
+
// const fieldValue = _.get(data, updatableField);
|
|
22
|
+
// if (fieldValue) {
|
|
23
|
+
// _.set(cleanData, updatableField, fieldValue)
|
|
24
|
+
// }
|
|
25
|
+
// });
|
|
26
|
+
|
|
27
|
+
const cleanData = data;
|
|
28
|
+
|
|
29
|
+
if (Object.keys(cleanData).length) {
|
|
30
|
+
try {
|
|
31
|
+
await currentModel.findByIdAndUpdate(modelItemId, cleanData, { runValidators: true });
|
|
32
|
+
res.json({ data: cleanData });
|
|
33
|
+
}
|
|
34
|
+
catch(e) {
|
|
35
|
+
const errorObject = fnHelper.buildError(e, 'Unable to update the model');
|
|
36
|
+
res.status(403).json(errorObject);
|
|
37
|
+
}
|
|
35
38
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
39
|
+
else {
|
|
40
|
+
res.json({});
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return putOne;
|
|
45
|
+
};
|