Skip to content

Commit 3d335bc

Browse files
authored
refactor(frameworks): consolidate score calculations into a single function (#1847)
1 parent f007cb7 commit 3d335bc

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

apps/app/src/app/(app)/[orgId]/frameworks/lib/getPeople.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { trainingVideos } from '@/lib/data/training-videos';
2-
import { getFleetInstance } from '@/lib/fleet';
32
import { db } from '@db';
43

54
export async function getPeopleScore(organizationId: string) {
@@ -50,7 +49,7 @@ export async function getPeopleScore(organizationId: string) {
5049
const requiredTrainingVideoIds = trainingVideos.map((video) => video.id);
5150

5251
// Get fleet instance for device checks
53-
const fleet = await getFleetInstance();
52+
// const fleet = await getFleetInstance();
5453

5554
// Check each employee's completion status
5655
let completedMembers = 0;
@@ -73,9 +72,9 @@ export async function getPeopleScore(organizationId: string) {
7372
);
7473

7574
// 3. Check if device is secure
76-
let hasSecureDevice = true;
75+
// let hasSecureDevice = false;
7776

78-
if (employee.fleetDmLabelId) {
77+
/* if (employee.fleetDmLabelId) {
7978
try {
8079
const deviceResponse = await fleet.get(`/labels/${employee.fleetDmLabelId}/hosts`);
8180
const device = deviceResponse.data.hosts?.[0];
@@ -91,10 +90,9 @@ export async function getPeopleScore(organizationId: string) {
9190
// If there's an error fetching device, consider it not secure
9291
hasSecureDevice = false;
9392
}
94-
}
93+
} */
9594

96-
// Employee is "done" if all three conditions are met
97-
if (hasAcceptedAllPolicies && hasCompletedAllTraining && hasSecureDevice) {
95+
if (hasAcceptedAllPolicies && hasCompletedAllTraining) {
9896
completedMembers++;
9997
}
10098
}

apps/app/src/app/(app)/[orgId]/frameworks/page.tsx

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,61 @@ export default async function DashboardPage({ params }: { params: Promise<{ orgI
6060
},
6161
});
6262

63-
const publishedPoliciesScore = await getPublishedPoliciesScore(organizationId);
64-
const doneTasksScore = await getDoneTasks(organizationId);
65-
const peopleScore = await getPeopleScore(organizationId);
63+
const scores = await getScores();
6664

6765
return (
6866
<Overview
6967
frameworksWithControls={frameworksWithControls}
7068
frameworksWithCompliance={frameworksWithCompliance}
7169
allFrameworks={allFrameworks}
7270
organizationId={organizationId}
73-
publishedPoliciesScore={publishedPoliciesScore}
74-
doneTasksScore={doneTasksScore}
75-
peopleScore={peopleScore}
71+
publishedPoliciesScore={scores.publishedPoliciesScore}
72+
doneTasksScore={scores.doneTasksScore}
73+
peopleScore={scores.peopleScore}
7674
currentMember={member}
7775
/>
7876
);
7977
}
8078

79+
const getScores = cache(async () => {
80+
const session = await auth.api.getSession({
81+
headers: await headers(),
82+
});
83+
84+
const organizationId = session?.session.activeOrganizationId;
85+
86+
if (!organizationId) {
87+
return {
88+
publishedPoliciesScore: {
89+
totalPolicies: 0,
90+
publishedPolicies: 0,
91+
draftPolicies: [],
92+
policiesInReview: [],
93+
unpublishedPolicies: [],
94+
},
95+
doneTasksScore: {
96+
totalTasks: 0,
97+
doneTasks: 0,
98+
incompleteTasks: [],
99+
},
100+
peopleScore: {
101+
totalMembers: 0,
102+
completedMembers: 0,
103+
},
104+
};
105+
}
106+
107+
const publishedPoliciesScore = await getPublishedPoliciesScore(organizationId);
108+
const doneTasksScore = await getDoneTasks(organizationId);
109+
const peopleScore = await getPeopleScore(organizationId);
110+
111+
return {
112+
publishedPoliciesScore,
113+
doneTasksScore,
114+
peopleScore,
115+
};
116+
});
117+
81118
const getControlTasks = cache(async () => {
82119
const session = await auth.api.getSession({
83120
headers: await headers(),

0 commit comments

Comments
 (0)