Skip to content

Commit b697f11

Browse files
committed
fix(API) Dates in aggregation pipeline $match passed as part of the query now work
1 parent 44059d8 commit b697f11

File tree

2 files changed

+78
-62
lines changed

2 files changed

+78
-62
lines changed

server/data_form.js

Lines changed: 38 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/data_form.ts

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,39 @@ DataForm.prototype.report = function () {
593593
}, this);
594594
};
595595

596+
DataForm.prototype.hackVariablesInPipeline = function (runPipeline) {
597+
for (var pipelineSection = 0; pipelineSection < runPipeline.length; pipelineSection++) {
598+
if (runPipeline[pipelineSection]['$match']) {
599+
this.hackVariables(runPipeline[pipelineSection]['$match']);
600+
}
601+
}
602+
};
603+
604+
DataForm.prototype.hackVariables = function (obj) {
605+
// Replace variables that cannot be serialised / deserialised. Bit of a hack, but needs must...
606+
// Anything formatted 1800-01-01T00:00:00.000Z or 1800-01-01T00:00:00.000+0000 is converted to a Date
607+
// Only handles the cases I need for now
608+
// TODO: handle arrays etc
609+
for (var prop in obj) {
610+
if (obj.hasOwnProperty(prop)) {
611+
if (typeof obj[prop] === 'string') {
612+
var dateTest = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})(Z|[+ -]\d{4})$/.exec(obj[prop]);
613+
if (dateTest) {
614+
obj[prop] = new Date(dateTest[1] + 'Z');
615+
} else {
616+
var objectIdTest = /^([0-9a-fA-F]{24})$/.exec(obj[prop]);
617+
if (objectIdTest) {
618+
obj[prop] = new mongoose.Types.ObjectId(objectIdTest[1]);
619+
}
620+
}
621+
} else if (_.isObject(obj[prop])) {
622+
this.hackVariables(obj[prop]);
623+
}
624+
}
625+
}
626+
};
627+
628+
596629
DataForm.prototype.reportInternal = function (req, resource, schema, options, callback) {
597630
var runPipeline,
598631
self = this;
@@ -638,36 +671,7 @@ DataForm.prototype.reportInternal = function (req, resource, schema, options, ca
638671
}
639672

640673
runPipeline = JSON.parse(runPipeline);
641-
642-
// Replace variables that cannot be serialised / deserialised. Bit of a hack, but needs must...
643-
// Anything formatted 1800-01-01T00:00:00.000Z or 1800-01-01T00:00:00.000+0000 is converted to a Date
644-
// Only handles the cases I need for now
645-
// TODO: handle arrays etc
646-
var hackVariables = function (obj) {
647-
for (var prop in obj) {
648-
if (obj.hasOwnProperty(prop)) {
649-
if (typeof obj[prop] === 'string') {
650-
var dateTest = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})(Z|[+ -]\d{4})$/.exec(obj[prop]);
651-
if (dateTest) {
652-
obj[prop] = new Date(dateTest[1] + 'Z');
653-
} else {
654-
var objectIdTest = /^([0-9a-fA-F]{24})$/.exec(obj[prop]);
655-
if (objectIdTest) {
656-
obj[prop] = new mongoose.Types.ObjectId(objectIdTest[1]);
657-
}
658-
}
659-
} else if (_.isObject(obj[prop])) {
660-
hackVariables(obj[prop]);
661-
}
662-
}
663-
}
664-
};
665-
666-
for (var pipelineSection = 0; pipelineSection < runPipeline.length; pipelineSection++) {
667-
if (runPipeline[pipelineSection]['$match']) {
668-
hackVariables(runPipeline[pipelineSection]['$match']);
669-
}
670-
}
674+
self.hackVariablesInPipeline(runPipeline);
671675

672676
// Add the findFunc query to the pipeline
673677
if (queryObj) {
@@ -864,7 +868,7 @@ DataForm.prototype.processCollection = function (req) {
864868
};
865869

866870
/**
867-
* Renders a view with the list of docs, which may be filtered by the f query parameter
871+
* Renders a view with the list of docs, which may be modified by query parameters
868872
*/
869873
DataForm.prototype.collectionGet = function () {
870874
return _.bind(function (req, res, next) {
@@ -881,6 +885,11 @@ DataForm.prototype.collectionGet = function () {
881885
var skipParam = urlParts.query.s ? JSON.parse(urlParts.query.s) : 0;
882886
var orderParam = urlParts.query.o ? JSON.parse(urlParts.query.o) : req.resource.options.listOrder;
883887

888+
// Dates in aggregation must be Dates
889+
if (aggregationParam) {
890+
this.hackVariablesInPipeline(aggregationParam);
891+
}
892+
884893
var self = this;
885894

886895
this.filteredFind(req.resource, req, aggregationParam, findParam, orderParam, limitParam, skipParam, function (err, docs) {

0 commit comments

Comments
 (0)