Fix filtering for projects

This commit is contained in:
Justin Edmund 2025-06-10 19:47:14 -07:00
parent bbced97929
commit 5edc7eb33b
2 changed files with 88 additions and 3 deletions

View file

@ -22,6 +22,9 @@ export const GET: RequestHandler = async (event) => {
const { page, limit } = getPaginationParams(event.url)
const skip = (page - 1) * limit
// Check if admin is authenticated
const isAdmin = checkAdminAuth(event)
// Get filter parameters
const status = event.url.searchParams.get('status')
const projectType = event.url.searchParams.get('projectType')
@ -34,8 +37,8 @@ export const GET: RequestHandler = async (event) => {
if (status) {
where.status = status
} else {
// Default behavior: determine which statuses to include
} else if (!isAdmin) {
// For non-admin users: only show published projects by default
const allowedStatuses = ['published']
if (includeListOnly) {
@ -48,6 +51,7 @@ export const GET: RequestHandler = async (event) => {
where.status = { in: allowedStatuses }
}
// For admin users: show all projects (no status filter applied)
if (projectType) {
where.projectType = projectType

View file

@ -151,7 +151,7 @@ export const PUT: RequestHandler = async (event) => {
})
if (usageReferences.length > 0) {
await trackMediaUsage(usageReferences)
await updateMediaUsage(usageReferences)
}
} catch (error) {
logger.warn('Failed to update media usage tracking for project', { projectId: id, error })
@ -166,6 +166,87 @@ export const PUT: RequestHandler = async (event) => {
}
}
// PATCH /api/projects/[id] - Partially update a project
export const PATCH: RequestHandler = async (event) => {
// Check authentication
if (!checkAdminAuth(event)) {
return errorResponse('Unauthorized', 401)
}
const id = parseInt(event.params.id)
if (isNaN(id)) {
return errorResponse('Invalid project ID', 400)
}
try {
const body = await parseRequestBody<any>(event.request)
if (!body) {
return errorResponse('Invalid request body', 400)
}
// Check if project exists
const existing = await prisma.project.findUnique({
where: { id }
})
if (!existing) {
return errorResponse('Project not found', 404)
}
// Build update data object with only provided fields
const updateData: any = {}
// Handle status update specially
if (body.status !== undefined) {
updateData.status = body.status
// Set publishedAt if changing to published for the first time
if (body.status === 'published' && !existing.publishedAt) {
updateData.publishedAt = new Date()
}
// Clear publishedAt if changing to draft
else if (body.status === 'draft') {
updateData.publishedAt = null
}
}
// Add other fields if provided
if (body.title !== undefined) updateData.title = body.title
if (body.subtitle !== undefined) updateData.subtitle = body.subtitle
if (body.description !== undefined) updateData.description = body.description
if (body.year !== undefined) updateData.year = body.year
if (body.client !== undefined) updateData.client = body.client
if (body.role !== undefined) updateData.role = body.role
if (body.featuredImage !== undefined) updateData.featuredImage = body.featuredImage
if (body.logoUrl !== undefined) updateData.logoUrl = body.logoUrl
if (body.gallery !== undefined) updateData.gallery = body.gallery
if (body.externalUrl !== undefined) updateData.externalUrl = body.externalUrl
if (body.caseStudyContent !== undefined) updateData.caseStudyContent = body.caseStudyContent
if (body.backgroundColor !== undefined) updateData.backgroundColor = body.backgroundColor
if (body.highlightColor !== undefined) updateData.highlightColor = body.highlightColor
if (body.projectType !== undefined) updateData.projectType = body.projectType
if (body.displayOrder !== undefined) updateData.displayOrder = body.displayOrder
if (body.password !== undefined) updateData.password = body.password
// Handle slug update if provided
if (body.slug && body.slug !== existing.slug) {
updateData.slug = await ensureUniqueSlug(body.slug, 'project', id)
}
// Update project
const project = await prisma.project.update({
where: { id },
data: updateData
})
logger.info('Project partially updated', { id: project.id, fields: Object.keys(updateData) })
return jsonResponse(project)
} catch (error) {
logger.error('Failed to update project', error as Error)
return errorResponse('Failed to update project', 500)
}
}
// DELETE /api/projects/[id] - Delete a project
export const DELETE: RequestHandler = async (event) => {
// Check authentication