Skip to content

Commit 397397b

Browse files
committed
Add report fetching and user accessed reports functionality
- Implemented GET endpoint to fetch reports by ID from Firestore, returning cached report data or appropriate error messages. - Added POST endpoint to log accessed reports for users, storing relevant data in the user's accessed_reports array. - Updated the dashboard to save accessed reports when streaming completes. - Enhanced the reports page to display user's accessed reports with loading states and error handling. - Refactored report detail page to fetch report data and handle loading/error states, improving user experience.
1 parent 0aef30d commit 397397b

File tree

5 files changed

+845
-868
lines changed

5 files changed

+845
-868
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { adminDb } from "@/lib/firebase-admin";
2+
3+
export async function GET(
4+
request: Request,
5+
{ params }: { params: Promise<{ id: string }> }
6+
) {
7+
try {
8+
const { id } = await params;
9+
10+
if (!id) {
11+
return Response.json({ error: "Report ID is required" }, { status: 400 });
12+
}
13+
14+
console.log("Fetching report for ID:", id);
15+
16+
// Fetch report from cache collection
17+
const cacheDoc = await adminDb
18+
.collection("cache")
19+
.doc(id.toLowerCase())
20+
.get();
21+
22+
console.log("Cache doc exists:", cacheDoc.exists);
23+
24+
if (!cacheDoc.exists) {
25+
return Response.json(
26+
{ error: "Report not found", id: id.toLowerCase() },
27+
{ status: 404 }
28+
);
29+
}
30+
31+
const data = cacheDoc.data();
32+
33+
return Response.json({
34+
cached_at: data?.cached_at || null,
35+
query: data?.query || id,
36+
report: data?.report || {},
37+
});
38+
} catch (error) {
39+
console.error("Error fetching report:", error);
40+
return Response.json(
41+
{
42+
error: "Internal server error",
43+
details: error instanceof Error ? error.message : "Unknown error",
44+
},
45+
{ status: 500 }
46+
);
47+
}
48+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { adminDb } from "@/lib/firebase-admin";
2+
import { FieldValue } from "firebase-admin/firestore";
3+
4+
export async function POST(request: Request) {
5+
try {
6+
const { userId, entityName } = await request.json();
7+
8+
if (!userId || !entityName) {
9+
return Response.json(
10+
{ error: "userId and entityName are required" },
11+
{ status: 400 }
12+
);
13+
}
14+
15+
// Get the cached report from the cache collection
16+
const cacheDoc = await adminDb.collection("cache").doc(entityName.toLowerCase()).get();
17+
18+
if (!cacheDoc.exists) {
19+
return Response.json(
20+
{ error: "Report not found in cache" },
21+
{ status: 404 }
22+
);
23+
}
24+
25+
const cacheData = cacheDoc.data();
26+
27+
// Add to user's accessed_reports array
28+
const userRef = adminDb.collection("users").doc(userId);
29+
30+
await userRef.set(
31+
{
32+
accessed_reports: FieldValue.arrayUnion({
33+
entity_name: entityName.toLowerCase(),
34+
accessed_at: new Date().toISOString(),
35+
trust_score: cacheData?.report?.trust_score?.score || null,
36+
product_name: cacheData?.report?.product_name || entityName,
37+
vendor: cacheData?.report?.vendor || null,
38+
}),
39+
},
40+
{ merge: true }
41+
);
42+
43+
return Response.json({
44+
success: true,
45+
message: "Accessed report saved successfully",
46+
});
47+
} catch (error) {
48+
console.error("Error saving accessed report:", error);
49+
return Response.json(
50+
{
51+
error: "Internal server error",
52+
details: error instanceof Error ? error.message : "Unknown error",
53+
},
54+
{ status: 500 }
55+
);
56+
}
57+
}
58+
59+
// GET endpoint to fetch user's accessed reports
60+
export async function GET(request: Request) {
61+
try {
62+
const { searchParams } = new URL(request.url);
63+
const userId = searchParams.get("userId");
64+
65+
if (!userId) {
66+
return Response.json({ error: "userId is required" }, { status: 400 });
67+
}
68+
69+
const userDoc = await adminDb.collection("users").doc(userId).get();
70+
71+
if (!userDoc.exists) {
72+
return Response.json({
73+
accessed_reports: [],
74+
});
75+
}
76+
77+
const userData = userDoc.data();
78+
const accessedReports = userData?.accessed_reports || [];
79+
80+
// Fetch full report details from cache for each accessed report
81+
const reportsWithDetails = await Promise.all(
82+
accessedReports.map(async (access: any) => {
83+
const cacheDoc = await adminDb
84+
.collection("cache")
85+
.doc(access.entity_name)
86+
.get();
87+
88+
if (cacheDoc.exists) {
89+
return {
90+
...access,
91+
report: cacheDoc.data()?.report,
92+
cached_at: cacheDoc.data()?.cached_at,
93+
};
94+
}
95+
96+
return access;
97+
})
98+
);
99+
100+
return Response.json({
101+
accessed_reports: reportsWithDetails,
102+
});
103+
} catch (error) {
104+
console.error("Error fetching accessed reports:", error);
105+
return Response.json(
106+
{
107+
error: "Internal server error",
108+
details: error instanceof Error ? error.message : "Unknown error",
109+
},
110+
{ status: 500 }
111+
);
112+
}
113+
}

junction-app/app/dashboard/page.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,25 @@ export default function DashboardPage() {
6161
}
6262
};
6363

64-
const handleStreamComplete = () => {
64+
const handleStreamComplete = async () => {
65+
// Save accessed report to user's collection
66+
if (user && streamingEntityName) {
67+
try {
68+
await fetch("/api/user/accessed-reports", {
69+
method: "POST",
70+
headers: {
71+
"Content-Type": "application/json",
72+
},
73+
body: JSON.stringify({
74+
userId: user.uid,
75+
entityName: streamingEntityName,
76+
}),
77+
});
78+
} catch (error) {
79+
console.error("Error saving accessed report:", error);
80+
}
81+
}
82+
6583
// Redirect to reports page after streaming completes
6684
router.push("/reports");
6785
};

0 commit comments

Comments
 (0)