27 lines
976 B
TypeScript
27 lines
976 B
TypeScript
import type { TaskStatus } from '@/lib/types';
|
|
import { requireAuthenticatedSession } from '@/lib/server/auth-session';
|
|
import { listRecentTasks } from '@/lib/server/tasks';
|
|
|
|
const ALLOWED_STATUSES: TaskStatus[] = ['queued', 'running', 'completed', 'failed'];
|
|
|
|
export async function GET(request: Request) {
|
|
const { session, response } = await requireAuthenticatedSession();
|
|
if (response) {
|
|
return response;
|
|
}
|
|
|
|
const url = new URL(request.url);
|
|
const limitValue = Number(url.searchParams.get('limit') ?? 20);
|
|
const limit = Number.isFinite(limitValue)
|
|
? Math.min(Math.max(Math.trunc(limitValue), 1), 200)
|
|
: 20;
|
|
|
|
const rawStatuses = url.searchParams.getAll('status');
|
|
const statuses = rawStatuses.filter((status): status is TaskStatus => {
|
|
return ALLOWED_STATUSES.includes(status as TaskStatus);
|
|
});
|
|
|
|
const tasks = await listRecentTasks(session.user.id, limit, statuses.length > 0 ? statuses : undefined);
|
|
return Response.json({ tasks });
|
|
}
|