@@ -182,20 +182,24 @@ const DesignerEditor = () => {
182
182
definition,
183
183
} ;
184
184
185
- const referencesToAdd = { ...( connectionsData ?. managedApiConnections ?? { } ) } ;
186
- if ( Object . keys ( connectionReferences ?? { } ) . length ) {
185
+ const newManagedApiConnections = { ...( connectionsData ?. managedApiConnections ?? { } ) } ;
186
+ const newServiceProviderConnections : Record < string , any > = { } ;
187
+
188
+ const referenceKeys = Object . keys ( connectionReferences ?? { } ) ;
189
+ if ( referenceKeys . length ) {
187
190
await Promise . all (
188
- Object . keys ( connectionReferences ) . map ( async ( referenceKey ) => {
191
+ referenceKeys . map ( async ( referenceKey ) => {
189
192
const reference = connectionReferences [ referenceKey ] ;
190
- if ( isArmResourceId ( reference . connection . id ) && ! referencesToAdd [ referenceKey ] ) {
193
+ if ( isArmResourceId ( reference ?. connection ?. id ) && ! newManagedApiConnections [ referenceKey ] ) {
194
+ // Managed API Connection
191
195
const {
192
196
api : { id : apiId } ,
193
197
connection : { id : connectionId } ,
194
198
connectionProperties,
195
199
} = reference ;
196
200
const connection = await getConnectionStandard ( connectionId ) ;
197
201
const userIdentity = connectionProperties ?. authentication ?. identity ;
198
- referencesToAdd [ referenceKey ] = {
202
+ const newConnectionObj = {
199
203
api : { id : apiId } ,
200
204
connection : { id : connectionId } ,
201
205
authentication : {
@@ -205,10 +209,22 @@ const DesignerEditor = () => {
205
209
connectionRuntimeUrl : connection ?. properties ?. connectionRuntimeUrl ?? '' ,
206
210
connectionProperties,
207
211
} ;
212
+ newManagedApiConnections [ referenceKey ] = newConnectionObj ;
213
+ } else if ( reference ?. connection ?. id . startsWith ( '/serviceProviders/' ) ) {
214
+ // Service Provider Connection
215
+ const connectionKey = reference . connection . id . split ( '/' ) . splice ( - 1 ) [ 0 ] ;
216
+ // We can't apply this directly in case there is a temporary key overlap
217
+ // We need to move the data out to a new object, delete the old data, then apply the new data at the end
218
+ newServiceProviderConnections [ referenceKey ] = connectionsData ?. serviceProviderConnections ?. [ connectionKey ] ;
219
+ delete connectionsData ?. serviceProviderConnections ?. [ connectionKey ] ;
208
220
}
209
221
} )
210
222
) ;
211
- ( connectionsData as ConnectionsData ) . managedApiConnections = referencesToAdd ;
223
+ ( connectionsData as ConnectionsData ) . managedApiConnections = newManagedApiConnections ;
224
+ ( connectionsData as ConnectionsData ) . serviceProviderConnections = {
225
+ ...connectionsData ?. serviceProviderConnections ,
226
+ ...newServiceProviderConnections ,
227
+ } ;
212
228
}
213
229
214
230
const connectionsToUpdate = getConnectionsToUpdate ( originalConnectionsData , connectionsData ?? { } ) ;
@@ -556,57 +572,63 @@ const addOrUpdateAppSettings = (settings: Record<string, string>, originalSettin
556
572
return originalSettings ;
557
573
} ;
558
574
575
+ const hasNewKeys = ( original : Record < string , any > = { } , updated : Record < string , any > = { } ) => {
576
+ return ! Object . keys ( updated ) . some ( ( key ) => ! Object . keys ( original ) . includes ( key ) ) ;
577
+ } ;
578
+
559
579
const getConnectionsToUpdate = (
560
580
originalConnectionsJson : ConnectionsData ,
561
581
connectionsJson : ConnectionsData
562
582
) : ConnectionsData | undefined => {
563
- const originalKeys = Object . keys ( {
564
- ...( originalConnectionsJson . functionConnections ?? { } ) ,
565
- ...( originalConnectionsJson . apiManagementConnections ?? { } ) ,
566
- ...( originalConnectionsJson . managedApiConnections ?? { } ) ,
567
- ...( originalConnectionsJson . serviceProviderConnections ?? { } ) ,
568
- } ) ;
569
-
570
- const updatedKeys = Object . keys ( {
571
- ...( connectionsJson . functionConnections ?? { } ) ,
572
- ...( connectionsJson . apiManagementConnections ?? { } ) ,
573
- ...( connectionsJson . managedApiConnections ?? { } ) ,
574
- ...( connectionsJson . serviceProviderConnections ?? { } ) ,
575
- } ) ;
583
+ const hasNewFunctionKeys = hasNewKeys ( originalConnectionsJson . functionConnections , connectionsJson . functionConnections ) ;
584
+ const hasNewApimKeys = hasNewKeys ( originalConnectionsJson . apiManagementConnections , connectionsJson . apiManagementConnections ) ;
585
+ const hasNewManagedApiKeys = hasNewKeys ( originalConnectionsJson . managedApiConnections , connectionsJson . managedApiConnections ) ;
586
+ const hasNewServiceProviderKeys = hasNewKeys (
587
+ originalConnectionsJson . serviceProviderConnections ,
588
+ connectionsJson . serviceProviderConnections
589
+ ) ;
576
590
577
- // NOTE: We don't edit connections from the workflow, so existing connections should not be changed. If no new connections are added, there was no change.
578
- if ( ! updatedKeys . some ( ( conn ) => ! originalKeys . includes ( conn ) ) ) {
591
+ if ( ! hasNewFunctionKeys && ! hasNewApimKeys && ! hasNewManagedApiKeys && ! hasNewServiceProviderKeys ) {
579
592
return undefined ;
580
593
}
581
594
582
595
const connectionsToUpdate = { ...connectionsJson } ;
583
- for ( const functionConnectionName of Object . keys ( connectionsJson . functionConnections ?? { } ) ) {
584
- if ( originalConnectionsJson . functionConnections ?. [ functionConnectionName ] ) {
585
- ( connectionsToUpdate . functionConnections as any ) [ functionConnectionName ] =
586
- originalConnectionsJson . functionConnections [ functionConnectionName ] ;
596
+
597
+ if ( hasNewFunctionKeys ) {
598
+ for ( const functionConnectionName of Object . keys ( connectionsJson . functionConnections ?? { } ) ) {
599
+ if ( originalConnectionsJson . functionConnections ?. [ functionConnectionName ] ) {
600
+ ( connectionsToUpdate . functionConnections as any ) [ functionConnectionName ] =
601
+ originalConnectionsJson . functionConnections [ functionConnectionName ] ;
602
+ }
587
603
}
588
604
}
589
605
590
- for ( const apimConnectionName of Object . keys ( connectionsJson . apiManagementConnections ?? { } ) ) {
591
- if ( originalConnectionsJson . apiManagementConnections ?. [ apimConnectionName ] ) {
592
- ( connectionsToUpdate . apiManagementConnections as any ) [ apimConnectionName ] =
593
- originalConnectionsJson . apiManagementConnections [ apimConnectionName ] ;
606
+ if ( hasNewApimKeys ) {
607
+ for ( const apimConnectionName of Object . keys ( connectionsJson . apiManagementConnections ?? { } ) ) {
608
+ if ( originalConnectionsJson . apiManagementConnections ?. [ apimConnectionName ] ) {
609
+ ( connectionsToUpdate . apiManagementConnections as any ) [ apimConnectionName ] =
610
+ originalConnectionsJson . apiManagementConnections [ apimConnectionName ] ;
611
+ }
594
612
}
595
613
}
596
614
597
- for ( const managedApiConnectionName of Object . keys ( connectionsJson . managedApiConnections ?? { } ) ) {
598
- if ( originalConnectionsJson . managedApiConnections ?. [ managedApiConnectionName ] ) {
599
- // eslint-disable-next-line no-param-reassign
600
- ( connectionsJson . managedApiConnections as any ) [ managedApiConnectionName ] =
601
- originalConnectionsJson . managedApiConnections [ managedApiConnectionName ] ;
615
+ if ( hasNewManagedApiKeys ) {
616
+ for ( const managedApiConnectionName of Object . keys ( connectionsJson . managedApiConnections ?? { } ) ) {
617
+ if ( originalConnectionsJson . managedApiConnections ?. [ managedApiConnectionName ] ) {
618
+ // eslint-disable-next-line no-param-reassign
619
+ ( connectionsJson . managedApiConnections as any ) [ managedApiConnectionName ] =
620
+ originalConnectionsJson . managedApiConnections [ managedApiConnectionName ] ;
621
+ }
602
622
}
603
623
}
604
624
605
- for ( const serviceProviderConnectionName of Object . keys ( connectionsJson . serviceProviderConnections ?? { } ) ) {
606
- if ( originalConnectionsJson . serviceProviderConnections ?. [ serviceProviderConnectionName ] ) {
607
- // eslint-disable-next-line no-param-reassign
608
- ( connectionsJson . serviceProviderConnections as any ) [ serviceProviderConnectionName ] =
609
- originalConnectionsJson . serviceProviderConnections [ serviceProviderConnectionName ] ;
625
+ if ( hasNewServiceProviderKeys ) {
626
+ for ( const serviceProviderConnectionName of Object . keys ( connectionsJson . serviceProviderConnections ?? { } ) ) {
627
+ if ( originalConnectionsJson . serviceProviderConnections ?. [ serviceProviderConnectionName ] ) {
628
+ // eslint-disable-next-line no-param-reassign
629
+ ( connectionsJson . serviceProviderConnections as any ) [ serviceProviderConnectionName ] =
630
+ originalConnectionsJson . serviceProviderConnections [ serviceProviderConnectionName ] ;
631
+ }
610
632
}
611
633
}
612
634
0 commit comments