Make project passport lookup more defensive

cgen-2aebd3c25cef4fc5aeb101a7930da63d
This commit is contained in:
Builder.io 2025-11-15 04:54:43 +00:00
parent e45479212e
commit 1b11b4c7b0

View file

@ -15,33 +15,47 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
try { try {
const admin = getAdminClient(); const admin = getAdminClient();
// Look up project by slug const projectFields = `
const { data: project, error: projectError } = await admin id,
.from("aethex_projects") title,
.select( slug,
` description,
id, user_id,
title, status,
slug, image_url,
description, website,
user_id, technologies,
status, created_at,
image_url, updated_at
website, `;
technologies,
created_at,
updated_at
`,
)
.eq("slug", slug)
.single();
if (projectError) { // Try to look up project by slug first (case-insensitive)
if (projectError.code === "PGRST116") { let project: any = null;
// No rows found
return res.status(404).json({ error: "Project not found" }); try {
const result = await admin
.from("aethex_projects")
.select(projectFields)
.ilike("slug", `%${slug}%`)
.limit(1)
.single();
project = result.data;
} catch (e) {
// Continue to ID lookup
}
// If not found by slug, try by exact ID match
if (!project) {
try {
const result = await admin
.from("aethex_projects")
.select(projectFields)
.eq("id", slug)
.single();
project = result.data;
} catch (e) {
// Continue to error handling
} }
throw projectError;
} }
if (!project) { if (!project) {
@ -49,11 +63,18 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
} }
// Get project owner // Get project owner
const { data: owner } = await admin let owner: any = null;
.from("user_profiles") try {
.select("id, username, full_name, avatar_url") const result = await admin
.eq("id", project.user_id) .from("user_profiles")
.single(); .select("id, username, full_name, avatar_url")
.eq("id", project.user_id)
.single();
owner = result.data;
} catch (e) {
// Owner may not exist or may be deleted
console.warn("[Passport Project] Could not find project owner:", e);
}
return res.status(200).json({ return res.status(200).json({
type: "project", type: "project",