Skip to content

Commit 42cb4ab

Browse files
committed
fix: api for user and api token list
1 parent 750889e commit 42cb4ab

File tree

5 files changed

+35
-36
lines changed

5 files changed

+35
-36
lines changed

src/Common/Constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export const ROUTES = {
103103
PLUGIN_GLOBAL_LIST_V2: 'plugin/global/list/v2',
104104
PLUGIN_GLOBAL_LIST_TAGS: 'plugin/global/list/tags',
105105
DEPLOYMENT_CHARTS_LIST: 'deployment/template/fetch',
106+
USER_LIST_MIN: 'user/list/min',
106107
}
107108

108109
export enum KEY_VALUE {

src/Pages/GlobalConfigurations/Authorization/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export * from './constants'
1818
export type { UserListFilterParams, UserRoleGroup, UserGroupDTO, UserGroupType } from './types'
1919
export * from './shared'
2020
export * from './service'
21+
export { getUserAndApiTokenOption } from './utils'
Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,34 @@
1-
import { get, getUrlWithSearchParams } from '@Common/index'
1+
import { get, ResponseType, ROUTES } from '@Common/index'
22
import { SelectPickerCustomOptionType } from '@Shared/Components'
33
import { API_TOKEN_PREFIX } from '@Shared/constants'
44
import { stringComparatorBySortOrder } from '@Shared/Helpers'
5-
import { DefaultUserKey, BaseFilterQueryParams } from '@Shared/types'
5+
import { DefaultUserKey } from '@Shared/types'
66
import { GroupBase } from 'react-select'
7+
import { UserMinType } from './types'
8+
import { getUserAndApiTokenOption } from './utils'
79

810
// FIXME: Common out the typing and url from dashboard
911
export const getUserAndApiTokenOptions = async (): Promise<GroupBase<SelectPickerCustomOptionType<string>>[]> => {
10-
const userUrl = getUrlWithSearchParams('user/v2', {
11-
showAll: true,
12-
} as BaseFilterQueryParams<never>)
12+
const { result } = (await get(ROUTES.USER_LIST_MIN)) as ResponseType<UserMinType[]>
1313

14-
const [userPromise, apiTokenPromise] = await Promise.allSettled([get(userUrl), get('api-token')])
15-
16-
const options: GroupBase<SelectPickerCustomOptionType<string>>[] = []
17-
18-
if (userPromise.status === 'fulfilled') {
19-
const { result: userResponse } = userPromise.value
20-
21-
options.push({
22-
label: 'Users',
23-
options: (userResponse?.users ?? [])
24-
.sort((a, b) => stringComparatorBySortOrder(a.email_id, b.email_id))
25-
.filter(({ email_id: emailId }) => emailId !== DefaultUserKey.system)
26-
.map(({ email_id: emailId }) => ({
27-
label: emailId,
28-
value: emailId,
29-
})),
30-
})
14+
if (!result) {
15+
return []
3116
}
3217

33-
if (apiTokenPromise.status === 'fulfilled') {
34-
const { result: apiTokenResponse } = apiTokenPromise.value
18+
const sortedEmailList = result.map(({ emailId }) => emailId).sort(stringComparatorBySortOrder)
3519

36-
options.push({
20+
return [
21+
{
22+
label: 'Users',
23+
options: sortedEmailList
24+
.filter((emailId) => emailId !== DefaultUserKey.system && !emailId.startsWith(API_TOKEN_PREFIX))
25+
.map((emailId) => getUserAndApiTokenOption(emailId)),
26+
},
27+
{
3728
label: 'API Tokens',
38-
options: (apiTokenResponse ?? [])
39-
.sort((a, b) => stringComparatorBySortOrder(a.userIdentifier, b.userIdentifier))
40-
.map(({ userIdentifier }) => ({
41-
// Remove the API Token Prefix
42-
label: userIdentifier.startsWith(API_TOKEN_PREFIX) ? userIdentifier.split(':')[1] : userIdentifier,
43-
value: userIdentifier,
44-
})),
45-
})
46-
}
47-
48-
return options
29+
options: sortedEmailList
30+
.filter((emailId) => emailId.startsWith(API_TOKEN_PREFIX))
31+
.map((emailId) => getUserAndApiTokenOption(emailId)),
32+
},
33+
]
4934
}

src/Pages/GlobalConfigurations/Authorization/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export type UserListFilterParams = BaseFilterQueryParams<UserListSortableKeys> &
2424
status: UserStatus[]
2525
}
2626

27+
export interface UserMinType {
28+
id: number
29+
emailId: string
30+
}
31+
2732
export interface UserRoleGroup {
2833
/**
2934
* Id of the permission group
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { SelectPickerCustomOptionType } from '@Shared/Components'
2+
import { API_TOKEN_PREFIX } from '@Shared/constants'
3+
4+
export const getUserAndApiTokenOption = (emailId: string): SelectPickerCustomOptionType<string> => ({
5+
label: emailId.startsWith(API_TOKEN_PREFIX) ? emailId.split(':')[1] : emailId,
6+
value: emailId,
7+
})

0 commit comments

Comments
 (0)