Skip to content

Commit

Permalink
WIP nail down status for archetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 committed Jan 26, 2024
1 parent 3cf032f commit 1d18575
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const ApplicationLandscape: React.FC<IApplicationLandscapeProps> = ({
() => aggregateRiskData(filteredAssessments, applications),
[filteredAssessments, applications]
);
console.log("landscapeData", landscapeData);

return (
<ConditionalRender
Expand Down
11 changes: 9 additions & 2 deletions client/src/app/queries/archetypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export const useFetchArchetypes = (forApplication?: Application | null) => {
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
);
Expand All @@ -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]);
Expand Down

0 comments on commit 1d18575

Please sign in to comment.