@@ -22,7 +22,9 @@ interface MatchResult {
2222 * Filter function to match a local and a remote source by type and handle
2323 */
2424const sameTypeAndName = ( local : LocalSource , remote : RemoteSource ) => {
25- return remote . type === local . graphQLType && slugify ( remote . title ) === slugify ( local . handle )
25+ return (
26+ remote . type . toLowerCase ( ) === local . graphQLType . toLowerCase ( ) && slugify ( remote . title ) === slugify ( local . handle )
27+ )
2628}
2729
2830/**
@@ -67,26 +69,42 @@ function matchByNameAndType(
6769/**
6870 * Automatically match local and remote sources if they have the same UID
6971 */
70- function matchByUUID (
72+ function matchByUIDandUUID (
7173 local : LocalSource [ ] ,
7274 remote : RemoteSource [ ] ,
75+ ids : IdentifiersExtensions ,
7376) : {
7477 matched : IdentifiersExtensions
7578 toCreate : LocalSource [ ]
7679 toConfirm : { local : LocalSource ; remote : RemoteSource } [ ]
7780 toManualMatch : { local : LocalSource [ ] ; remote : RemoteSource [ ] }
7881} {
7982 const matched : IdentifiersExtensions = { }
83+ const pendingLocal : LocalSource [ ] = [ ]
8084
85+ // First, try to match by UID, then by UUID.
8186 local . forEach ( ( localSource ) => {
82- const possibleMatch = remote . find ( ( remoteSource ) => remoteSource . uuid === localSource . uid )
83- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
84- if ( possibleMatch ) matched [ localSource . localIdentifier ] = possibleMatch . uuid !
87+ const matchByUID = remote . find ( ( remoteSource ) => remoteSource . id === localSource . uid )
88+ const matchByUUID = remote . find ( ( remoteSource ) => remoteSource . uuid === ids [ localSource . localIdentifier ] )
89+
90+ if ( matchByUID ) {
91+ matched [ localSource . localIdentifier ] = matchByUID . id
92+ } else if ( matchByUUID ) {
93+ matched [ localSource . localIdentifier ] = matchByUUID . uuid
94+ } else {
95+ pendingLocal . push ( localSource )
96+ }
8597 } )
8698
87- const toCreate = local . filter ( ( elem ) => ! matched [ elem . localIdentifier ] )
99+ const pendingRemote = remote . filter (
100+ ( remoteSource ) =>
101+ ! Object . values ( matched ) . includes ( remoteSource . uuid ) && ! Object . values ( matched ) . includes ( remoteSource . id ) ,
102+ )
103+
104+ // Then, try to match by name and type as a last resort.
105+ const { matched : matchedByName , toCreate, toConfirm, toManualMatch} = matchByNameAndType ( pendingLocal , pendingRemote )
88106
89- return { matched, toCreate, toConfirm : [ ] , toManualMatch : { local : [ ] , remote : [ ] } }
107+ return { matched : { ... matched , ... matchedByName } , toCreate, toConfirm, toManualMatch}
90108}
91109
92110function migrateLegacyFunctions (
@@ -225,7 +243,7 @@ export async function automaticMatchmaking(
225243 const { local, remote} = pendingAfterMigratingFunctions
226244
227245 const { matched, toCreate, toConfirm, toManualMatch} = useUuidMatching
228- ? matchByUUID ( local , remote )
246+ ? matchByUIDandUUID ( localSources , remoteSources , ids )
229247 : matchByNameAndType ( local , remote )
230248
231249 return {
0 commit comments