From 1d185750f3cee2682a48c8de76fafc3ce0b68e13 Mon Sep 17 00:00:00 2001 From: ibolton336 Date: Fri, 26 Jan 2024 13:00:07 -0500 Subject: [PATCH] WIP nail down status for archetypes --- .../application-assessment-status.tsx | 94 ++++++++++++++----- .../application-landscape.tsx | 1 + client/src/app/queries/archetypes.ts | 11 ++- 3 files changed, 80 insertions(+), 26 deletions(-) diff --git a/client/src/app/pages/applications/components/application-assessment-status/application-assessment-status.tsx b/client/src/app/pages/applications/components/application-assessment-status/application-assessment-status.tsx index 56b6cf738f..bf84df305f 100644 --- a/client/src/app/pages/applications/components/application-assessment-status/application-assessment-status.tsx +++ b/client/src/app/pages/applications/components/application-assessment-status/application-assessment-status.tsx @@ -4,7 +4,10 @@ import { Spinner } from "@patternfly/react-core"; import { EmptyTextMessage } from "@app/components/EmptyTextMessage"; import { Application } from "@app/api/models"; import { IconedStatus, IconedStatusPreset } from "@app/components/IconedStatus"; -import { useFetchAssessmentsByItemId } from "@app/queries/assessments"; +import { + useFetchAllAssessmentsWithArchetypes, + useFetchAssessmentsByItemId, +} from "@app/queries/assessments"; import { useFetchArchetypes } from "@app/queries/archetypes"; interface ApplicationAssessmentStatusProps { application: Application; @@ -16,19 +19,61 @@ export const ApplicationAssessmentStatus: React.FC< > = ({ application }) => { const { t } = useTranslation(); - const { archetypes, isFetching } = useFetchArchetypes(); + const { archetypes, isFetching } = useFetchArchetypes(application); + console.log("app assessment status", archetypes, application.name); - const applicationArchetypes = application.archetypes?.map((archetypeRef) => { - return archetypes?.find((archetype) => archetype.id === archetypeRef.id); - }); + const { + assessmentsWithArchetypes, + isLoading: isFetchingAllAssessmentsWithArchetypesLoading, + } = useFetchAllAssessmentsWithArchetypes(archetypes); + + const assessedArchetypesWithARequiredAssessment = assessmentsWithArchetypes + ?.filter((assessmentsWithArchetype) => { + return ( + assessmentsWithArchetype.archetype.assessed && + assessmentsWithArchetype.assessments.some( + (assessment) => assessment?.required === true + ) + ); + }) + .map((assessmentsWithArchetype) => assessmentsWithArchetype.archetype); + + const allArchetypesAssessed = + assessmentsWithArchetypes.length > 0 && + assessmentsWithArchetypes?.every((assessmentsWithArchetype) => { + const requiredAssessments = assessmentsWithArchetype.assessments.filter( + (assessment) => assessment?.required === true + ); + return ( + assessmentsWithArchetype.archetype.assessed && + assessmentsWithArchetype.assessments.length > 0 && + requiredAssessments.length > 0 && + requiredAssessments.every( + (assessment) => assessment?.status === "complete" + ) + ); + }); - const someArchetypesAssessed = applicationArchetypes?.some( - (archetype) => !!archetype?.assessments?.length ?? 0 > 0 + const hasInProgressOrNotStartedRequiredAssessments = () => { + return ( + assessmentsWithArchetypes?.some( + (assessmentsWithArchetype) => + !assessmentsWithArchetype.archetype.assessed && + assessmentsWithArchetype.assessments.some( + (assessment) => + assessment?.required === true && + (assessment.status === "empty" || + assessment.status === "started" || + assessment.status === "complete") + ) + ) ?? false + ); + }; + console.log( + "has in progress", + hasInProgressOrNotStartedRequiredAssessments(), + { assessmentsWithArchetypes } ); - const areAllArchetypesAssessed = - applicationArchetypes?.every( - (archetype) => archetype?.assessments?.length ?? 0 > 0 - ) ?? false; const { assessments, @@ -47,27 +92,28 @@ export const ApplicationAssessmentStatus: React.FC< let statusPreset: IconedStatusPreset = "NotStarted"; // Default status let tooltipCount: number = 0; - const assessedArchetypeCount = - applicationArchetypes?.filter( - (archetype) => archetype?.assessments?.length ?? 0 > 0 - ).length || 0; - const isDirectlyAssessed = application.assessed && (application.assessments?.length ?? 0) > 0; + console.log("app assessment data", { + allArchetypesAssessed, + assessmentsWithArchetypes, + application, + archetypes, + assessments, + assessedArchetypesWithARequiredAssessment, + }); + if (isDirectlyAssessed) { statusPreset = "Completed"; - } else if (areAllArchetypesAssessed) { + } else if (allArchetypesAssessed) { statusPreset = "InheritedAssessments"; - tooltipCount = assessedArchetypeCount; - } else if (someArchetypesAssessed) { + tooltipCount = assessedArchetypesWithARequiredAssessment?.length ?? 0; + } else if (hasInProgressOrNotStartedRequiredAssessments()) { statusPreset = "InProgressInheritedAssessments"; - tooltipCount = assessedArchetypeCount; + tooltipCount = assessedArchetypesWithARequiredAssessment?.length ?? 0; } else if ( - assessments?.some( - (assessment) => - assessment.status === "started" || assessment.status === "complete" - ) + assessments?.some((assessment) => assessment.status === "started") ) { statusPreset = "InProgress"; } diff --git a/client/src/app/pages/reports/components/application-landscape/application-landscape.tsx b/client/src/app/pages/reports/components/application-landscape/application-landscape.tsx index 6637c350e8..9d518d1320 100644 --- a/client/src/app/pages/reports/components/application-landscape/application-landscape.tsx +++ b/client/src/app/pages/reports/components/application-landscape/application-landscape.tsx @@ -122,6 +122,7 @@ export const ApplicationLandscape: React.FC = ({ () => aggregateRiskData(filteredAssessments, applications), [filteredAssessments, applications] ); + console.log("landscapeData", landscapeData); return ( { queryFn: getArchetypes, refetchInterval: 5000, onSuccess: (fetchedArchetypes) => { - if (forApplication && forApplication.archetypes) { + if (!forApplication) { + setFilteredArchetypes(fetchedArchetypes); + } else if ( + Array.isArray(forApplication.archetypes) && + forApplication.archetypes.length > 0 + ) { const archetypeIds = forApplication.archetypes.map( (archetype) => archetype.id ); @@ -39,8 +44,10 @@ export const useFetchArchetypes = (forApplication?: Application | null) => { ); setFilteredArchetypes(filtered); } else { - setFilteredArchetypes(fetchedArchetypes); + setFilteredArchetypes([]); } + + // Invalidate queries as needed queryClient.invalidateQueries([reviewsQueryKey]); queryClient.invalidateQueries([assessmentsQueryKey]); queryClient.invalidateQueries([assessmentsByItemIdQueryKey]);