adminmate-express-mongoose 1.3.11 → 1.3.13
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
|
@@ -87,10 +87,12 @@ module.exports = _conf => {
|
|
|
87
87
|
const parentIds = repartitionData.map(d => d.key);
|
|
88
88
|
const parentData = await currentModel.find({ _id: parentIds }).select(data.field).lean();
|
|
89
89
|
|
|
90
|
+
const parentMap = new Map(parentData.map(p => [p._id.toString(), p]));
|
|
91
|
+
|
|
90
92
|
repartitionData.forEach(d => {
|
|
91
93
|
d.item_model = data.model;
|
|
92
94
|
d.item_id = d.key;
|
|
93
|
-
const parent =
|
|
95
|
+
const parent = parentMap.get(d.key.toString());
|
|
94
96
|
if (parent) {
|
|
95
97
|
d.key = parent[data.field];
|
|
96
98
|
}
|
|
@@ -115,16 +115,23 @@ module.exports = _conf => {
|
|
|
115
115
|
if (repartitionData && repartitionData.length > 0) {
|
|
116
116
|
// Get min & max date in the results
|
|
117
117
|
const momentFormat = data.timeframe === 'week' ? 'YYYY-WW' : 'YYYY-MM-DD HH:mm:ss';
|
|
118
|
-
const unixRange = repartitionData.map(
|
|
118
|
+
const unixRange = repartitionData.map(d => moment(d.key, momentFormat));
|
|
119
119
|
const min = _.min(unixRange).clone();
|
|
120
120
|
const max = _.max(unixRange).clone();
|
|
121
121
|
|
|
122
|
+
// Pre-build a map keyed by formatted date to avoid O(n²) lookup
|
|
123
|
+
const dataMap = new Map(repartitionData.map(d => {
|
|
124
|
+
const key = moment(d.key, momentFormat).startOf(data.timeframe).format('YYYY-MM-DD');
|
|
125
|
+
return [key, d.value];
|
|
126
|
+
}));
|
|
127
|
+
|
|
122
128
|
let currentDate = min;
|
|
123
129
|
while (currentDate.isSameOrBefore(max)) {
|
|
124
|
-
const
|
|
125
|
-
const
|
|
130
|
+
const dateKey = currentDate.format('YYYY-MM-DD');
|
|
131
|
+
const matchingValue = dataMap.get(dateKey);
|
|
132
|
+
const value = matchingValue !== undefined ? fnHelper.toFixedIfNecessary(matchingValue, 2) : 0;
|
|
126
133
|
formattedData.push({
|
|
127
|
-
key:
|
|
134
|
+
key: dateKey,
|
|
128
135
|
value
|
|
129
136
|
});
|
|
130
137
|
currentDate.add(1, data.timeframe).startOf('day');
|
|
@@ -102,7 +102,9 @@ module.exports = _conf => {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
// Simple count
|
|
105
|
-
const dataCount =
|
|
105
|
+
const dataCount = Object.keys(findParams).length
|
|
106
|
+
? await currentModel.countDocuments(findParams)
|
|
107
|
+
: await currentModel.estimatedDocumentCount();
|
|
106
108
|
|
|
107
109
|
return {
|
|
108
110
|
success: true,
|
package/src/helpers/functions.js
CHANGED
|
@@ -115,14 +115,17 @@ module.exports = _conf => {
|
|
|
115
115
|
return queryRuleSet(rule);
|
|
116
116
|
}
|
|
117
117
|
let q = {};
|
|
118
|
+
// Strict 24-hex check — mongoose.isValidObjectId accepts any 24-char string,
|
|
119
|
+
// which throws when constructing an ObjectId from a non-hex value.
|
|
120
|
+
const isStrictObjectId = (val) => typeof val === 'string' && /^[a-f\d]{24}$/i.test(val);
|
|
118
121
|
if (rule.operator === 'is') {
|
|
119
122
|
// In order that aggregate queries to work well
|
|
120
|
-
const value =
|
|
123
|
+
const value = isStrictObjectId(rule.value) ? new mongoose.Types.ObjectId(rule.value) : rule.value;
|
|
121
124
|
q[rule.field] = { $eq: value };
|
|
122
125
|
}
|
|
123
126
|
else if (rule.operator === 'is_not') {
|
|
124
127
|
// In order that aggregate queries to work well
|
|
125
|
-
const value =
|
|
128
|
+
const value = isStrictObjectId(rule.value) ? new mongoose.Types.ObjectId(rule.value) : rule.value;
|
|
126
129
|
q[rule.field] = { $ne: value };
|
|
127
130
|
}
|
|
128
131
|
// Date
|
|
@@ -334,9 +337,10 @@ module.exports = _conf => {
|
|
|
334
337
|
params.$or.push({ [field]: { '$regex': `${search}`, '$options': 'i' } });
|
|
335
338
|
});
|
|
336
339
|
|
|
337
|
-
// If the search
|
|
338
|
-
//
|
|
339
|
-
|
|
340
|
+
// If the search looks like a real mongodb ObjectId (24 hex chars).
|
|
341
|
+
// mongoose.isValidObjectId is too permissive (accepts any 24-char string),
|
|
342
|
+
// which causes Cast errors for values like "foo.myshopify.com" (24 chars but not hex).
|
|
343
|
+
if (/^[a-f\d]{24}$/i.test(search)) {
|
|
340
344
|
params.$or.push({ _id: search });
|
|
341
345
|
fieldsToPopulate.map(field => {
|
|
342
346
|
params.$or.push({ [field.path]: search });
|