Skip to content

Commit

Permalink
fix(general): concat fields for searching. Fixes MEMB-855 (#60)
Browse files Browse the repository at this point in the history
* fix(general): concat fields for searching. Fixes MEMB-855
* chore(ci): not building on master
  • Loading branch information
serge1peshcoff authored Jul 2, 2020
1 parent ce6506a commit 4edf22e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ workflows:
- eslint
build:
jobs:
- build
- build:
filters:
branches:
ignore: master
yamllint:
jobs:
- yamllint
Expand Down
18 changes: 9 additions & 9 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ module.exports = {
CAMPAIGN: ['name', 'url', 'description_short', 'description_long'],
CIRCLE: ['name', 'description'],
JOIN_REQUEST: [
'$user.first_name$',
'$user.last_name$',
'$user.email$',
'user.first_name',
'user.last_name',
'user.email',
],
BODY_MEMBERSHIP: [
'$user.first_name$',
'$user.last_name$',
'$user.email$',
'user.first_name',
'user.last_name',
'user.email',
],
CIRCLE_MEMBERSHIP: [
'$user.first_name$',
'$user.last_name$',
'$user.email$',
'user.first_name',
'user.last_name',
'user.email',
],
MEMBER: ['first_name', 'last_name', 'email'],
PERMISSION: ['combined', 'description']
Expand Down
30 changes: 19 additions & 11 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ function getSorting(query) {

/*
Given a string like 'query' and fields like ['field1', 'field2'...]
returns an array like following:
returns an object like following:
[
{ field1: { [Sequelize.Op.iLike]: '%query%' } },
{ field2: { [Sequelize.Op.iLike]: '%query%' } },
...
]
{
names: Sequelize.where(
Sequelize.fn('concat', Sequelize.col('field1'), ' ', 'Sequelize.col('field2'), ...)
{ [Sequelize.Op.iLike]: '%query%' }
}
}
Required for filtering stuff.
*/
Expand All @@ -89,12 +90,19 @@ function filterBy(query, fields) {
return {};
}

const concatFields = [];
for (let i = 0; i < fields.length; i++) {
concatFields.push(Sequelize.col(fields[i]));
if (i < fields.length - 1) {
concatFields.push(' ');
}
}

return {
[Sequelize.Op.or]: fields.map((field) => {
return {
[field]: { [Sequelize.Op.iLike]: '%' + query + '%' }
};
})
namesQuery: Sequelize.where(
Sequelize.fn('concat', ...concatFields),
{ [Sequelize.Op.iLike]: '%' + query + '%' }
)
};
}

Expand Down

0 comments on commit 4edf22e

Please sign in to comment.