-
Notifications
You must be signed in to change notification settings - Fork 13.9k
feat: filter users.list by custom fields #42078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ricardogarim
wants to merge
4
commits into
develop
Choose a base branch
from
feat/users-list-custom-fields-filter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3a9e2cd
feat: filter users.list by custom fields
ricardogarim c63044e
fix: reject an empty customFields parameter on users.list
ricardogarim ec7fc30
test: cover the empty customFields parameter on users.list
ricardogarim e56dce8
fix: declare customFields on the users.list response type
ricardogarim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@rocket.chat/rest-typings": minor | ||
| "@rocket.chat/meteor": minor | ||
| --- | ||
|
|
||
| Adds `customFields` and `includeCustomFields` parameters to the `users.list` endpoint, filtering users by exact custom field values and returning the fields the caller may read |
45 changes: 45 additions & 0 deletions
45
apps/meteor/server/api/lib/parseCustomFieldsFilter.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { expect } from 'chai'; | ||
| import { describe, it } from 'mocha'; | ||
|
|
||
| import { parseCustomFieldsFilter } from './parseCustomFieldsFilter'; | ||
|
|
||
| describe('parseCustomFieldsFilter', () => { | ||
| it('should map a key to an exact-match filter', () => { | ||
| expect(parseCustomFieldsFilter('{"CustomerID":"acct-4821"}')).to.deep.equal({ | ||
| 'customFields.CustomerID': 'acct-4821', | ||
| }); | ||
| }); | ||
|
|
||
| it('should combine multiple keys', () => { | ||
| expect(parseCustomFieldsFilter('{"CustomerID":"1","BAID":"2"}')).to.deep.equal({ | ||
| 'customFields.CustomerID': '1', | ||
| 'customFields.BAID': '2', | ||
| }); | ||
| }); | ||
|
|
||
| it('should reject malformed JSON as a shape error, not an empty payload', () => { | ||
| expect(() => parseCustomFieldsFilter('{ssn:')).to.throw('customFields must be a JSON object'); | ||
| }); | ||
|
|
||
| it('should reject a payload that is not a plain object', () => { | ||
| expect(() => parseCustomFieldsFilter('["a"]')).to.throw('must be a JSON object'); | ||
| expect(() => parseCustomFieldsFilter('"a"')).to.throw('must be a JSON object'); | ||
| expect(() => parseCustomFieldsFilter('null')).to.throw('must be a JSON object'); | ||
| }); | ||
|
|
||
| it('should reject an empty object', () => { | ||
| expect(() => parseCustomFieldsFilter('{}')).to.throw('at least one field'); | ||
| }); | ||
|
|
||
| it('should reject values that are not non-empty strings', () => { | ||
| expect(() => parseCustomFieldsFilter('{"a":{"$ne":null}}')).to.throw('non-empty string'); | ||
| expect(() => parseCustomFieldsFilter('{"a":123}')).to.throw('non-empty string'); | ||
| expect(() => parseCustomFieldsFilter('{"a":["x"]}')).to.throw('non-empty string'); | ||
| expect(() => parseCustomFieldsFilter('{"a":""}')).to.throw('non-empty string'); | ||
| }); | ||
|
|
||
| it('should reject keys carrying an operator or a nested path', () => { | ||
| expect(() => parseCustomFieldsFilter('{"$where":"return true"}')).to.throw('not allowed'); | ||
| expect(() => parseCustomFieldsFilter('{"a.b":"x"}')).to.throw('not allowed'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { isRecord, wrapExceptions } from '@rocket.chat/tools'; | ||
|
|
||
| export function parseCustomFieldsFilter(raw: string): Record<string, string> { | ||
| const parsed = wrapExceptions(() => JSON.parse(raw)).suppress(); | ||
|
|
||
| if (!isRecord(parsed)) { | ||
| throw new Error('customFields must be a JSON object'); | ||
| } | ||
|
|
||
| const entries = Object.entries(parsed); | ||
|
|
||
| if (!entries.length) { | ||
| throw new Error('customFields must declare at least one field'); | ||
| } | ||
|
|
||
| return Object.fromEntries( | ||
| entries.map(([key, value]) => { | ||
| if (typeof value !== 'string' || !value) { | ||
| throw new Error(`customFields.${key} must be a non-empty string`); | ||
| } | ||
|
|
||
| if (key.includes('$') || key.includes('.')) { | ||
| throw new Error(`The given key contains a period or an operator, which is not allowed. Key: ${key}`); | ||
| } | ||
|
|
||
| return [`customFields.${key}`, value]; | ||
| }), | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.