@@ -536,6 +536,7 @@ export class FlowService {
536536
537537 getTotalCollectionDelegators = async (
538538 socialId : string ,
539+ userId : number ,
539540 ) : Promise < Record < number , User [ ] > > => {
540541 const collections = await this . prismaService . project . findMany ( {
541542 select : { id : true } ,
@@ -546,7 +547,7 @@ export class FlowService {
546547
547548 const delegators = await Promise . all (
548549 collections . map ( ( item ) =>
549- this . getCollectionDelegators ( socialId , item . id ) ,
550+ this . getCollectionDelegators ( socialId , item . id , userId ) ,
550551 ) ,
551552 ) ;
552553
@@ -565,71 +566,51 @@ export class FlowService {
565566 getCollectionDelegators = async (
566567 socialId : string ,
567568 collectionId : number ,
569+ userId : number ,
568570 ) : Promise < User [ ] > => {
569- // Store users who have delegated to our target
570571 const delegators : User [ ] = [ ] ;
572+ const processedUserIds = new Set < number > ( [ userId ] ) ;
573+ const processedSocialIds = new Set < string > ( ) ; // Add this line
574+ processedSocialIds . add ( socialId . toLowerCase ( ) ) ; // Add this line - normalize case
571575
572- // Track processed userIds to avoid cycles in delegation chains
573- const processedUserIds = new Set < number > ( ) ;
574-
575- // Use BFS to traverse the delegation graph backward
576576 let currentBatch : string [ ] = [ socialId ] ;
577577
578- // let iteration = 1;
579-
580- // console.log('--------------------------------');
581-
582578 while ( currentBatch . length > 0 ) {
583- // Find all direct delegations to users in the current batch
584-
585- // console.log(
586- // 'iteration',
587- // iteration++,
588- // 'current batch',
589- // currentBatch,
590- // 'metadata',
591- // { collectionId },
592- // );
593-
594579 const directDelegations =
595580 await this . prismaService . collectionDelegation . findMany ( {
596581 where : {
597- target : {
598- in : currentBatch ,
599- mode : 'insensitive' ,
600- } ,
582+ target : { in : currentBatch , mode : 'insensitive' } ,
601583 collectionId,
602584 } ,
603- include : {
604- user : true ,
605- } ,
585+ include : { user : true } ,
606586 } ) ;
607587
608588 const nextBatch : string [ ] = [ ] ;
609589
610590 for ( const delegation of directDelegations ) {
611591 const delegatorId = delegation . userId ;
612-
613- // Avoid processing the same user multiple times
614592 if ( ! processedUserIds . has ( delegatorId ) ) {
615593 processedUserIds . add ( delegatorId ) ;
616594 delegators . push ( delegation . user ) ;
617-
618- // For each delegator, get their social ID to find who delegated to them
619595 }
620596 }
621597
622- // console.log('Direct delegations', directDelegations);
623598 const delegatorSocialIds = await this . convertUserIdsToSocials (
624599 directDelegations . map ( ( el ) => el . userId ) ,
625600 ) ;
626- nextBatch . push ( ...delegatorSocialIds ) ;
601+
602+ // Only add social IDs that haven't been processed yet
603+ for ( const id of delegatorSocialIds ) {
604+ const normalizedId = id . toLowerCase ( ) ; // Normalize case
605+ if ( ! processedSocialIds . has ( normalizedId ) ) {
606+ processedSocialIds . add ( normalizedId ) ;
607+ nextBatch . push ( id ) ;
608+ }
609+ }
627610
628611 currentBatch = nextBatch ;
629612 }
630613
631- // console.log('--------------------------------');
632-
633614 return delegators ;
634615 } ;
635616
0 commit comments