@@ -12,7 +12,7 @@ import Select, { components } from 'react-select';
12
12
import { ModalContext , useTenantBackOffice } from '../../../contexts' ;
13
13
import { I18nContext } from '../../../core' ;
14
14
import * as Services from '../../../services' ;
15
- import { IOtoroshiSettings , ITeamFullGql , ITeamSimple , ResponseError , isError } from '../../../types' ;
15
+ import { IAuthorizedEntities , IOtoroshiSettings , ISimpleOtoroshiSettings , ITeamFullGql , ITeamSimple , ResponseError , isError } from '../../../types' ;
16
16
import { teamSchema } from '../../backoffice/teams/TeamEdit' ;
17
17
import { AvatarWithAction , Can , tenant as TENANT , manage } from '../../utils' ;
18
18
@@ -65,14 +65,90 @@ export const TeamList = () => {
65
65
66
66
const navigate = useNavigate ( ) ;
67
67
68
+ const teamSchemaForAdmin = ( team : ITeamSimple , _otoroshis : ISimpleOtoroshiSettings [ ] ) => ( {
69
+ ...teamSchema ( team , translate ) ,
70
+ apisCreationPermission : {
71
+ type : type . bool ,
72
+ defaultValue : false ,
73
+ label : translate ( 'APIs creation permission' ) ,
74
+ help : translate ( 'apisCreationPermission.help' ) ,
75
+ visible : ! ! tenant . creationSecurity
76
+ } ,
77
+ metadata : {
78
+ type : type . object ,
79
+ label : translate ( 'Metadata' ) ,
80
+ } ,
81
+ authorizedOtoroshiEntities : {
82
+ type : type . object ,
83
+ array : true ,
84
+ label : translate ( 'authorizedOtoroshiEntities' ) ,
85
+ format : format . form ,
86
+ schema : {
87
+ otoroshiSettingsId : {
88
+ type : type . string ,
89
+ format : format . select ,
90
+ label : translate ( 'Otoroshi instances' ) ,
91
+ optionsFrom : ( ) => Promise . resolve ( _otoroshis ) ,
92
+ transformer : ( s : IOtoroshiSettings ) => ( {
93
+ label : s . url ,
94
+ value : s . _id
95
+ } ) ,
96
+ constraints : [
97
+ constraints . required ( translate ( 'constraints.required.value' ) )
98
+ ]
99
+ } ,
100
+ authorizedEntities : {
101
+ type : type . object ,
102
+ visible : ( props ) => {
103
+ return ! ! props . rawValues . authorizedOtoroshiEntities [ props . informations ?. parent ?. index || 0 ] . value . otoroshiSettingsId
104
+ } ,
105
+ deps : [ 'authorizedOtoroshiEntities.otoroshiSettingsId' ] ,
106
+ render : ( props ) => OtoroshiEntitiesSelector ( { ...props , translate, targetKey : "authorizedOtoroshiEntities" } ) ,
107
+ label : translate ( 'Authorized entities' ) ,
108
+ placeholder : translate ( 'Authorized.entities.placeholder' ) ,
109
+ help : translate ( 'authorized.entities.help' ) ,
110
+ defaultValue : { routes : [ ] , services : [ ] , groups : [ ] }
111
+ } ,
112
+ }
113
+ }
114
+ } )
115
+
116
+ const sanitizeTeamAuthorizedEntities = ( team : ITeamSimple ) => {
117
+ return Promise . resolve ( {
118
+ ...team ,
119
+ authorizedOtoroshiEntities : team . authorizedOtoroshiEntities . reduce < Array < { otoroshiSettingsId : string , authorizedEntities : IAuthorizedEntities } > > ( ( acc , curr ) => {
120
+ if ( acc . some ( x => x . otoroshiSettingsId === curr . otoroshiSettingsId ) ) {
121
+ const authorizedEntities = acc . find ( x => x . otoroshiSettingsId === curr . otoroshiSettingsId ) ! . authorizedEntities
122
+ return [
123
+ ...acc . filter ( x => x . otoroshiSettingsId !== curr . otoroshiSettingsId ) ,
124
+ {
125
+ otoroshiSettingsId : curr . otoroshiSettingsId ,
126
+ authorizedEntities : {
127
+ groups : [ ...new Set ( [ ...curr . authorizedEntities . groups , ...authorizedEntities . groups ] ) ] ,
128
+ services : [ ...new Set ( [ ...curr . authorizedEntities . services , ...authorizedEntities . services ] ) ] ,
129
+ routes : [ ...new Set ( [ ...curr . authorizedEntities . routes , ...authorizedEntities . routes ] ) ]
130
+ }
131
+ } ]
132
+ } else {
133
+ return [ ...acc , curr ]
134
+ }
135
+ } , [ ] )
136
+ } )
137
+ }
138
+
68
139
const createNewTeam = ( ) => {
69
- Services . fetchNewTeam ( )
70
- . then ( ( newTeam ) => {
140
+ Promise . all ( [
141
+ Services . fetchNewTeam ( ) ,
142
+ Services . allSimpleOtoroshis ( tenant . _id )
143
+ ] )
144
+ . then ( ( [ newTeam , otoroshis ] ) => {
145
+ const _otoroshis = isError ( otoroshis ) ? [ ] : otoroshis
71
146
openFormModal ( {
72
147
title : translate ( 'Create a new team' ) ,
73
148
actionLabel : translate ( 'Create' ) ,
74
- schema : teamSchema ( newTeam , translate ) ,
75
- onSubmit : ( data : ITeamSimple ) => Services . createTeam ( data )
149
+ schema : teamSchemaForAdmin ( newTeam , _otoroshis ) ,
150
+ onSubmit : ( data : ITeamSimple ) => sanitizeTeamAuthorizedEntities ( data )
151
+ . then ( team => Services . createTeam ( team ) )
76
152
. then ( r => {
77
153
if ( r . error ) {
78
154
toastr . error ( translate ( 'Error' ) , r . error )
@@ -144,62 +220,10 @@ export const TeamList = () => {
144
220
openFormModal ( {
145
221
title : translate ( 'Update team' ) ,
146
222
actionLabel : translate ( 'Update' ) ,
147
- schema : {
148
- ...teamSchema ( team , translate ) ,
149
- apisCreationPermission : {
150
- type : type . bool ,
151
- defaultValue : false ,
152
- label : translate ( 'APIs creation permission' ) ,
153
- help : translate ( 'apisCreationPermission.help' ) ,
154
- visible : ! ! tenant . creationSecurity
155
- } ,
156
- metadata : {
157
- type : type . object ,
158
- label : translate ( 'Metadata' ) ,
159
- } ,
160
- authorizedOtoroshiEntities : {
161
- type : type . object ,
162
- array : true ,
163
- label : translate ( 'authorizedOtoroshiEntities' ) ,
164
- format : format . form ,
165
- schema : {
166
- otoroshiSettingsId : {
167
- type : type . string ,
168
- format : format . select ,
169
- label : translate ( 'Otoroshi instances' ) ,
170
- optionsFrom : ( ) => {
171
- // const authorizedOto = props.getValue("authorizedOtoroshiEntities").map((o) => o.value.otoroshiSettingsId)
172
- // console.debug(otoroshis.filter(o => !authorizedOto.includes(o._id)), )
173
- return Promise . resolve ( _otoroshis )
174
- } ,
175
- transformer : ( s : IOtoroshiSettings ) => ( {
176
- label : s . url ,
177
- value : s . _id
178
- } ) ,
179
- constraints : [
180
- constraints . required ( )
181
- ]
182
- } ,
183
- authorizedEntities : {
184
- type : type . object ,
185
- visible : ( props ) => {
186
- return ! ! props . rawValues . authorizedOtoroshiEntities [ props . informations ?. parent ?. index || 0 ] . value . otoroshiSettingsId
187
- } ,
188
- deps : [ 'authorizedOtoroshiEntities.otoroshiSettingsId' ] ,
189
- render : ( props ) => OtoroshiEntitiesSelector ( { ...props , translate, targetKey : "authorizedOtoroshiEntities" } ) ,
190
- label : translate ( 'Authorized entities' ) ,
191
- placeholder : translate ( 'Authorized.entities.placeholder' ) ,
192
- help : translate ( 'authorized.entities.help' ) ,
193
- defaultValue : { routes : [ ] , services : [ ] , groups : [ ] }
194
- } ,
195
- } ,
196
- constraints : [
197
- constraints . max ( _otoroshis . length , "oops" )
198
- ]
199
- }
200
- } ,
223
+ schema : teamSchemaForAdmin ( team , _otoroshis ) ,
201
224
onSubmit : ( teamToUpdate ) => {
202
- return Services . updateTeam ( teamToUpdate )
225
+ return sanitizeTeamAuthorizedEntities ( teamToUpdate )
226
+ . then ( teamToUpdate => Services . updateTeam ( teamToUpdate ) )
203
227
. then ( r => {
204
228
if ( r . error ) {
205
229
toastr . error ( translate ( 'Error' ) , r . error )
@@ -356,15 +380,13 @@ const OtoroshiEntitiesSelector = ({
356
380
357
381
useEffect ( ( ) => {
358
382
if ( groups && services && routes ) {
359
- console . log ( { groups, services, routes } )
360
383
setLoading ( false ) ;
361
384
}
362
385
} , [ services , groups , routes ] ) ;
363
386
364
387
useEffect ( ( ) => {
365
388
if ( ! ! groups && ! ! services && ! ! routes && ! ! rawValues . authorizedOtoroshiEntities [ informations ?. parent ?. index || 0 ] . value ) {
366
389
const v = rawValues . authorizedOtoroshiEntities [ informations ?. parent ?. index || 0 ] . value
367
- console . log ( { v } )
368
390
setValue ( [
369
391
...v . authorizedEntities . groups . map ( ( authGroup : any ) => ( groups as any ) . find ( ( g : any ) => g . value === authGroup ) ) ,
370
392
...( v . authorizedEntities . services || [ ] ) . map ( ( authService : any ) => ( services as any ) . find ( ( g : any ) => g . value === authService ) ) ,
0 commit comments