Skip to content

Commit 131a79d

Browse files
author
Lucas Cosmo Rocha
committed
Add 'today' validation param in Date
1 parent c96542b commit 131a79d

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

lib/mapper/filters.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ function buildRegEx(value) {
8787

8888
function parseDate(query, options) {
8989
var result = []
90+
if (query.start_at === 'today') query.start_at = dateToString(new Date())
91+
if (query.end_at === 'today') query.end_at = dateToString(new Date())
9092
if (query.period) {
91-
(query.end_at) ?
93+
(query.end_at && (isDate(query.end_at) || isDateTime(query.end_at))) ?
9294
result.push(
9395
processQuery(options.date_field, 'lt:'.concat(normalizeDate(query.end_at, false))),
9496
processQuery(options.date_field, 'gte:'.concat(normalizeDate(

test/integration/index.spec.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,26 @@ describe('queryFilter()', function () {
216216
})
217217

218218
context('when query contains date filters param', function () {
219+
it('should return req.query with set start_at params as today', function (done) {
220+
const expect_filters = {
221+
$and: [
222+
{ created_at: { $lt: new Date().toISOString() } },
223+
{ created_at: { $gte: new Date().toISOString() } }
224+
]
225+
}
226+
227+
const query = { start_at: 'today' }
228+
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
229+
const res = httpMocks.createResponse()
230+
231+
const options = JSON.parse(JSON.stringify(default_options))
232+
options.default.filters = expect_filters
233+
234+
qs({})(req, res, function next() {
235+
validateFilterWithDate(req, options)
236+
})
237+
done()
238+
})
219239
it('should return req.query with set start_at params as date', function (done) {
220240
const expect_filters = {
221241
$and: [
@@ -300,14 +320,14 @@ describe('queryFilter()', function () {
300320
done()
301321
})
302322

303-
it('should return req.query with set period and end_at param', function (done) {
323+
it('should return req.query with set period and end_at param as dateTime', function (done) {
304324
const expect_filters = {
305325
$and: [
306326
{ created_at: { $lt: new Date('2018-12-09').toISOString() } },
307327
{ created_at: { $gte: new Date('2018-11-10').toISOString() } }
308328
]
309329
}
310-
const query = { period: '1m', end_at: '2018-12-09' }
330+
const query = { period: '1m', end_at: '2018-12-09T00:00:00' }
311331
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
312332
const res = httpMocks.createResponse()
313333

@@ -320,7 +340,7 @@ describe('queryFilter()', function () {
320340
done()
321341
})
322342

323-
it('should return req.query with set period as day params', function (done) {
343+
it('should return req.query with set period as day params and end_at as today', function (done) {
324344
const now = new Date()
325345
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
326346
const beforeToday = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 9))
@@ -331,7 +351,7 @@ describe('queryFilter()', function () {
331351
]
332352
}
333353

334-
const query = { period: '10d' }
354+
const query = { period: '10d', end_at: 'today' }
335355
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
336356
const res = httpMocks.createResponse()
337357

0 commit comments

Comments
 (0)