adminmate-express-mongoose 1.2.7 → 1.2.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adminmate-express-mongoose",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "description": "Adminmate Express/Mongoose connector",
5
5
  "author": "Marc Delalonde",
6
6
  "homepage": "http://adminmate.io",
@@ -23,7 +23,7 @@
23
23
  "url": "https://github.com/Adminmate/adminmate-express-mongoose.git"
24
24
  },
25
25
  "dependencies": {
26
- "adminmate-express-core": "^1.1.6",
26
+ "adminmate-express-core": "^1.1.7",
27
27
  "lodash": "^4.17.21",
28
28
  "moment": "^2.29.1",
29
29
  "mongoose": "^5.9.7",
@@ -1,9 +1,56 @@
1
+ const fnHelper = require('../helpers/functions');
2
+
1
3
  module.exports = async (currentModel, data) => {
4
+ // Get relationship model
5
+ const relationshipModel = fnHelper.getModelObject(data.relationship_model);
6
+ if (!relationshipModel) {
7
+ return res.status(403).json({ message: 'Invalid request' });
8
+ }
9
+
10
+ // Default limit
11
+ let limit = data.limit || 10;
12
+
13
+ let _value = 1;
14
+ if (data.relationship_field && ['sum', 'avg'].includes(data.relationship_operation)) {
15
+ _value = `$${data.relationship_field}`;
16
+ }
17
+
18
+ const repartitionData = await relationshipModel
19
+ .aggregate([
20
+ {
21
+ $group: {
22
+ _id: `$${data.relationship_model_ref_field}`,
23
+ count: data.operation === 'avg' ? { $avg: _value } : { $sum: _value },
24
+ }
25
+ },
26
+ {
27
+ $project: {
28
+ key: '$_id',
29
+ value: '$count',
30
+ _id: false
31
+ }
32
+ }
33
+ ])
34
+ .limit(limit)
35
+ .sort({ value: -1 });
36
+
37
+ const parentIds = repartitionData.map(d => d.key);
38
+ const parentData = await currentModel.find({ _id: parentIds }).select(data.field).lean();
39
+
40
+ repartitionData.forEach(d => {
41
+ d.item_model = data.model;
42
+ d.item_id = d.key;
43
+ const parent = parentData.find(p => p._id.toString() === d.key.toString());
44
+ if (parent) {
45
+ d.key = parent[data.field];
46
+ }
47
+ });
48
+
2
49
  return {
3
- success: false,
50
+ success: true,
4
51
  data: {
5
52
  config: null,
6
- data: {}
53
+ data: repartitionData
7
54
  }
8
55
  };
9
56
  };
@@ -1,12 +0,0 @@
1
- version: '3'
2
-
3
- services:
4
- mongo:
5
- container_name: adminmate-express-mongoose
6
- environment:
7
- - MONGO_INITDB_DATABASE=demo
8
- - MONGO_DATA_DIR=/data/db
9
- - MONGO_LOG_DIR=/dev/null
10
- image: mongo
11
- ports:
12
- - '27017:27017'