diff --git a/pages/api/collections/[identifier]/thumbnails.js b/pages/api/collections/[identifier]/thumbnails.js index 163ab8e..612d244 100644 --- a/pages/api/collections/[identifier]/thumbnails.js +++ b/pages/api/collections/[identifier]/thumbnails.js @@ -34,51 +34,46 @@ export default async function handler(req, res) { // Determine if identifier is a slug or numeric ID const isSlug = isValidSlug(identifier) || isNaN(parseInt(identifier)); - console.log('🔍 Identifier analysis:', { identifier, isSlug, isValidSlug: isValidSlug(identifier), isNaN: isNaN(parseInt(identifier)) }); - // Verify user has access to this collection let collectionResult; - if (isSlug) { - console.log('🔍 Querying by slug:', identifier); - collectionResult = await sql` - SELECT c.*, cp.role as user_role - FROM collections c - LEFT JOIN collection_permissions cp ON c.id = cp.collection_id AND cp.user_id = ${user.userId} AND cp.status = 'active' - WHERE c.slug = ${identifier} - AND ( - c.user_id = ${user.userId} OR - cp.id IS NOT NULL OR - c.is_public = true - ) - `; - } else { - const numericId = parseInt(identifier); - console.log('🔍 Querying by ID:', numericId); - collectionResult = await sql` - SELECT c.*, cp.role as user_role - FROM collections c - LEFT JOIN collection_permissions cp ON c.id = cp.collection_id AND cp.user_id = ${user.userId} AND cp.status = 'active' - WHERE c.id = ${numericId} - AND ( - c.user_id = ${user.userId} OR - cp.id IS NOT NULL OR - c.is_public = true - ) - `; + try { + if (isSlug) { + collectionResult = await sql` + SELECT c.*, cp.role as user_role + FROM collections c + LEFT JOIN collection_permissions cp ON c.id = cp.collection_id AND cp.user_id = ${user.userId} AND cp.status = 'active' + WHERE c.slug = ${identifier} + AND ( + c.user_id = ${user.userId} OR + cp.id IS NOT NULL OR + c.is_public = true + ) + `; + } else { + const numericId = parseInt(identifier); + collectionResult = await sql` + SELECT c.*, cp.role as user_role + FROM collections c + LEFT JOIN collection_permissions cp ON c.id = cp.collection_id AND cp.user_id = ${user.userId} AND cp.status = 'active' + WHERE c.id = ${numericId} + AND ( + c.user_id = ${user.userId} OR + cp.id IS NOT NULL OR + c.is_public = true + ) + `; + } + + } catch (sqlError) { + console.error('🔍 SQL Error:', sqlError); + return res.status(500).json({ error: 'Database query failed' }); } - if (collectionResult.length === 0) { + if (!collectionResult || !collectionResult.rows || collectionResult.rows.length === 0) { return res.status(404).json({ error: 'Collection not found or access denied' }); } - console.log('🔍 Collection result:', { - identifier, - isSlug, - resultLength: collectionResult.length, - collection: collectionResult[0] - }); - - const collection = collectionResult[0]; + const collection = collectionResult.rows[0]; // Add debugging and validation if (!collection || !collection.id) {