Skip to content

Commit

Permalink
Filter events by event type (#981)
Browse files Browse the repository at this point in the history
* where clause working

* filtering

* test

* bug fix

* comment fix

* extra quote

* closing comma

* fixed

* case insensitive test
  • Loading branch information
kriscfoster authored and Cássio Dias committed Aug 17, 2018
1 parent 0d61380 commit 9a16d5c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions api/v1/controllers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ module.exports = {
req.swagger.params.limit.value = DEFAULT_LIMIT;
}

// Extracting type from params to filter by context.type
if (req.swagger.params.type && req.swagger.params.type.value) {
req.swagger.params.context = {
value: {
type: req.swagger.params.type.value,
},
};

delete req.swagger.params.type;
}

doFind(req, res, next, helper);
},

Expand Down
4 changes: 4 additions & 0 deletions api/v1/helpers/verbs/findUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ function toSequelizeWhere(filter, props) {
for (let k = ZERO; k < arr.length; k++) {
values.push(toWhereClause(arr[k]));
}
} else if (typeof v === 'object') {
for (const p in v) {
values.push({ [p]: toWhereClause(v[p]) });
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions api/v1/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3135,6 +3135,12 @@ paths:
in: query
required: false
type: string
-
name: type
description: Get events that are a specific type
in: query
required: false
type: string
-
name: sort
description: >
Expand Down
30 changes: 30 additions & 0 deletions tests/api/v1/events/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ describe('tests/api/v1/events/get.js >', () => {
let testEvent = u.getStandard();
let testEventOutput;
let testEvent2 = u.getStandard();
testEvent2.context.type = 'Comment';
testEvent2.log = 'Sample Event 2';
let testEvent3 = u.getStandard();
testEvent3.context.type = 'Comment';
testEvent3.log = 'Sample Event 3';
let token;

Expand Down Expand Up @@ -232,6 +234,34 @@ describe('tests/api/v1/events/get.js >', () => {
});
});

it('Pass, get by context type', (done) => {
api.get(`${path}?type=Comment`)
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.end((err, res) => {
if (err) {
return done(err);
}

expect(res.body.length).to.equal(TWO);
done();
});
});

it('Pass, get by context type is not case sensitive', (done) => {
api.get(`${path}?type=comment`)
.set('Authorization', token)
.expect(constants.httpStatus.OK)
.end((err, res) => {
if (err) {
return done(err);
}

expect(res.body.length).to.equal(TWO);
done();
});
});

it('Fail, id not found', (done) => {
api.get(`${path}/INVALID_ID`)
.set('Authorization', token)
Expand Down

0 comments on commit 9a16d5c

Please sign in to comment.