import type { LayoutServerLoad } from './$types'; import { db } from '$lib/server/db'; import { pets, petPhotos, petMedical, fosterAssignments } from '$lib/server/schema'; import { eq, count } from 'drizzle-orm'; import { error } from '@sveltejs/kit'; import { hasPermission, canViewPets } from '$lib/roles'; export const load: LayoutServerLoad = async ({ params, locals }) => { if (!canViewPets(locals.user!.role)) error(403, 'Access denied'); const id = parseInt(params.id, 10); if (isNaN(id)) error(404, 'Pet not found'); const [pet] = await db.select().from(pets).where(eq(pets.id, id)); if (!pet) error(404, 'Pet not found'); const [photosCount, medicalCount, fostersCount] = await Promise.all([ db.select({ total: count() }).from(petPhotos).where(eq(petPhotos.petId, id)), db.select({ total: count() }).from(petMedical).where(eq(petMedical.petId, id)), db.select({ total: count() }).from(fosterAssignments).where(eq(fosterAssignments.petId, id)) ]); return { pet, canEditPets: hasPermission(locals.user!.role, 'pets'), counts: { photos: photosCount[0]?.total ?? 0, medical: medicalCount[0]?.total ?? 0, fosters: fostersCount[0]?.total ?? 0 } }; };