@@ -381,15 +381,31 @@ export const mappingProcess = async (
381
381
// Resolution des representations & markings - refIds = default values for representation attributes
382
382
const { representations, user_chosen_markings } = mapper ;
383
383
384
- const representationEntities = representations
385
- . filter ( ( r ) => r . type === CsvMapperRepresentationType . Entity )
386
- . sort ( ( r1 , r2 ) => r1 . attributes . filter ( ( attr ) => attr . based_on ) . length - r2 . attributes . filter ( ( attr ) => attr . based_on ) . length ) ;
387
384
const representationRelationships = representations . filter ( ( r ) => r . type === CsvMapperRepresentationType . Relationship ) ;
385
+
386
+ const representationEntitiesWithoutBasedOnRelationships = representations
387
+ . filter ( ( r ) => {
388
+ const isEntity = r . type === CsvMapperRepresentationType . Entity ;
389
+ const entityHasRefToRelations = ! r . attributes . some ( ( a ) => {
390
+ // Check for each attribute of entity if it has based_on representations
391
+ return a . based_on ?. representations ?. some ( ( b ) => {
392
+ // Check if at least one of based_on ref is a relation in CSV Mapper
393
+ return representationRelationships . some ( ( rel ) => rel . id === b ) ;
394
+ } ) ;
395
+ } ) ;
396
+ return isEntity && entityHasRefToRelations ;
397
+ } )
398
+ . sort ( ( r1 , r2 ) => r1 . attributes . filter ( ( attr ) => attr . based_on ) . length - r2 . attributes . filter ( ( attr ) => attr . based_on ) . length ) ;
399
+
400
+ // representations thar are not in representationEntitiesWithoutBasedOnRelationships
401
+ const representationEntitiesWithBasedOnRelationships = representations
402
+ . filter ( ( r ) => r . type === CsvMapperRepresentationType . Entity && ! representationEntitiesWithoutBasedOnRelationships . some ( ( r1 ) => r1 . id === r . id ) ) ;
403
+
388
404
const results = new Map < string , Record < string , InputType > > ( ) ;
389
405
390
406
// 1. entities sort by no based on at first
391
- for ( let i = 0 ; i < representationEntities . length ; i += 1 ) {
392
- const representation = representationEntities [ i ] ;
407
+ for ( let i = 0 ; i < representationEntitiesWithoutBasedOnRelationships . length ; i += 1 ) {
408
+ const representation = representationEntitiesWithoutBasedOnRelationships [ i ] ;
393
409
const input = await mapRecord (
394
410
context ,
395
411
user ,
@@ -420,5 +436,22 @@ export const mappingProcess = async (
420
436
results . set ( representation . id , input ) ;
421
437
}
422
438
}
439
+
440
+ // 3. entities with based on relationships at last
441
+ for ( let i = 0 ; i < representationEntitiesWithBasedOnRelationships . length ; i += 1 ) {
442
+ const representation = representationEntitiesWithBasedOnRelationships [ i ] ;
443
+ const input = await mapRecord (
444
+ context ,
445
+ user ,
446
+ record ,
447
+ representation ,
448
+ results ,
449
+ refEntities ,
450
+ user_chosen_markings ?? [ ]
451
+ ) ;
452
+ if ( input ) {
453
+ results . set ( representation . id , input ) ;
454
+ }
455
+ }
423
456
return Array . from ( results . values ( ) ) ;
424
457
} ;
0 commit comments