Skip to content

Commit 024294b

Browse files
authored
Implement user frameworks API endpoint (#1449)
- Added a new API endpoint to fetch user frameworks based on their organization memberships. - Implemented authorization checks using a secret key from environment variables. - Enhanced error handling for server configuration and data fetching issues. - The endpoint returns a structured response with user emails and their associated frameworks. This addition improves the backend functionality for managing user frameworks in the application.
1 parent 8862fd3 commit 024294b

File tree

1 file changed

+73
-0
lines changed
  • apps/app/src/app/api/user-frameworks

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { db } from '@db';
2+
import { NextResponse } from 'next/server';
3+
4+
export async function GET(request: Request) {
5+
const authHeader = request.headers.get('authorization');
6+
const secretKey = process.env.SECRET_KEY;
7+
8+
if (!secretKey) {
9+
console.error('SECRET_KEY environment variable is not set');
10+
return NextResponse.json({ error: 'Server configuration error' }, { status: 500 });
11+
}
12+
13+
if (!authHeader || !authHeader.startsWith('Bearer ') || authHeader.slice(7) !== secretKey) {
14+
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
15+
}
16+
17+
try {
18+
const users = await db.user.findMany({
19+
select: {
20+
email: true,
21+
members: {
22+
select: {
23+
organization: {
24+
select: {
25+
frameworkInstances: {
26+
select: {
27+
framework: {
28+
select: {
29+
name: true,
30+
},
31+
},
32+
},
33+
},
34+
},
35+
},
36+
},
37+
},
38+
},
39+
where: {
40+
members: {
41+
some: {
42+
organization: {
43+
frameworkInstances: {
44+
some: {},
45+
},
46+
},
47+
},
48+
},
49+
},
50+
orderBy: {
51+
email: 'asc',
52+
},
53+
});
54+
55+
const userFrameworks = users.map((user) => ({
56+
email: user.email,
57+
frameworks: [
58+
...new Set(
59+
user.members.flatMap((membership) =>
60+
membership.organization.frameworkInstances.map((fi) => fi.framework.name),
61+
),
62+
),
63+
],
64+
}));
65+
66+
return NextResponse.json({
67+
userFrameworks,
68+
});
69+
} catch (error) {
70+
console.error('Error fetching user frameworks:', error);
71+
return NextResponse.json({ error: 'Failed to fetch user frameworks' }, { status: 500 });
72+
}
73+
}

0 commit comments

Comments
 (0)