@@ -6,7 +6,7 @@ import _ from 'lodash'
6
6
import { EnvironmentConfig } from '~/config'
7
7
import { xhrDeleteAsync , xhrGetAsync , xhrPostAsync } from '~/libs/core'
8
8
9
- import { adjustUserRoleResponse , ApiV3Response , UserRole } from '../models'
9
+ import { adjustUserRoleResponse , UserRole } from '../models'
10
10
11
11
/**
12
12
* Fetchs roles of the specified subject
@@ -17,11 +17,11 @@ import { adjustUserRoleResponse, ApiV3Response, UserRole } from '../models'
17
17
export const fetchRolesBySubject = async (
18
18
subjectId : string ,
19
19
) : Promise < UserRole [ ] > => {
20
- const result = await xhrGetAsync < ApiV3Response < UserRole [ ] > > (
21
- `${ EnvironmentConfig . API . V3 } /roles/?filter=subjectID=${ subjectId } ` ,
20
+ const roles = await xhrGetAsync < UserRole [ ] > (
21
+ `${ EnvironmentConfig . API . V6 } /roles/?filter=subjectID=${ subjectId } ` ,
22
22
)
23
- const roles = result . result . content . map ( adjustUserRoleResponse )
24
- return _ . orderBy ( roles , [ 'roleName' ] , [ 'asc' ] )
23
+ const adjusted = roles . map ( adjustUserRoleResponse )
24
+ return _ . orderBy ( adjusted , [ 'roleName' ] , [ 'asc' ] )
25
25
}
26
26
27
27
/**
@@ -30,11 +30,11 @@ export const fetchRolesBySubject = async (
30
30
* by names.
31
31
*/
32
32
export const fetchRoles = async ( ) : Promise < UserRole [ ] > => {
33
- const result = await xhrGetAsync < ApiV3Response < UserRole [ ] > > (
34
- `${ EnvironmentConfig . API . V3 } /roles` ,
33
+ const roles = await xhrGetAsync < UserRole [ ] > (
34
+ `${ EnvironmentConfig . API . V6 } /roles` ,
35
35
)
36
- const roles = result . result . content . map ( adjustUserRoleResponse )
37
- return _ . orderBy ( roles , [ 'roleName' ] , [ 'asc' ] )
36
+ const adjusted = roles . map ( adjustUserRoleResponse )
37
+ return _ . orderBy ( adjusted , [ 'roleName' ] , [ 'asc' ] )
38
38
}
39
39
40
40
/**
@@ -43,15 +43,15 @@ export const fetchRoles = async (): Promise<UserRole[]> => {
43
43
* @returns resolves to the role object, if success.
44
44
*/
45
45
export const createRole = async ( roleName : string ) : Promise < UserRole > => {
46
- const result = await xhrPostAsync < any , ApiV3Response < UserRole > > (
47
- `${ EnvironmentConfig . API . V3 } /roles` ,
46
+ const response = await xhrPostAsync < any , UserRole > (
47
+ `${ EnvironmentConfig . API . V6 } /roles` ,
48
48
{
49
49
param : {
50
50
roleName,
51
51
} ,
52
52
} ,
53
53
)
54
- return adjustUserRoleResponse ( result . result . content )
54
+ return adjustUserRoleResponse ( response )
55
55
}
56
56
57
57
/**
@@ -63,13 +63,10 @@ export const createRole = async (roleName: string): Promise<UserRole> => {
63
63
export const assignRole = async (
64
64
roleId : string ,
65
65
userId : string ,
66
- ) : Promise < string > => {
67
- const result = await xhrPostAsync < undefined , ApiV3Response < string > > (
68
- `${ EnvironmentConfig . API . V3 } /roles/${ roleId } /assign?action=true&filter=subjectID%3D${ userId } ` ,
69
- undefined ,
70
- )
71
- return result . result . content
72
- }
66
+ ) : Promise < string > => xhrPostAsync < undefined , string > (
67
+ `${ EnvironmentConfig . API . V6 } /roles/${ roleId } /assign?action=true&filter=subjectID%3D${ userId } ` ,
68
+ undefined ,
69
+ )
73
70
74
71
/**
75
72
* Unassigns role from the user.
@@ -80,12 +77,9 @@ export const assignRole = async (
80
77
export const unassignRole = async (
81
78
roleId : string ,
82
79
userId : string ,
83
- ) : Promise < string > => {
84
- const result = await xhrDeleteAsync < ApiV3Response < string > > (
85
- `${ EnvironmentConfig . API . V3 } /roles/${ roleId } /deassign?action=true&filter=subjectID%3D${ userId } ` ,
86
- )
87
- return result . result . content
88
- }
80
+ ) : Promise < string > => xhrDeleteAsync < string > (
81
+ `${ EnvironmentConfig . API . V6 } /roles/${ roleId } /deassign?action=true&filter=subjectID%3D${ userId } ` ,
82
+ )
89
83
90
84
/**
91
85
* Fetchs role info
@@ -97,67 +91,46 @@ export const fetchRole = async (
97
91
roleId : string ,
98
92
fields : string [ ] ,
99
93
) : Promise < UserRole > => {
100
- // there is a bug in backend, when we ask to get role subjects
101
- // but there are no subjects, backend returns 404 even if role exists
102
- // as a workaround we get role without subjects first to check if it exists
103
- // and only after we try to get it subject
104
- // TODO: remove code in this if, after this bug is fixed at the backend
105
- // keep only the part after else
94
+ const baseUrl = `${ EnvironmentConfig . API . V6 } /roles/${ roleId } `
95
+
106
96
if ( fields && _ . includes ( fields , 'subjects' ) ) {
107
- const fieldsWithouSubjects = _ . without ( fields , ' subjects' )
108
- // if there are no fields after removing 'subjects', add 'id' to retrieve minimum data
109
- if ( ! fieldsWithouSubjects . length ) {
110
- fieldsWithouSubjects . push ( 'id' )
97
+ // Work around backend returning 404 when requesting subjects for empty roles.
98
+ const fieldsWithoutSubjects = _ . without ( fields , 'subjects' )
99
+ if ( ! fieldsWithoutSubjects . length ) {
100
+ fieldsWithoutSubjects . push ( 'id' )
111
101
}
112
102
113
- const fieldsQuery = fields
114
- ? `?fields=${ fieldsWithouSubjects . join ( ',' ) } `
115
- : ''
116
-
117
- return xhrGetAsync < ApiV3Response < UserRole > > (
118
- `${ EnvironmentConfig . API . V3 } /roles/${ roleId } ${ fieldsQuery } ` ,
103
+ const fieldsQuery = `?fields=${ fieldsWithoutSubjects . join ( ',' ) } `
104
+ const roleWithoutSubjects = await xhrGetAsync < UserRole > (
105
+ `${ baseUrl } ${ fieldsQuery } ` ,
119
106
)
120
- . then ( async ( res : ApiV3Response < UserRole > ) => {
121
- const roleWithoutSubjects = res . result . content
122
107
123
- // now let's try to get subjects
124
- return xhrGetAsync < ApiV3Response < UserRole > > (
125
- `${ EnvironmentConfig . API . V3 } /roles/${ roleId } ?fields=subjects` ,
126
- )
127
- // populate role with subjects and return it
128
- . then ( ( resChild : ApiV3Response < UserRole > ) => _ . assign (
129
- roleWithoutSubjects ,
130
- {
131
- subjects : resChild . result . content . subjects ,
132
- } ,
133
- ) )
134
- . catch ( ( error : any ) => {
135
- // if get error 404 in this case we know role exits
136
- // so just return roleWithoutSubjects with subjects as en empty array
137
- if (
138
- error . data
139
- && error . data . result
140
- && error . data . result . status === 404
141
- ) {
142
- return adjustUserRoleResponse (
143
- _ . assign ( roleWithoutSubjects , {
144
- subjects : [ ] ,
145
- } ) ,
146
- )
147
-
148
- }
149
-
150
- // for other errors return rejected promise with error
151
- return Promise . reject ( error )
152
- } )
108
+ try {
109
+ const subjectsResponse = await xhrGetAsync < UserRole > (
110
+ `${ baseUrl } ?fields=subjects` ,
111
+ )
112
+ const mergedRole = _ . assign ( { } , roleWithoutSubjects , {
113
+ subjects : subjectsResponse . subjects ,
153
114
} )
115
+ return adjustUserRoleResponse ( mergedRole )
116
+ } catch ( error : any ) {
117
+ const statusCode = error ?. data ?. result ?. status
118
+ ?? error ?. response ?. status
119
+ ?? error ?. status
154
120
121
+ if ( statusCode === 404 ) {
122
+ return adjustUserRoleResponse (
123
+ _ . assign ( { } , roleWithoutSubjects , { subjects : [ ] } ) ,
124
+ )
125
+ }
126
+
127
+ throw error
128
+ }
155
129
}
156
130
157
- // if don't ask for subjects, then just normal request
158
- const fieldsQuery = fields ? `?fields=${ fields . join ( ',' ) } ` : ''
159
- const result = await xhrGetAsync < ApiV3Response < UserRole > > (
160
- `${ EnvironmentConfig . API . V3 } /roles/${ roleId } ${ fieldsQuery } ` ,
131
+ const fieldsQuery = fields ?. length ? `?fields=${ fields . join ( ',' ) } ` : ''
132
+ const response = await xhrGetAsync < UserRole > (
133
+ `${ baseUrl } ${ fieldsQuery } ` ,
161
134
)
162
- return adjustUserRoleResponse ( result . result . content )
135
+ return adjustUserRoleResponse ( response )
163
136
}
0 commit comments