52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { eq } from 'drizzle-orm';
|
|
import { db, wishlists } from '@/lib/db';
|
|
import { getGuestFromRequest, verifyAdminToken } from '@/lib/auth/tokens';
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ slug: string }> }
|
|
) {
|
|
try {
|
|
const isAdmin = verifyAdminToken(request);
|
|
const guest = await getGuestFromRequest(request);
|
|
if (!isAdmin && !guest) {
|
|
return NextResponse.json({ error: 'Convite necessário' }, { status: 401 });
|
|
}
|
|
|
|
const { slug } = await params;
|
|
|
|
const wishlist = await db
|
|
.select()
|
|
.from(wishlists)
|
|
.where(eq(wishlists.slug, slug))
|
|
.limit(1);
|
|
|
|
if (wishlist.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Wishlist not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Only return public wishlists (admin can see all)
|
|
if (!wishlist[0].isPublic && !isAdmin) {
|
|
return NextResponse.json(
|
|
{ error: 'Wishlist not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
wishlist: wishlist[0],
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching wishlist by slug:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch wishlist' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|