|
1 | | -// // app/api/profile-projects/route.ts |
2 | | -// import { NextRequest, NextResponse } from 'next/server'; |
3 | | -// import { db } from '@/lib/db'; |
4 | | -// import { projects } from '@/lib/schema'; |
5 | | -// import { eq } from 'drizzle-orm'; |
6 | | -// import { adminAuth } from '@/lib/firebase/firebaseadmin'; |
7 | | - |
8 | | -// export async function GET(req: NextRequest) { |
9 | | -// try { |
10 | | -// const authHeader = req.headers.get("authorization"); |
11 | | -// const token = authHeader?.split("Bearer ")[1]; |
12 | | - |
13 | | -// if (!token) { |
14 | | -// return NextResponse.json({ error: "Unauthorized - No token" }, { status: 401 }); |
15 | | -// } |
16 | | - |
17 | | -// const decodedToken = await adminAuth.verifyIdToken(token); |
18 | | -// const firebaseUid = decodedToken.uid; |
19 | | - |
20 | | -// const userProjects = await db |
21 | | -// .select() |
22 | | -// .from(projects) |
23 | | -// .where(eq(projects.createdByUid, firebaseUid)); |
24 | | - |
25 | | -// return NextResponse.json(userProjects); |
26 | | -// } catch (err) { |
27 | | -// console.error("Error in /api/profile-projects:", err); |
28 | | -// return NextResponse.json({ error: "Server error" }, { status: 500 }); |
29 | | -// } |
30 | | -// } |
31 | | - |
32 | | - |
33 | | -// app/api/profile-projects/route.ts |
34 | | -// import { NextRequest, NextResponse } from 'next/server'; |
35 | | -// import { db } from '@/lib/db'; |
36 | | -// import { eq } from 'drizzle-orm'; |
37 | | -// import { adminAuth } from '@/lib/firebase/firebaseadmin'; |
38 | | -// import { projects } from '@/lib/schema'; |
39 | | - |
40 | | -// export async function GET(req: NextRequest) { |
41 | | -// try { |
42 | | -// const authHeader = req.headers.get("authorization"); |
43 | | -// const token = authHeader?.split("Bearer ")[1]; |
44 | | - |
45 | | -// if (!token) { |
46 | | -// return NextResponse.json({ error: "Unauthorized - No token" }, { status: 401 }); |
47 | | -// } |
48 | | - |
49 | | -// const decodedToken = await adminAuth.verifyIdToken(token); |
50 | | -// const firebaseUid = decodedToken.uid; |
51 | | - |
52 | | -// const userProjects = await db.query.projects.findMany({ |
53 | | -// where: (fields, { eq }) => eq(fields.createdByUid, firebaseUid), |
54 | | -// with: { |
55 | | -// categories: { |
56 | | -// with: { |
57 | | -// category: true, |
58 | | -// optionValue: true, |
59 | | -// }, |
60 | | -// }, |
61 | | -// members: true, |
62 | | -// }, |
63 | | -// }); |
64 | | - |
65 | | -// // Transform the result to include flattened category structure |
66 | | -// const result = userProjects.map((project) => ({ |
67 | | -// projectId: project.projectId, |
68 | | -// projectName: project.projectName, |
69 | | -// projectDescription: project.projectDescription, |
70 | | -// projectLink: project.projectLink, |
71 | | -// customDomain: project.customDomain, |
72 | | -// createdAt: project.createdAt, |
73 | | -// members: project.members, |
74 | | -// categories: project.categories.map((cat) => ({ |
75 | | -// categoryName: cat.category.categoryName, |
76 | | -// optionName: cat.optionValue.optionName, |
77 | | -// })), |
78 | | -// })); |
79 | | - |
80 | | -// return NextResponse.json(result); |
81 | | -// } catch (err) { |
82 | | -// console.error("Error in /api/profile-projects:", err); |
83 | | -// return NextResponse.json({ error: "Server error" }, { status: 500 }); |
84 | | -// } |
85 | | -// } |
86 | | - |
87 | 1 |
|
88 | 2 | // app/api/profile-projects/route.ts |
89 | 3 | import { NextRequest, NextResponse } from 'next/server'; |
@@ -129,8 +43,18 @@ export async function GET(req: NextRequest) { |
129 | 43 | .where(eq(projectOptions.projectId, project.projectId)); |
130 | 44 |
|
131 | 45 | const categoriesArray = await Promise.all(projectOpts.map(async (opt) => { |
132 | | - const cat = await db.select({ categoryName: categories.category }).from(categories).where(eq(categories.categoryId, opt.categoryId)); |
133 | | - const val = await db.select({ optionName: categoryOptionValues.optionName }).from(categoryOptionValues).where(eq(categoryOptionValues.optionId, opt.optionId)); |
| 46 | + |
| 47 | + if (opt.categoryId === null || opt.optionId === null) return null; |
| 48 | + |
| 49 | + const cat = await db |
| 50 | + .select({ categoryName: categories.category }) |
| 51 | + .from(categories) |
| 52 | + .where(eq(categories.categoryId, opt.categoryId)); |
| 53 | + |
| 54 | + const val = await db |
| 55 | + .select({ optionName: categoryOptionValues.optionName }) |
| 56 | + .from(categoryOptionValues) |
| 57 | + .where(eq(categoryOptionValues.optionId, opt.optionId)); |
134 | 58 |
|
135 | 59 | return cat.length && val.length ? { |
136 | 60 | categoryName: cat[0].categoryName!, |
|
0 commit comments