From bbf81e41c35721c99093e3f1eb8dc8fedf7399ea Mon Sep 17 00:00:00 2001 From: Shawn Du Date: Wed, 16 Nov 2016 22:53:40 -0500 Subject: [PATCH 1/3] changes for challenge Topcoder Project Service Search Enhancements --- src/routes/projects/list.js | 28 +++++++++-- src/routes/projects/list.spec.js | 85 +++++++++++++++++++++++++++++++- swagger.yaml | 2 +- 3 files changed, 108 insertions(+), 7 deletions(-) mode change 100644 => 100755 src/routes/projects/list.js mode change 100644 => 100755 src/routes/projects/list.spec.js mode change 100644 => 100755 swagger.yaml diff --git a/src/routes/projects/list.js b/src/routes/projects/list.js old mode 100644 new mode 100755 index f0968295..987b99f8 --- a/src/routes/projects/list.js +++ b/src/routes/projects/list.js @@ -41,10 +41,28 @@ var _retrieveProjects = (req, criteria, sort, fields) => { let retrieveAttachments = !req.query.fields || req.query.fields.indexOf('attachments') > -1 let retrieveMembers = !req.query.fields || !!fields.project_members.length - - // special handling for name filter - if (_.has(criteria.filters, 'name')) { - criteria.filters.name = { ilike: `%${criteria.filters.name}%`} + // special handling for keyword filter + if (_.has(criteria.filters, 'keyword')) { + criteria.filters.$or = [ + { + name: { + $ilike: `%${criteria.filters.keyword}%` + }, + }, { + description: { + $ilike: `%${criteria.filters.keyword}%` + }, + }, { + details: { + utm: { + code: { + $ilike: `%${criteria.filters.keyword}%` + } + } + } + } + ] + delete criteria.filters.keyword } return models.Project.findAndCountAll({ @@ -117,7 +135,7 @@ module.exports = [ 'name', 'name asc', 'name desc', 'type', 'type asc', 'type desc' ] - if (!util.isValidFilter(filters, ['id', 'status', 'type', 'memberOnly', 'name']) || + if (!util.isValidFilter(filters, ['id', 'status', 'type', 'memberOnly', 'keyword']) || (sort && _.indexOf(sortableProps, sort) < 0)) { util.handleError('Invalid filters or sort', null, req, next) } diff --git a/src/routes/projects/list.spec.js b/src/routes/projects/list.spec.js old mode 100644 new mode 100755 index 9d8221b5..8e578da6 --- a/src/routes/projects/list.spec.js +++ b/src/routes/projects/list.spec.js @@ -20,7 +20,11 @@ describe('LIST Project', () => { name: 'test1', description: 'test project1', status: 'active', - details: {}, + details: { + utm: { + code: 'code1' + } + }, createdBy: 1, updatedBy: 1 }).then(p => { @@ -175,6 +179,85 @@ describe('LIST Project', () => { done() }) }) + + it('should return the project when filtering by keyword, which matches the name', done => { + request(server) + .get('/v4/projects/?filter=keyword%3D1') + .set({ + 'Authorization': 'Bearer ' + testUtil.jwts.admin + }) + .expect('Content-Type', /json/) + .expect(200) + .end(function(err, res) { + if (err) { + return done(err) + } + var resJson = res.body.result.content + should.exist(resJson) + resJson.should.have.lengthOf(1) + resJson[0].name.should.equal('test1') + done() + }) + }) + + it('should return the project when filtering by keyword, which matches the description', done => { + request(server) + .get('/v4/projects/?filter=keyword%3Dproject') + .set({ + 'Authorization': 'Bearer ' + testUtil.jwts.admin + }) + .expect('Content-Type', /json/) + .expect(200) + .end(function(err, res) { + if (err) { + return done(err) + } + var resJson = res.body.result.content + should.exist(resJson) + resJson.should.have.lengthOf(3) + done() + }) + }) + + it('should return the project when filtering by keyword, which matches the details', done => { + request(server) + .get('/v4/projects/?filter=keyword%3Dcode') + .set({ + 'Authorization': 'Bearer ' + testUtil.jwts.admin + }) + .expect('Content-Type', /json/) + .expect(200) + .end(function(err, res) { + if (err) { + return done(err) + } + var resJson = res.body.result.content + should.exist(resJson) + resJson.should.have.lengthOf(1) + resJson[0].name.should.equal('test1') + done() + }) + }) + + it('should return the project when filtering by name and keyword', done => { + request(server) + .get('/v4/projects/?filter=name%3Dtest%26keyword%3Dcode') + .set({ + 'Authorization': 'Bearer ' + testUtil.jwts.admin + }) + .expect('Content-Type', /json/) + .expect(200) + .end(function(err, res) { + if (err) { + return done(err) + } + var resJson = res.body.result.content + should.exist(resJson) + resJson.should.have.lengthOf(1) + resJson[0].name.should.equal('test1') + done() + }) + }) }) }) diff --git a/swagger.yaml b/swagger.yaml old mode 100644 new mode 100755 index 33eb66c1..ff0b6b11 --- a/swagger.yaml +++ b/swagger.yaml @@ -48,7 +48,7 @@ paths: - type - status - memberOnly - - name + - keyword - name: sort required: false description: | From c7667f9193cea0ec14fa2bdab11d9f1bca5dd3a6 Mon Sep 17 00:00:00 2001 From: Shawn Du Date: Thu, 17 Nov 2016 00:11:04 -0500 Subject: [PATCH 2/3] remove incorrect test --- src/routes/projects/list.spec.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/routes/projects/list.spec.js b/src/routes/projects/list.spec.js index 8e578da6..90c38d0b 100755 --- a/src/routes/projects/list.spec.js +++ b/src/routes/projects/list.spec.js @@ -180,9 +180,9 @@ describe('LIST Project', () => { }) }) - it('should return the project when filtering by keyword, which matches the name', done => { + it('should return all projects that match when filtering by name', done => { request(server) - .get('/v4/projects/?filter=keyword%3D1') + .get('/v4/projects/?filter=keyword%3Dtest') .set({ 'Authorization': 'Bearer ' + testUtil.jwts.admin }) @@ -194,15 +194,14 @@ describe('LIST Project', () => { } var resJson = res.body.result.content should.exist(resJson) - resJson.should.have.lengthOf(1) - resJson[0].name.should.equal('test1') + resJson.should.have.lengthOf(3) done() }) }) - it('should return the project when filtering by keyword, which matches the description', done => { + it('should return the project when filtering by keyword, which matches the name', done => { request(server) - .get('/v4/projects/?filter=keyword%3Dproject') + .get('/v4/projects/?filter=keyword%3D1') .set({ 'Authorization': 'Bearer ' + testUtil.jwts.admin }) @@ -214,14 +213,15 @@ describe('LIST Project', () => { } var resJson = res.body.result.content should.exist(resJson) - resJson.should.have.lengthOf(3) + resJson.should.have.lengthOf(1) + resJson[0].name.should.equal('test1') done() }) }) - it('should return the project when filtering by keyword, which matches the details', done => { + it('should return the project when filtering by keyword, which matches the description', done => { request(server) - .get('/v4/projects/?filter=keyword%3Dcode') + .get('/v4/projects/?filter=keyword%3Dproject') .set({ 'Authorization': 'Bearer ' + testUtil.jwts.admin }) @@ -233,15 +233,14 @@ describe('LIST Project', () => { } var resJson = res.body.result.content should.exist(resJson) - resJson.should.have.lengthOf(1) - resJson[0].name.should.equal('test1') + resJson.should.have.lengthOf(3) done() }) }) - it('should return the project when filtering by name and keyword', done => { + it('should return the project when filtering by keyword, which matches the details', done => { request(server) - .get('/v4/projects/?filter=name%3Dtest%26keyword%3Dcode') + .get('/v4/projects/?filter=keyword%3Dcode') .set({ 'Authorization': 'Bearer ' + testUtil.jwts.admin }) @@ -258,6 +257,5 @@ describe('LIST Project', () => { done() }) }) - }) }) From 638e81b4e2a16c6ae8f500a2e4f66256fadfc725 Mon Sep 17 00:00:00 2001 From: Shawn Du Date: Thu, 17 Nov 2016 00:13:19 -0500 Subject: [PATCH 3/3] add back missing brackets --- src/routes/projects/list.spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/routes/projects/list.spec.js b/src/routes/projects/list.spec.js index 90c38d0b..3fec4a6f 100755 --- a/src/routes/projects/list.spec.js +++ b/src/routes/projects/list.spec.js @@ -257,5 +257,6 @@ describe('LIST Project', () => { done() }) }) - + }) + })