30 lines
1.0 KiB
TypeScript
30 lines
1.0 KiB
TypeScript
import type Database from "bun:sqlite";
|
|
|
|
import type { RuntimeMode } from "../../../shared/api";
|
|
import type { Logger } from "../../logger";
|
|
|
|
import { listConversations } from "../../db/conversations";
|
|
import { jsonResponse } from "../../helpers";
|
|
import { validateIdParam, validatePagination } from "../../middleware";
|
|
|
|
export function handleListConversations(req: Request, db: Database, mode: RuntimeMode, _logger: Logger): Response {
|
|
const url = new URL(req.url);
|
|
const projectId = url.pathname.split("/")[3];
|
|
|
|
const validated = validateIdParam(projectId ?? "", mode);
|
|
if (validated instanceof Response) return validated;
|
|
|
|
const pageParam = url.searchParams.get("page");
|
|
const pageSizeParam = url.searchParams.get("pageSize");
|
|
|
|
const pagination = validatePagination(pageParam, pageSizeParam, mode);
|
|
if (pagination instanceof Response) return pagination;
|
|
|
|
const result = listConversations(db, validated.id, {
|
|
page: pagination.page,
|
|
pageSize: pagination.pageSize,
|
|
});
|
|
|
|
return jsonResponse(result, { mode });
|
|
}
|