@@ -112,27 +112,49 @@ public function getList(): array
112112 {
113113 /* For SUPER_ADMIN_FO all the analysis are fetched to be able to update the permissions. */
114114 $ isSuperAdmin = $ this ->connectedUser ->hasRole (Entity \UserRole::SUPER_ADMIN_FO );
115+ $ linkedSupervisorsByAnrId = $ this ->getLinkedSupervisorsByAnrId ();
116+ $ assignedRiskCountsBySupervisorId = $ this ->getAssignedRiskCountsBySupervisorId (
117+ array_map (
118+ static fn (Entity \AnrSupervisor $ supervisor ): int => $ supervisor ->getId (),
119+ array_values ($ linkedSupervisorsByAnrId )
120+ )
121+ );
115122
116123 $ anrData = [];
117124 if ($ isSuperAdmin ) {
118125 foreach ($ this ->anrTable ->findAll () as $ anr ) {
119126 if (!$ anr ->isAnrSnapshot ()) {
120127 $ anrData [] = $ this ->getPreparedAnrData (
121128 $ anr ,
122- $ this ->userAnrTable ->findByAnrAndUser ($ anr , $ this ->connectedUser )
129+ $ this ->userAnrTable ->findByAnrAndUser ($ anr , $ this ->connectedUser ),
130+ false ,
131+ $ linkedSupervisorsByAnrId [$ anr ->getId ()] ?? null ,
132+ $ assignedRiskCountsBySupervisorId
123133 );
124134 }
125135 }
126136 } else {
127137 $ preparedAnrs = [];
128138 foreach ($ this ->connectedUser ->getUserAnrs () as $ userAnr ) {
129- $ preparedData = $ this ->getPreparedAnrData ($ userAnr ->getAnr (), $ userAnr );
139+ $ preparedData = $ this ->getPreparedAnrData (
140+ $ userAnr ->getAnr (),
141+ $ userAnr ,
142+ false ,
143+ $ linkedSupervisorsByAnrId [$ userAnr ->getAnr ()->getId ()] ?? null ,
144+ $ assignedRiskCountsBySupervisorId
145+ );
130146 $ preparedAnrs [$ preparedData ['id ' ]] = $ preparedData ;
131147 }
132- foreach ($ this -> anrSupervisorService -> getLinkedSupervisorsByUser ( $ this -> connectedUser ) as $ supervisor ) {
148+ foreach ($ linkedSupervisorsByAnrId as $ supervisor ) {
133149 $ anr = $ supervisor ->getAnr ();
134150 if (!$ anr ->isAnrSnapshot () && !isset ($ preparedAnrs [$ anr ->getId ()])) {
135- $ preparedAnrs [$ anr ->getId ()] = $ this ->getPreparedAnrData ($ anr , null );
151+ $ preparedAnrs [$ anr ->getId ()] = $ this ->getPreparedAnrData (
152+ $ anr ,
153+ null ,
154+ false ,
155+ $ supervisor ,
156+ $ assignedRiskCountsBySupervisorId
157+ );
136158 }
137159 }
138160 $ anrData = array_values ($ preparedAnrs );
@@ -507,12 +529,21 @@ private function duplicateInterestedParties(
507529 private function getPreparedAnrData (
508530 Entity \Anr $ anr ,
509531 ?Entity \UserAnr $ userAnr ,
510- bool $ includeSnapshotDetails = false
532+ bool $ includeSnapshotDetails = false ,
533+ ?Entity \AnrSupervisor $ linkedSupervisor = null ,
534+ array $ assignedRiskCountsBySupervisorId = []
511535 ): array {
512536 $ supervisorAccessAnr = $ anr ->isAnrSnapshot () ? $ anr ->getSnapshot ()?->getAnrReference() : $ anr ;
513- $ linkedSupervisor = $ supervisorAccessAnr === null
514- ? null
515- : $ this ->anrSupervisorService ->findLinkedSupervisor ($ supervisorAccessAnr , $ this ->connectedUser );
537+ if ($ linkedSupervisor === null && $ supervisorAccessAnr !== null ) {
538+ $ linkedSupervisor = $ this ->anrSupervisorService ->findLinkedSupervisor (
539+ $ supervisorAccessAnr ,
540+ $ this ->connectedUser
541+ );
542+ }
543+ $ assignedRiskCounts = $ this ->getAssignedRiskCountsForSupervisor (
544+ $ linkedSupervisor ,
545+ $ assignedRiskCountsBySupervisorId
546+ );
516547 $ referentialData = [];
517548 foreach ($ anr ->getReferentials () as $ referential ) {
518549 $ referentialData [] = [
@@ -535,6 +566,8 @@ private function getPreparedAnrData(
535566 'linkedSupervisorRoles ' => $ linkedSupervisor ?->getRolesArray() ?? [],
536567 'canApproveResidualRisk ' => (int )($ linkedSupervisor !== null
537568 && $ linkedSupervisor ->hasRole (Entity \AnrSupervisorRole::ROLE_RESIDUAL_RISK_APPROVER )),
569+ 'ownedRisksCount ' => $ assignedRiskCounts ['owned ' ],
570+ 'approvalRisksCount ' => $ assignedRiskCounts ['approval ' ],
538571 'isCurrentAnr ' => (int )($ this ->connectedUser ->getCurrentAnr () !== null
539572 && $ this ->connectedUser ->getCurrentAnr ()->getId () === $ anr ->getId ()),
540573 'status ' => $ anr ->getStatus (),
@@ -607,6 +640,93 @@ private function getPreparedAnrData(
607640 return $ anrData ;
608641 }
609642
643+ /**
644+ * @return array<int, Entity\AnrSupervisor>
645+ */
646+ private function getLinkedSupervisorsByAnrId (): array
647+ {
648+ $ linkedSupervisorsByAnrId = [];
649+ foreach ($ this ->anrSupervisorService ->getLinkedSupervisorsByUser ($ this ->connectedUser ) as $ supervisor ) {
650+ $ anrId = $ supervisor ->getAnr ()->getId ();
651+ if (!isset ($ linkedSupervisorsByAnrId [$ anrId ])) {
652+ $ linkedSupervisorsByAnrId [$ anrId ] = $ supervisor ;
653+ }
654+ }
655+
656+ return $ linkedSupervisorsByAnrId ;
657+ }
658+
659+ /**
660+ * @param int[] $supervisorIds
661+ *
662+ * @return array<int, array{owned:int, approval:int}>
663+ */
664+ private function getAssignedRiskCountsBySupervisorId (array $ supervisorIds ): array
665+ {
666+ $ normalizedSupervisorIds = array_values (array_unique (array_map ('intval ' , $ supervisorIds )));
667+ if ($ normalizedSupervisorIds === []) {
668+ return [];
669+ }
670+
671+ $ countsBySupervisorId = [];
672+ foreach ($ normalizedSupervisorIds as $ supervisorId ) {
673+ $ countsBySupervisorId [$ supervisorId ] = [
674+ 'owned ' => 0 ,
675+ 'approval ' => 0 ,
676+ ];
677+ }
678+
679+ $ informationRiskOwnerCounts = $ this ->instanceRiskTable
680+ ->getCountsByRiskOwnerSupervisorIds ($ normalizedSupervisorIds );
681+ foreach ($ informationRiskOwnerCounts as $ supervisorId => $ count ) {
682+ $ countsBySupervisorId [$ supervisorId ]['owned ' ] += $ count ;
683+ }
684+ $ informationApproverCounts = $ this ->instanceRiskTable
685+ ->getCountsByResidualAcceptanceApproverSupervisorIds ($ normalizedSupervisorIds );
686+ foreach ($ informationApproverCounts as $ supervisorId => $ count ) {
687+ $ countsBySupervisorId [$ supervisorId ]['approval ' ] += $ count ;
688+ }
689+ $ operationalRiskOwnerCounts = $ this ->instanceRiskOpTable
690+ ->getCountsByRiskOwnerSupervisorIds ($ normalizedSupervisorIds );
691+ foreach ($ operationalRiskOwnerCounts as $ supervisorId => $ count ) {
692+ $ countsBySupervisorId [$ supervisorId ]['owned ' ] += $ count ;
693+ }
694+ $ operationalApproverCounts = $ this ->instanceRiskOpTable
695+ ->getCountsByResidualAcceptanceApproverSupervisorIds ($ normalizedSupervisorIds );
696+ foreach ($ operationalApproverCounts as $ supervisorId => $ count ) {
697+ $ countsBySupervisorId [$ supervisorId ]['approval ' ] += $ count ;
698+ }
699+
700+ return $ countsBySupervisorId ;
701+ }
702+
703+ /**
704+ * @param array<int, array{owned:int, approval:int}> $assignedRiskCountsBySupervisorId
705+ *
706+ * @return array{owned:int, approval:int}
707+ */
708+ private function getAssignedRiskCountsForSupervisor (
709+ ?Entity \AnrSupervisor $ linkedSupervisor ,
710+ array $ assignedRiskCountsBySupervisorId
711+ ): array {
712+ if ($ linkedSupervisor === null ) {
713+ return [
714+ 'owned ' => 0 ,
715+ 'approval ' => 0 ,
716+ ];
717+ }
718+
719+ $ supervisorId = $ linkedSupervisor ->getId ();
720+ if (isset ($ assignedRiskCountsBySupervisorId [$ supervisorId ])) {
721+ return $ assignedRiskCountsBySupervisorId [$ supervisorId ];
722+ }
723+
724+ return $ this ->getAssignedRiskCountsBySupervisorId ([$ supervisorId ])[$ supervisorId ] ?? [
725+ 'owned ' => 0 ,
726+ 'approval ' => 0 ,
727+ ];
728+ }
729+
610730 /**
611731 * @param string[] $referentialUuids
612732 *
0 commit comments