Skip to content

Commit 1120a19

Browse files
author
Lucas Cosmo Rocha
committed
Altering filter validation
Replacing the date_start and date_end parameter with start_at and end_at. Update integration tests.
1 parent 2df7e11 commit 1120a19

File tree

3 files changed

+92
-54
lines changed

3 files changed

+92
-54
lines changed

lib/mapper/filters.js

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ function filters(query, options) {
1111

1212
if (Object.keys(query).length > 0) {
1313
result.$and = result.$and.concat(parseDate(query, options))
14-
delete query.date_start
15-
delete query.date_end
14+
delete query.start_at
15+
delete query.end_at
1616
delete query.period
1717
for (var elem in query) {
1818
if (query[elem] instanceof Array) {
1919
query[elem].forEach(function (param) {
2020
result['$and'].push(processQuery(elem, param))
2121
})
2222
} else {
23-
if (query[elem].includes(',')) {
23+
if (query[elem] instanceof Object) { }
24+
else if (query[elem].includes(',')) {
2425
result['$or'] = result['$or'].concat(splitByCommas(elem, query[elem]))
2526
} else {
2627
Object.assign(result, processQuery(elem, query[elem]))
@@ -46,7 +47,7 @@ function treatValue(value) {
4647
else if (isDateTime(value)) return value
4748
else if (value.includes('*')) return buildRegEx(value)
4849
else if (value.includes(':')) return getCompareOperator(value)
49-
else if (value === 'now') return normalizeDate (new Date().toDateString())
50+
else if (value === 'now') return normalizeDate(dateToString(new Date()), false)
5051
else if (parseInt(value)) return parseInt(value)
5152
return value
5253
}
@@ -87,56 +88,56 @@ function buildRegEx(value) {
8788
function parseDate(query, options) {
8889
var result = []
8990
if (query.period) {
90-
(query.date_end) ?
91+
(query.end_at) ?
9192
result.push(
92-
processQuery(options.date_field, 'lt:'.concat(normalizeDate(query.date_end, false))),
93+
processQuery(options.date_field, 'lt:'.concat(normalizeDate(query.end_at, false))),
9394
processQuery(options.date_field, 'gte:'.concat(normalizeDate(
94-
getDateStart(query.period, new Date(query.date_end))))), true) :
95+
getDateStartByPeriod(query.period, new Date(query.end_at))))), true) :
9596
result.push(
9697
processQuery(options.date_field, 'lt:now'),
9798
processQuery(options.date_field, 'gte:'.concat(
98-
getDateStart(query.period, new Date()))))
99+
getDateStartByPeriod(query.period, new Date()))))
99100
return result
100101
}
101102

102-
if (query.date_start && (isDate(query.date_start) || isDateTime(query.date_start))) {
103-
if (query.date_end && (isDate(query.date_end) || isDateTime(query.date_end))) {
103+
if (query.start_at && (isDate(query.start_at) || isDateTime(query.start_at))) {
104+
if (query.end_at && (isDate(query.end_at) || isDateTime(query.end_at))) {
104105
result.push(
105-
processQuery(options.date_field, 'lt:'.concat(query.date_end)))
106+
processQuery(options.date_field, 'lt:'.concat(query.end_at)))
106107
} else {
107108
result.push(
108109
processQuery(options.date_field, 'lt:now'))
109110
}
110111
result.push(
111-
processQuery(options.date_field, 'gte:'.concat(query.date_start)))
112+
processQuery(options.date_field, 'gte:'.concat(query.start_at)))
112113
}
113114
return result
114115
}
115116

116-
function getDateStart(period, date_end) {
117-
if (period.endsWith('d')) {
118-
return dateToString(new Date(
119-
date_end.getFullYear(),
120-
date_end.getMonth(),
121-
(date_end.getDate() - onlyNumbers(period))))
122-
}
123-
else if (period.endsWith('w')) {
124-
return dateToString(new Date(
125-
date_end.getFullYear(),
126-
date_end.getMonth(),
127-
(date_end.getDate() - 7 * onlyNumbers(period))))
128-
}
129-
else if (period.endsWith('m')) {
130-
return dateToString(new Date(
131-
date_end.getFullYear(),
132-
(date_end.getMonth() - onlyNumbers(period)),
133-
date_end.getDate()))
134-
}
135-
else {
136-
return dateToString(new Date(
137-
(date_end.getFullYear() - onlyNumbers(period)),
138-
date_end.getMonth(),
139-
date_end.getDate()))
117+
function getDateStartByPeriod(period, end_at) {
118+
if (!(/^[0-9]{1,}[d|w|m|y]$/.test(period))) return dateToString(new Date())
119+
120+
switch (period.slice(-1)) {
121+
case 'd':
122+
return dateToString(new Date(
123+
end_at.getFullYear(),
124+
end_at.getMonth(),
125+
(end_at.getDate() - onlyNumbers(period))))
126+
case 'w':
127+
return dateToString(new Date(
128+
end_at.getFullYear(),
129+
end_at.getMonth(),
130+
(end_at.getDate() - 7 * onlyNumbers(period))))
131+
case 'm':
132+
return dateToString(new Date(
133+
end_at.getFullYear(),
134+
(end_at.getMonth() - onlyNumbers(period)),
135+
end_at.getDate()))
136+
case 'y':
137+
return dateToString(new Date(
138+
(end_at.getFullYear() - onlyNumbers(period)),
139+
end_at.getMonth(),
140+
end_at.getDate()))
140141
}
141142
}
142143

@@ -153,15 +154,12 @@ function onlyNumbers(value) {
153154
}
154155

155156
function normalizeDate(date, isDateStart) {
156-
if (isDate(date) && isDateStart) {
157+
if (isDateStart) {
157158
return date.replace(/\D/g, '').replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3T00:00:00')
158159
}
159-
else if (isDate(date) && !isDateStart) {
160-
return date.replace(/\D/g, '').replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3T23:59:59')
161-
}
162-
return date
160+
return date.replace(/\D/g, '').replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3T23:59:59')
163161
}
164162

165163
exports = module.exports = {
166164
filters: filters
167-
}
165+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "query-strings-parser",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Middleware to transform query strings in a format that is recognized by the MongoDB, MySQL and other databases...",
55
"license": "MIT",
66
"main": "index.js",

test/integration/index.spec.js

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('queryFilter()', function () {
99

1010
context('when use default configurations', function () {
1111
context('when query is empty', function () {
12+
1213
it('should return req.query as default middleware options', function (done) {
1314
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: {} })
1415
const res = httpMocks.createResponse()
@@ -113,6 +114,21 @@ describe('queryFilter()', function () {
113114
})
114115
})
115116

117+
118+
context('when query filters param value is a object', function () {
119+
it('should return req.query as default middleware options', function (done) {
120+
121+
const query = { value: { and: 123 } }
122+
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
123+
const res = httpMocks.createResponse()
124+
125+
qs({})(req, res, function next() {
126+
validate(req, default_options)
127+
})
128+
done()
129+
})
130+
})
131+
116132
context('when query contains simple filters param', function () {
117133
it('should return req.query with set field params', function (done) {
118134
const expect_filters = { name: 'lucas', age: 30 }
@@ -200,15 +216,15 @@ describe('queryFilter()', function () {
200216
})
201217

202218
context('when query contains date filters param', function () {
203-
it('should return req.query with set date_start params as date', function (done) {
219+
it('should return req.query with set start_at params as date', function (done) {
204220
const expect_filters = {
205221
$and: [
206222
{ created_at: { $lt: new Date().toISOString() } },
207223
{ created_at: { $gte: '2018-12-05T00:00:00.000Z' } }
208224
]
209225
}
210226

211-
const query = { date_start: '2018-12-05' }
227+
const query = { start_at: '2018-12-05' }
212228
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
213229
const res = httpMocks.createResponse()
214230

@@ -221,15 +237,15 @@ describe('queryFilter()', function () {
221237
done()
222238
})
223239

224-
it('should return req.query with set date_start params as dateTime', function (done) {
240+
it('should return req.query with set start_at params as dateTime', function (done) {
225241
const expect_filters = {
226242
$and: [
227243
{ created_at: { $lt: new Date().toISOString() } },
228244
{ created_at: { $gte: '2018-12-05T00:00:00.000Z' } }
229245
]
230246
}
231247

232-
const query = { date_start: '2018-12-05T00:00:00'}
248+
const query = { start_at: '2018-12-05T00:00:00' }
233249
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
234250
const res = httpMocks.createResponse()
235251

@@ -242,15 +258,15 @@ describe('queryFilter()', function () {
242258
done()
243259
})
244260

245-
it('should return req.query with set date_start and date_end params as date', function (done) {
261+
it('should return req.query with set start_at and end_at params as date', function (done) {
246262

247263
const expect_filters = {
248264
$and: [
249265
{ created_at: { $lt: '2018-12-11T00:00:00.000Z' } },
250266
{ created_at: { $gte: '2018-12-01T00:00:00.000Z' } }]
251267
}
252268

253-
const query = { date_start: '2018-12-01', date_end: '2018-12-11' }
269+
const query = { start_at: '2018-12-01', end_at: '2018-12-11' }
254270
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
255271
const res = httpMocks.createResponse()
256272

@@ -263,15 +279,15 @@ describe('queryFilter()', function () {
263279
done()
264280
})
265281

266-
it('should return req.query with set date_start and date_end params as dateTime', function (done) {
282+
it('should return req.query with set start_at and end_at params as dateTime', function (done) {
267283

268284
const expect_filters = {
269285
$and: [
270286
{ created_at: { $lt: '2018-12-11T00:00:00.000Z' } },
271287
{ created_at: { $gte: '2018-12-01T00:00:00.000Z' } }]
272288
}
273289

274-
const query = { date_start: '2018-12-01T00:00:00', date_end: '2018-12-11T00:00:00' }
290+
const query = { start_at: '2018-12-01T00:00:00', end_at: '2018-12-11T00:00:00' }
275291
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
276292
const res = httpMocks.createResponse()
277293

@@ -284,15 +300,14 @@ describe('queryFilter()', function () {
284300
done()
285301
})
286302

287-
it('should return req.query with set period and date_end param', function (done) {
288-
303+
it('should return req.query with set period and end_at param', function (done) {
289304
const expect_filters = {
290305
$and: [
291306
{ created_at: { $lt: new Date('2018-12-09').toISOString() } },
292307
{ created_at: { $gte: new Date('2018-11-10').toISOString() } }
293308
]
294309
}
295-
const query = { period: '1m', date_end: '2018-12-09' }
310+
const query = { period: '1m', end_at: '2018-12-09' }
296311
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
297312
const res = httpMocks.createResponse()
298313

@@ -402,6 +417,31 @@ describe('queryFilter()', function () {
402417
})
403418
done()
404419
})
420+
421+
it('should return req.query with today start_at for invalid period', function (done) {
422+
423+
const now = new Date()
424+
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
425+
const beforeToday = dateToString(new Date(now.getFullYear() - 1, now.getMonth(), now.getDate()))
426+
const expect_filters = {
427+
$and: [
428+
{ created_at: { $lt: new Date(today).toISOString() } },
429+
{ created_at: { $gte: new Date(beforeToday).toISOString() } }
430+
]
431+
}
432+
433+
const query = { period: '12' }
434+
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
435+
const res = httpMocks.createResponse()
436+
437+
const options = JSON.parse(JSON.stringify(default_options))
438+
options.default.filters = expect_filters
439+
440+
qs({})(req, res, function next() {
441+
validateFilterWithDate(req, options)
442+
})
443+
done()
444+
})
405445
})
406446

407447
context('when use custom options', function () {

0 commit comments

Comments
 (0)