Make project passport lookup more defensive
cgen-2aebd3c25cef4fc5aeb101a7930da63d
This commit is contained in:
parent
e45479212e
commit
1b11b4c7b0
1 changed files with 51 additions and 30 deletions
|
|
@ -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",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue