Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for querying "createdAt" date range in listHandler #297

Open
alsabbahy opened this issue May 15, 2022 · 1 comment
Open

Added support for querying "createdAt" date range in listHandler #297

alsabbahy opened this issue May 15, 2022 · 1 comment

Comments

@alsabbahy
Copy link

Hi @JKHeadley ! 👋

Firstly, thanks for your work on this project! 🙂

Regarding the issue #296.
Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/rest-hapi/utilities/handler-helper.js b/node_modules/rest-hapi/utilities/handler-helper.js
index e0334e5..a13b2ed 100644
--- a/node_modules/rest-hapi/utilities/handler-helper.js
+++ b/node_modules/rest-hapi/utilities/handler-helper.js
@@ -148,6 +148,23 @@ async function _listHandler(model, request, Log) {
     if (query.$flatten) {
       flatten = true
     }
+
+    if ( query.$startDate || query.$endDate ) {
+      query.createdAt = {}
+      
+      if ( query.$startDate ) {
+        query.createdAt.$gte = new Date( query.$startDate )
+
+        delete query.$startDate;
+      }
+
+      if ( query.$endDate ) {
+        query.createdAt.$lte = new Date( query.$endDate )
+
+        delete query.$endDate;
+      }
+    }
+
     delete query.$flatten
     const { $embed } = query
     if (query.$count) {
diff --git a/node_modules/rest-hapi/utilities/joi-mongoose-helper.js b/node_modules/rest-hapi/utilities/joi-mongoose-helper.js
index 1a076eb..e230021 100644
--- a/node_modules/rest-hapi/utilities/joi-mongoose-helper.js
+++ b/node_modules/rest-hapi/utilities/joi-mongoose-helper.js
@@ -337,6 +337,8 @@ internals.generateJoiListQueryModel = function(model, logger) {
         .optional()
         .description('An optional field for raw mongoose queries.')
     }
+    queryModel.$startDate = Joi.date().description( 'The start of the date range.' ).optional()
+    queryModel.$endDate = Joi.date().description( 'The end of the date range.' ).optional()
     
     _.each(queryableFields, function(fieldName) {
       const joiModel = internals.generateJoiModelFromFieldType(

This issue body was partially generated by patch-package.

@alsabbahy alsabbahy changed the title Added support for date Added support for querying "createdAt" date range in listHandler May 15, 2022
@JKHeadley
Copy link
Owner

@alsabbahy thanks for this! I'll work on integrating this functionality into the tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants