fix: add valibot schemas to query() functions — fixes bad request on detail page

This commit is contained in:
Sanju Sivalingam
2026-02-17 21:16:58 +05:30
parent 423cd7af01
commit 5b73e89217
2 changed files with 29 additions and 25 deletions

View File

@@ -1,3 +1,4 @@
import * as v from 'valibot';
import { query, getRequestEvent } from '$app/server'; import { query, getRequestEvent } from '$app/server';
import { db } from '$lib/server/db'; import { db } from '$lib/server/db';
import { device, agentSession, agentStep } from '$lib/server/db/schema'; import { device, agentSession, agentStep } from '$lib/server/db/schema';
@@ -59,7 +60,7 @@ export const listDevices = query(async () => {
}); });
}); });
export const getDevice = query(async (deviceId: string) => { export const getDevice = query(v.string(), async (deviceId) => {
const { locals } = getRequestEvent(); const { locals } = getRequestEvent();
if (!locals.user) return null; if (!locals.user) return null;
@@ -88,7 +89,7 @@ export const getDevice = query(async (deviceId: string) => {
}; };
}); });
export const getDeviceStats = query(async (deviceId: string) => { export const getDeviceStats = query(v.string(), async (deviceId) => {
const { locals } = getRequestEvent(); const { locals } = getRequestEvent();
if (!locals.user) return null; if (!locals.user) return null;
@@ -116,7 +117,7 @@ export const getDeviceStats = query(async (deviceId: string) => {
} }
}); });
export const listDeviceSessions = query(async (deviceId: string) => { export const listDeviceSessions = query(v.string(), async (deviceId) => {
const { locals } = getRequestEvent(); const { locals } = getRequestEvent();
if (!locals.user) return []; if (!locals.user) return [];
@@ -130,24 +131,27 @@ export const listDeviceSessions = query(async (deviceId: string) => {
return sessions; return sessions;
}); });
export const listSessionSteps = query(async ({ deviceId, sessionId }: { deviceId: string; sessionId: string }) => { export const listSessionSteps = query(
const { locals } = getRequestEvent(); v.object({ deviceId: v.string(), sessionId: v.string() }),
if (!locals.user) return []; async ({ deviceId, sessionId }) => {
const { locals } = getRequestEvent();
if (!locals.user) return [];
// Verify session belongs to user // Verify session belongs to user
const sess = await db const sess = await db
.select() .select()
.from(agentSession) .from(agentSession)
.where(and(eq(agentSession.id, sessionId), eq(agentSession.userId, locals.user.id))) .where(and(eq(agentSession.id, sessionId), eq(agentSession.userId, locals.user.id)))
.limit(1); .limit(1);
if (sess.length === 0) return []; if (sess.length === 0) return [];
const steps = await db const steps = await db
.select() .select()
.from(agentStep) .from(agentStep)
.where(eq(agentStep.sessionId, sessionId)) .where(eq(agentStep.sessionId, sessionId))
.orderBy(agentStep.stepNumber); .orderBy(agentStep.stepNumber);
return steps; return steps;
}); }
);

View File

@@ -41,9 +41,9 @@
id: string; id: string;
goal: string; goal: string;
status: string; status: string;
stepsUsed: number; stepsUsed: number | null;
startedAt: string; startedAt: Date;
completedAt: string | null; completedAt: Date | null;
} }
interface Step { interface Step {
id: string; id: string;
@@ -144,8 +144,8 @@
return unsub; return unsub;
}); });
function formatTime(iso: string) { function formatTime(d: string | Date) {
return new Date(iso).toLocaleString(); return (d instanceof Date ? d : new Date(d)).toLocaleString();
} }
function relativeTime(iso: string) { function relativeTime(iso: string) {