Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
178 lines
4.3 KiB
TypeScript
178 lines
4.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { eq } from 'drizzle-orm';
|
|
import { db, wishlistItems, wishlists } from '@/lib/db';
|
|
import { verifyAdminToken, getGuestFromRequest } from '@/lib/auth/tokens';
|
|
import { attachClaimsToItems } from '@/lib/items-with-claims';
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params;
|
|
|
|
const isAdmin = verifyAdminToken(request);
|
|
const guest = await getGuestFromRequest(request);
|
|
if (!isAdmin && !guest) {
|
|
return NextResponse.json({ error: 'Convite necessário' }, { status: 401 });
|
|
}
|
|
|
|
// Get item
|
|
const item = await db
|
|
.select()
|
|
.from(wishlistItems)
|
|
.where(eq(wishlistItems.id, id))
|
|
.limit(1);
|
|
|
|
if (item.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Item not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Check if wishlist is public
|
|
const wishlist = await db
|
|
.select()
|
|
.from(wishlists)
|
|
.where(eq(wishlists.id, item[0].wishlistId))
|
|
.limit(1);
|
|
|
|
if (wishlist.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Wishlist not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
if (!wishlist[0].isPublic && !isAdmin) {
|
|
return NextResponse.json(
|
|
{ error: 'This item is private' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
const [withClaims] = await attachClaimsToItems(item);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
item: withClaims,
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching item:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch item' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function PATCH(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
if (!verifyAdminToken(request)) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
const body = await request.json();
|
|
const {
|
|
name,
|
|
description,
|
|
quantity,
|
|
imageUrl,
|
|
purchaseUrls,
|
|
isArchived,
|
|
} = body;
|
|
|
|
// Check if item exists
|
|
const existingItem = await db
|
|
.select()
|
|
.from(wishlistItems)
|
|
.where(eq(wishlistItems.id, id))
|
|
.limit(1);
|
|
|
|
if (existingItem.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Item not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Build update object (only include provided fields)
|
|
const updateData: Record<string, unknown> = {
|
|
updatedAt: new Date(),
|
|
};
|
|
|
|
if (name !== undefined) updateData.name = name;
|
|
if (description !== undefined) updateData.description = description;
|
|
if (quantity !== undefined) updateData.quantity = quantity;
|
|
if (imageUrl !== undefined) updateData.imageUrl = imageUrl;
|
|
if (purchaseUrls !== undefined) updateData.purchaseUrls = purchaseUrls;
|
|
if (isArchived !== undefined) updateData.isArchived = isArchived;
|
|
|
|
// Update item
|
|
const updatedItem = await db
|
|
.update(wishlistItems)
|
|
.set(updateData)
|
|
.where(eq(wishlistItems.id, id))
|
|
.returning();
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
item: updatedItem[0],
|
|
});
|
|
} catch (error) {
|
|
console.error('Error updating item:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to update item' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
if (!verifyAdminToken(request)) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
|
|
// Check if item exists
|
|
const existingItem = await db
|
|
.select()
|
|
.from(wishlistItems)
|
|
.where(eq(wishlistItems.id, id))
|
|
.limit(1);
|
|
|
|
if (existingItem.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Item not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Delete item
|
|
await db
|
|
.delete(wishlistItems)
|
|
.where(eq(wishlistItems.id, id));
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: 'Item deleted successfully',
|
|
});
|
|
} catch (error) {
|
|
console.error('Error deleting item:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to delete item' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|