Skip to content

Commit

Permalink
fix(current-user): add getUserGroupIds, allow custom fields in getUse…
Browse files Browse the repository at this point in the history
…rGroups (DHIS2-10625) (#280)

* fix(current-user): allow custom fields in getUserGroups (DHIS2-10625)

This allows to override fields=:all to reduce the response size.
One example is the Interpretations component, where only the group ids
are actually needed.

* feat: add method for accessing group ids directly

Current user group ids are already available from the /me response.
Add a method for accessing them directly without making any api request.
In some cases the ids are all is needed (ie. Interpretations component).

Related to DHIS2-10625.
  • Loading branch information
edoardo authored Mar 9, 2021
1 parent a75ea8c commit d3e84a6
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/current-user/CurrentUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ class CurrentUser {
this.dataStore = UserDataStore.getUserDataStore()
}

/**
* Get a list of group ids the current user belongs to.
*
* @returns {Array} The list of ids of all the user's groups.
*/
getUserGroupIds() {
return this[propertySymbols.userGroups]
}

/**
* Get a ModelCollection of userGroup models that are assigned to the currentUser
*
Expand All @@ -157,15 +166,17 @@ class CurrentUser {
*
* The request done is equivalent do doing https://play.dhis2.org/demo/api/27/me.json?fields=userGroups[:all]
*
* @param {Object} [listOptions={}] Additional query parameters that should be send with the request.
* @returns {Promise<ModelCollection>} The model collection that contains the user's groups.
*/
getUserGroups() {
getUserGroups(listOptions = {}) {
const userGroupIds = this[propertySymbols.userGroups]

return this[models].userGroup.list({
filter: [`id:in:[${userGroupIds.join(',')}]`],
paging: false,
})
return this[models].userGroup.list(
Object.assign({ paging: false }, listOptions, {
filter: [`id:in:[${userGroupIds.join(',')}]`],
})
)
}

/**
Expand Down

0 comments on commit d3e84a6

Please sign in to comment.