-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle user auth group extra props (#248)
* handle user auth group extra props * export UserAuthGroup --------- Co-authored-by: Stefano Ricci <[email protected]>
- Loading branch information
Showing
5 changed files
with
54 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,23 +1,33 @@ | ||
import { User } from './user' | ||
import { AuthGroup } from './authGroup' | ||
import { User, UserAuthGroup } from './user' | ||
import { AuthGroups } from './authGroups' | ||
import { Dictionary } from '../common' | ||
|
||
const getAuthGroups = (user: User): AuthGroup[] => user?.authGroups ?? [] | ||
const getAuthGroups = (user: User): UserAuthGroup[] => user?.authGroups ?? [] | ||
|
||
const isSystemAdmin = (user: User): boolean => (getAuthGroups(user) ?? []).some(AuthGroups.isSystemAdmin) | ||
|
||
const getAuthGroupBySurveyUuid = | ||
(surveyUuid?: string, includeSystemAdmin = true) => | ||
(user: User): AuthGroup | undefined => { | ||
(user: User): UserAuthGroup | undefined => { | ||
const authGroups = getAuthGroups(user) | ||
if (includeSystemAdmin && isSystemAdmin(user) && authGroups) { | ||
return authGroups[0] | ||
} | ||
if (!authGroups) return undefined | ||
return includeSystemAdmin && isSystemAdmin(user) | ||
? authGroups[0] | ||
: authGroups.find((authGroup) => authGroup.surveyUuid === surveyUuid) | ||
} | ||
|
||
return authGroups?.find((authGroup) => authGroup?.surveyUuid === surveyUuid) | ||
const getCombinedExtraProps = | ||
(surveyUuid: string) => | ||
(user: User): Dictionary<any> => { | ||
const userSurveyAuthGroup = user ? Users.getAuthGroupBySurveyUuid(surveyUuid)(user) : null | ||
const userAuthGroupExtraProps = userSurveyAuthGroup?.props?.extra ?? {} | ||
const userExtraProps = user?.props.extra ?? {} | ||
// user auth group extra props overwrite user extra props | ||
return { ...userExtraProps, ...userAuthGroupExtraProps } | ||
} | ||
|
||
export const Users = { | ||
isSystemAdmin, | ||
getAuthGroupBySurveyUuid, | ||
getCombinedExtraProps, | ||
} |
This file contains 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 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