Skip to content

Commit e6d6f47

Browse files
committed
refactor: simplify getAllFrameworkInstancesWithControls function and enhance task computation
- Removed caching from the getAllFrameworkInstancesWithControls function for improved clarity and maintainability. - Streamlined the mapping of framework instances to controls, ensuring better readability. - Updated task computation logic in computeFrameworkStats to deduplicate tasks, preventing double counting and improving accuracy in task statistics.
1 parent 9a9495c commit e6d6f47

File tree

2 files changed

+61
-60
lines changed

2 files changed

+61
-60
lines changed

apps/app/src/app/(app)/[orgId]/frameworks/data/getAllFrameworkInstancesWithControls.ts

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,73 @@
22

33
import type { Control, PolicyStatus, RequirementMap } from '@db';
44
import { db } from '@db';
5-
import { cache } from 'react';
65
import type { FrameworkInstanceWithControls } from '../types';
76

8-
export const getAllFrameworkInstancesWithControls = cache(
9-
async function getAllFrameworkInstancesWithControls({
10-
organizationId,
11-
}: {
12-
organizationId: string;
13-
}): Promise<FrameworkInstanceWithControls[]> {
14-
const frameworkInstancesFromDb = await db.frameworkInstance.findMany({
15-
where: {
16-
organizationId,
17-
},
18-
include: {
19-
framework: true,
20-
requirementsMapped: {
21-
include: {
22-
control: {
23-
include: {
24-
policies: {
25-
select: {
26-
id: true,
27-
name: true,
28-
status: true,
29-
},
7+
export async function getAllFrameworkInstancesWithControls({
8+
organizationId,
9+
}: {
10+
organizationId: string;
11+
}): Promise<FrameworkInstanceWithControls[]> {
12+
const frameworkInstancesFromDb = await db.frameworkInstance.findMany({
13+
where: {
14+
organizationId,
15+
},
16+
include: {
17+
framework: true,
18+
requirementsMapped: {
19+
include: {
20+
control: {
21+
include: {
22+
policies: {
23+
select: {
24+
id: true,
25+
name: true,
26+
status: true,
3027
},
31-
requirementsMapped: true,
3228
},
29+
requirementsMapped: true,
3330
},
3431
},
3532
},
3633
},
37-
});
34+
},
35+
});
3836

39-
const frameworksWithControls: FrameworkInstanceWithControls[] = frameworkInstancesFromDb.map(
40-
(fi) => {
41-
const controlsMap = new Map<
42-
string,
43-
Control & {
44-
policies: Array<{
45-
id: string;
46-
name: string;
47-
status: PolicyStatus;
48-
}>;
49-
requirementsMapped: RequirementMap[];
50-
}
51-
>();
37+
const frameworksWithControls: FrameworkInstanceWithControls[] = frameworkInstancesFromDb.map(
38+
(fi) => {
39+
const controlsMap = new Map<
40+
string,
41+
Control & {
42+
policies: Array<{
43+
id: string;
44+
name: string;
45+
status: PolicyStatus;
46+
}>;
47+
requirementsMapped: RequirementMap[];
48+
}
49+
>();
5250

53-
for (const rm of fi.requirementsMapped) {
54-
if (rm.control) {
55-
const { requirementsMapped: _, ...controlData } = rm.control;
56-
if (!controlsMap.has(rm.control.id)) {
57-
controlsMap.set(rm.control.id, {
58-
...controlData,
59-
policies: rm.control.policies || [],
60-
requirementsMapped: rm.control.requirementsMapped || [],
61-
});
62-
}
51+
for (const rm of fi.requirementsMapped) {
52+
if (rm.control) {
53+
const { requirementsMapped: _, ...controlData } = rm.control;
54+
if (!controlsMap.has(rm.control.id)) {
55+
controlsMap.set(rm.control.id, {
56+
...controlData,
57+
policies: rm.control.policies || [],
58+
requirementsMapped: rm.control.requirementsMapped || [],
59+
});
6360
}
6461
}
62+
}
6563

66-
const { requirementsMapped, ...restOfFi } = fi;
64+
const { requirementsMapped, ...restOfFi } = fi;
6765

68-
return {
69-
...restOfFi,
70-
controls: Array.from(controlsMap.values()),
71-
};
72-
},
73-
);
66+
return {
67+
...restOfFi,
68+
controls: Array.from(controlsMap.values()),
69+
};
70+
},
71+
);
7472

75-
return frameworksWithControls;
76-
},
77-
);
73+
return frameworksWithControls;
74+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ export function computeFrameworkStats(
2929

3030
const controlIds = controls.map((c) => c.id);
3131
const frameworkTasks = tasks.filter((t) => t.controls.some((c) => controlIds.includes(c.id)));
32-
const totalTasks = frameworkTasks.length;
33-
const doneTasks = frameworkTasks.filter((t) => t.status === 'done').length;
32+
// Deduplicate tasks by id to avoid double counting across multiple controls
33+
const uniqueTaskMap = new Map<string, Task & { controls: Control[] }>();
34+
for (const t of frameworkTasks) uniqueTaskMap.set(t.id, t);
35+
const uniqueTasks = Array.from(uniqueTaskMap.values());
36+
const totalTasks = uniqueTasks.length;
37+
const doneTasks = uniqueTasks.filter((t) => t.status === 'done').length;
3438
const taskRatio = totalTasks > 0 ? doneTasks / totalTasks : 1;
3539

3640
const complianceScore = Math.round(((policyRatio + taskRatio) / 2) * 100);

0 commit comments

Comments
 (0)