1- import { notFound } from 'next/navigation' ;
2- import { ControlDetailsClientPage } from './ControlDetailsClientPage' ;
3- import type { FrameworkEditorControlTemplate } from '@prisma/client' ;
4- import { db } from '@comp/db' ;
1+ import { notFound , redirect } from "next/navigation" ;
2+ import { ControlDetailsClientPage } from "./ControlDetailsClientPage" ;
3+ import type { FrameworkEditorControlTemplate } from "@prisma/client" ;
4+ import { db } from "@comp/db" ;
5+ import { isAuthorized } from "@/app/lib/utils" ;
56
67// Define a more specific type for the control details, including relations
78// Prisma will generate a similar type, but defining it explicitly can be helpful for props
89export type ControlDetailsWithRelations = FrameworkEditorControlTemplate & {
9- policyTemplates : { id : string ; name : string } [ ] ;
10- requirements : (
11- {
12- id : string ;
13- name : string ;
14- framework : { id : string ; name : string | null } | null ; // Added id to framework
15- }
16- ) [ ] ;
17- taskTemplates : { id : string ; name : string } [ ] ;
10+ policyTemplates : { id : string ; name : string } [ ] ;
11+ requirements : {
12+ id : string ;
13+ name : string ;
14+ framework : { id : string ; name : string | null } | null ; // Added id to framework
15+ } [ ] ;
16+ taskTemplates : { id : string ; name : string } [ ] ;
1817} ;
1918
20- async function getControlDetails ( id : string ) : Promise < ControlDetailsWithRelations | null > {
21- const control = await db . frameworkEditorControlTemplate . findUnique ( {
22- where : { id } ,
23- include : {
24- policyTemplates : {
25- select : {
26- id : true ,
27- name : true ,
28- } ,
29- orderBy : { name : 'asc' } // Optional: sort related items
30- } ,
31- requirements : {
32- select : {
33- id : true ,
34- name : true ,
35- framework : {
36- select : {
37- id : true , // Ensure framework id is selected
38- name : true ,
39- }
40- }
41- } ,
42- orderBy : { name : 'asc' } // Optional: sort related items
43- } ,
44- taskTemplates : {
45- select : {
46- id : true ,
47- name : true ,
48- } ,
49- orderBy : { name : 'asc' } // Optional: sort related items
50- } ,
51- }
52- } ) ;
53- return control ;
19+ async function getControlDetails (
20+ id : string ,
21+ ) : Promise < ControlDetailsWithRelations | null > {
22+ const control = await db . frameworkEditorControlTemplate . findUnique ( {
23+ where : { id } ,
24+ include : {
25+ policyTemplates : {
26+ select : {
27+ id : true ,
28+ name : true ,
29+ } ,
30+ orderBy : { name : "asc" } , // Optional: sort related items
31+ } ,
32+ requirements : {
33+ select : {
34+ id : true ,
35+ name : true ,
36+ framework : {
37+ select : {
38+ id : true , // Ensure framework id is selected
39+ name : true ,
40+ } ,
41+ } ,
42+ } ,
43+ orderBy : { name : "asc" } , // Optional: sort related items
44+ } ,
45+ taskTemplates : {
46+ select : {
47+ id : true ,
48+ name : true ,
49+ } ,
50+ orderBy : { name : "asc" } , // Optional: sort related items
51+ } ,
52+ } ,
53+ } ) ;
54+ return control ;
5455}
5556
5657interface ControlDetailsPageProps {
57- params : {
58- controlId : string ;
59- } ;
58+ params : {
59+ controlId : string ;
60+ } ;
6061}
6162
62- export default async function ControlDetailsPage ( { params } : ControlDetailsPageProps ) {
63- const controlDetails = await getControlDetails ( params . controlId ) ;
63+ export default async function ControlDetailsPage ( {
64+ params,
65+ } : ControlDetailsPageProps ) {
66+ const isAllowed = await isAuthorized ( ) ;
6467
65- if ( ! controlDetails ) {
66- notFound ( ) ;
67- }
68+ if ( ! isAllowed ) {
69+ redirect ( "/auth" ) ;
70+ }
6871
69- // Ensure the props passed to client component match its expectation or update client component props
70- return < ControlDetailsClientPage controlDetails = { controlDetails } /> ;
71- }
72+ const controlDetails = await getControlDetails ( params . controlId ) ;
73+
74+ if ( ! controlDetails ) {
75+ notFound ( ) ;
76+ }
77+
78+ // Ensure the props passed to client component match its expectation or update client component props
79+ return < ControlDetailsClientPage controlDetails = { controlDetails } /> ;
80+ }
0 commit comments