|
2 | 2 |
|
3 | 3 | import type { Control, PolicyStatus, RequirementMap } from '@db'; |
4 | 4 | import { db } from '@db'; |
5 | | -import { cache } from 'react'; |
6 | 5 | import type { FrameworkInstanceWithControls } from '../types'; |
7 | 6 |
|
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, |
30 | 27 | }, |
31 | | - requirementsMapped: true, |
32 | 28 | }, |
| 29 | + requirementsMapped: true, |
33 | 30 | }, |
34 | 31 | }, |
35 | 32 | }, |
36 | 33 | }, |
37 | | - }); |
| 34 | + }, |
| 35 | + }); |
38 | 36 |
|
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 | + >(); |
52 | 50 |
|
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 | + }); |
63 | 60 | } |
64 | 61 | } |
| 62 | + } |
65 | 63 |
|
66 | | - const { requirementsMapped, ...restOfFi } = fi; |
| 64 | + const { requirementsMapped, ...restOfFi } = fi; |
67 | 65 |
|
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 | + ); |
74 | 72 |
|
75 | | - return frameworksWithControls; |
76 | | - }, |
77 | | -); |
| 73 | + return frameworksWithControls; |
| 74 | +} |
0 commit comments