From 3d335bc93afc8c79081b1359d2d815bf93fca3ff Mon Sep 17 00:00:00 2001 From: Lewis Carhart Date: Wed, 26 Nov 2025 21:07:03 +0000 Subject: [PATCH] refactor(frameworks): consolidate score calculations into a single function (#1847) --- .../(app)/[orgId]/frameworks/lib/getPeople.ts | 12 ++--- .../src/app/(app)/[orgId]/frameworks/page.tsx | 49 ++++++++++++++++--- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/apps/app/src/app/(app)/[orgId]/frameworks/lib/getPeople.ts b/apps/app/src/app/(app)/[orgId]/frameworks/lib/getPeople.ts index b653bccfb..2b35d4859 100644 --- a/apps/app/src/app/(app)/[orgId]/frameworks/lib/getPeople.ts +++ b/apps/app/src/app/(app)/[orgId]/frameworks/lib/getPeople.ts @@ -1,5 +1,4 @@ import { trainingVideos } from '@/lib/data/training-videos'; -import { getFleetInstance } from '@/lib/fleet'; import { db } from '@db'; export async function getPeopleScore(organizationId: string) { @@ -50,7 +49,7 @@ export async function getPeopleScore(organizationId: string) { const requiredTrainingVideoIds = trainingVideos.map((video) => video.id); // Get fleet instance for device checks - const fleet = await getFleetInstance(); + // const fleet = await getFleetInstance(); // Check each employee's completion status let completedMembers = 0; @@ -73,9 +72,9 @@ export async function getPeopleScore(organizationId: string) { ); // 3. Check if device is secure - let hasSecureDevice = true; + // let hasSecureDevice = false; - if (employee.fleetDmLabelId) { + /* if (employee.fleetDmLabelId) { try { const deviceResponse = await fleet.get(`/labels/${employee.fleetDmLabelId}/hosts`); const device = deviceResponse.data.hosts?.[0]; @@ -91,10 +90,9 @@ export async function getPeopleScore(organizationId: string) { // If there's an error fetching device, consider it not secure hasSecureDevice = false; } - } + } */ - // Employee is "done" if all three conditions are met - if (hasAcceptedAllPolicies && hasCompletedAllTraining && hasSecureDevice) { + if (hasAcceptedAllPolicies && hasCompletedAllTraining) { completedMembers++; } } diff --git a/apps/app/src/app/(app)/[orgId]/frameworks/page.tsx b/apps/app/src/app/(app)/[orgId]/frameworks/page.tsx index 02f00747e..b1c25eeb1 100644 --- a/apps/app/src/app/(app)/[orgId]/frameworks/page.tsx +++ b/apps/app/src/app/(app)/[orgId]/frameworks/page.tsx @@ -60,9 +60,7 @@ export default async function DashboardPage({ params }: { params: Promise<{ orgI }, }); - const publishedPoliciesScore = await getPublishedPoliciesScore(organizationId); - const doneTasksScore = await getDoneTasks(organizationId); - const peopleScore = await getPeopleScore(organizationId); + const scores = await getScores(); return ( ); } +const getScores = cache(async () => { + const session = await auth.api.getSession({ + headers: await headers(), + }); + + const organizationId = session?.session.activeOrganizationId; + + if (!organizationId) { + return { + publishedPoliciesScore: { + totalPolicies: 0, + publishedPolicies: 0, + draftPolicies: [], + policiesInReview: [], + unpublishedPolicies: [], + }, + doneTasksScore: { + totalTasks: 0, + doneTasks: 0, + incompleteTasks: [], + }, + peopleScore: { + totalMembers: 0, + completedMembers: 0, + }, + }; + } + + const publishedPoliciesScore = await getPublishedPoliciesScore(organizationId); + const doneTasksScore = await getDoneTasks(organizationId); + const peopleScore = await getPeopleScore(organizationId); + + return { + publishedPoliciesScore, + doneTasksScore, + peopleScore, + }; +}); + const getControlTasks = cache(async () => { const session = await auth.api.getSession({ headers: await headers(),