From 5edc7eb33b3f8a718709e0a7f34419a660a6a434 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Tue, 10 Jun 2025 19:47:14 -0700 Subject: [PATCH] Fix filtering for projects --- src/routes/api/projects/+server.ts | 8 ++- src/routes/api/projects/[id]/+server.ts | 83 ++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/src/routes/api/projects/+server.ts b/src/routes/api/projects/+server.ts index cdf037b..b2d3f9e 100644 --- a/src/routes/api/projects/+server.ts +++ b/src/routes/api/projects/+server.ts @@ -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 diff --git a/src/routes/api/projects/[id]/+server.ts b/src/routes/api/projects/[id]/+server.ts index 702c86a..a70191c 100644 --- a/src/routes/api/projects/[id]/+server.ts +++ b/src/routes/api/projects/[id]/+server.ts @@ -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(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