Files

84 lines
2.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/pages/api/auth/[...nextauth]'
export const dynamic = 'force-dynamic'
export const revalidate = 0
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
)
}
const userId = session.user.id
const listId = params.id
// Check if user has access to this list
const list = await prisma.list.findFirst({
where: {
id: listId,
OR: [
{ ownerId: userId },
{
collaborators: {
some: {
userId: userId,
},
},
},
],
},
select: {
updatedAt: true,
items: {
select: {
updatedAt: true,
},
orderBy: {
updatedAt: 'desc',
},
take: 1,
},
},
})
if (!list) {
return NextResponse.json(
{ error: 'List not found or access denied' },
{ status: 404 }
)
}
// Get the most recent update time (either list or its items)
const listUpdated = list.updatedAt.getTime()
const itemUpdated = list.items[0]?.updatedAt.getTime() || 0
const lastUpdated = Math.max(listUpdated, itemUpdated)
return NextResponse.json(
{ lastUpdated: new Date(lastUpdated).toISOString() },
{
headers: {
'Cache-Control': 'no-store, no-cache, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
},
}
)
} catch (error) {
console.error('Error fetching last updated timestamp:', error)
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
)
}
}