feat(admin): Task 33 — applications list, detail, status actions, homevisit notes, contract
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { db } from '$lib/server/db';
|
||||
import { applications, pets } from '$lib/server/schema';
|
||||
import { eq, and, like, count, desc } from 'drizzle-orm';
|
||||
import { hasPermission } from '$lib/roles';
|
||||
import type { Role } from '$lib/roles';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
const PER_PAGE = 20;
|
||||
|
||||
export const load: PageServerLoad = async ({ url, locals }) => {
|
||||
if (!hasPermission(locals.user!.role as Role, 'applications')) error(403, 'Access denied');
|
||||
|
||||
const q = url.searchParams.get('q')?.trim() ?? '';
|
||||
const type = url.searchParams.get('type') ?? '';
|
||||
const status = url.searchParams.get('status') ?? '';
|
||||
const page = Math.max(1, parseInt(url.searchParams.get('page') ?? '1', 10));
|
||||
const offset = (page - 1) * PER_PAGE;
|
||||
|
||||
const conditions = [];
|
||||
if (q) conditions.push(like(applications.applicantName, `%${q}%`));
|
||||
if (type) conditions.push(eq(applications.type, type as typeof applications.type.enumValues[number]));
|
||||
if (status) conditions.push(eq(applications.status, status as typeof applications.status.enumValues[number]));
|
||||
|
||||
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
|
||||
const [totalRow] = await db.select({ total: count() }).from(applications).where(where);
|
||||
const total = totalRow?.total ?? 0;
|
||||
const totalPages = Math.max(1, Math.ceil(total / PER_PAGE));
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: applications.id,
|
||||
type: applications.type,
|
||||
petId: applications.petId,
|
||||
applicantName: applications.applicantName,
|
||||
email: applications.applicantEmail,
|
||||
phone: applications.applicantPhone,
|
||||
status: applications.status,
|
||||
createdAt: applications.createdAt,
|
||||
petName: pets.name
|
||||
})
|
||||
.from(applications)
|
||||
.leftJoin(pets, eq(applications.petId, pets.id))
|
||||
.where(where)
|
||||
.orderBy(desc(applications.createdAt))
|
||||
.limit(PER_PAGE)
|
||||
.offset(offset);
|
||||
|
||||
return { applications: rows, total, page, totalPages, q, type, status };
|
||||
};
|
||||
Reference in New Issue
Block a user