@@ -490,96 +490,43 @@ export class FlowService {
490490 return mappedParkedParentOrganizations ;
491491 }
492492
493- async getParkedParentFlowsByFlowObjectFilter (
493+ /**
494+ * (All parents are considered `parked`, if they are not parked, it means there is corruption in the data)
495+ */
496+ async getParkedParentsChildrenByFlowObjectFilter (
494497 models : Database ,
495498 flowObjectFilters : FlowObjectFilterGrouped
496499 ) : Promise < UniqueFlowEntity [ ] > {
497- // 1. Retrieve the parked category
498- const parkedCategory = await models . category . findOne ( {
499- where : {
500- name : 'Parked' ,
501- group : 'flowType' ,
502- } ,
503- } ) ;
504- if ( ! parkedCategory ) {
505- throw new Error ( 'Parked category not found' ) ;
506- }
500+ // 1. Create where conditions from flow object filters
501+ const flowObjectsWhere =
502+ buildWhereConditionsForFlowObjectFilters ( flowObjectFilters ) ;
507503
508- // 2. Get all category references for parked flows
509- const categoryRefs = await models . categoryRef . find ( {
510- where : {
511- categoryID : parkedCategory . id ,
512- objectType : 'flow' ,
513- } ,
514- distinct : [ 'objectID' , 'versionID' ] ,
515- } ) ;
504+ // 2. Extract number of conditions from flow object filters
505+ const numberOfConditions = flowObjectFilters
506+ . values ( )
507+ . flatMap ( ( m ) => [ ...m . values ( ) ] )
508+ . toArray ( )
509+ . flat ( ) . length ;
516510
517- // Build list of parent IDs from categoryRefs
518- const parentIDs : FlowId [ ] = categoryRefs . map ( ( ref ) =>
519- createBrandedValue ( ref . objectID )
511+ // 3. Retrieve flow objects matching the conditions
512+ const flowObjects = await this . flowObjectService . getFlowFromFlowObjects (
513+ models ,
514+ flowObjectsWhere ,
515+ numberOfConditions
520516 ) ;
521517
522- // 3 . Retrieve flow links where the parent is among those references and depth > 0
518+ // 4 . Retrieve flow links where the parent is among those references and depth > 0
523519 const flowLinks = await models . flowLink . find ( {
524520 where : {
525521 depth : { [ Op . GT ] : 0 } ,
526- parentID : { [ Op . IN ] : parentIDs } ,
522+ parentID : {
523+ [ Op . IN ] : flowObjects . map ( ( fo ) => createBrandedValue ( fo . id ) ) ,
524+ } ,
527525 } ,
528526 distinct : [ 'parentID' , 'childID' ] ,
529527 } ) ;
530-
531- // Create a reference list of parent flows from the flow links
532- const parentFlowsRef : UniqueFlowEntity [ ] = flowLinks . map ( ( flowLink ) => ( {
533- id : flowLink . parentID ,
534- versionID : null ,
535- } ) ) ;
536-
537- // 4. Query parent flows progressively in chunks
538- const parentFlows = await this . progresiveSearch (
539- models ,
540- parentFlowsRef ,
541- 1000 ,
542- 0 ,
543- false , // Do not stop on batch size
544- [ ] ,
545- { activeStatus : true }
546- ) ;
547-
548- // 5. Retrieve flow objects using the flow object filters
549- const flowObjectsWhere =
550- buildWhereConditionsForFlowObjectFilters ( flowObjectFilters ) ;
551- const flowObjects = await this . flowObjectService . getFlowFromFlowObjects (
552- models ,
553- flowObjectsWhere
554- ) ;
555-
556- // 6. Build a Set for flowObjects for fast lookup (using a composite key of id and versionID)
557- const flowObjectsSet = new Set (
558- flowObjects . map (
559- ( flowObject ) => `${ flowObject . id } |${ flowObject . versionID } `
560- )
561- ) ;
562-
563- // 7. Filter parent flows that are present in the flowObjects list
564- const filteredParentFlows = parentFlows . filter ( ( parentFlow ) => {
565- const key = `${ parentFlow . id } |${ parentFlow . versionID } ` ;
566- return flowObjectsSet . has ( key ) ;
567- } ) ;
568-
569- // 8. Build a Set of filtered parent flow IDs for quick membership checking
570- const filteredParentFlowIds = new Set (
571- filteredParentFlows . map ( ( flow ) => flow . id )
572- ) ;
573-
574- // 9. Extract child flow IDs from flowLinks where the parent is in the filtered set
575- const childFlowsIDsSet = new Set < FlowId > ( ) ;
576- for ( const flowLink of flowLinks ) {
577- if ( filteredParentFlowIds . has ( flowLink . parentID ) ) {
578- childFlowsIDsSet . add ( flowLink . childID ) ;
579- }
580- }
581-
582- // 10. Retrieve child flows
528+ const childFlowsIDsSet = new Set < FlowId > ( flowLinks . map ( ( fl ) => fl . childID ) ) ;
529+ // 5. Retrieve child flows that are active
583530 const childFlows = await models . flow . find ( {
584531 where : {
585532 activeStatus : true ,
@@ -588,13 +535,14 @@ export class FlowService {
588535 distinct : [ 'id' , 'versionID' ] ,
589536 } ) ;
590537
591- // 11. Map child flows to UniqueFlowEntity and return the result
592- const result = childFlows . map ( ( ref ) => ( {
593- id : ref . id ,
594- versionID : ref . versionID ,
595- } ) ) ;
596-
597- return result ;
538+ // 6. Map child flows to UniqueFlowEntity and return the result
539+ return childFlows . map (
540+ ( ref ) =>
541+ ( {
542+ id : ref . id ,
543+ versionID : ref . versionID ,
544+ } ) satisfies UniqueFlowEntity
545+ ) ;
598546 }
599547
600548 /**
0 commit comments