Initial commit
This commit is contained in:
65
apps/web/app/api/imports/[id]/status/route.ts
Normal file
65
apps/web/app/api/imports/[id]/status/route.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { getDb } from "@tline/db";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
const paramsSchema = z.object({ id: z.string().uuid() });
|
||||
|
||||
export async function GET(
|
||||
_request: Request,
|
||||
context: { params: Promise<{ id: string }> },
|
||||
): Promise<Response> {
|
||||
const rawParams = await context.params;
|
||||
const paramsParsed = paramsSchema.safeParse(rawParams);
|
||||
if (!paramsParsed.success) {
|
||||
return Response.json(
|
||||
{ error: "invalid_params", issues: paramsParsed.error.issues },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const params = paramsParsed.data;
|
||||
const db = getDb();
|
||||
|
||||
const [imp] = await db<
|
||||
{
|
||||
id: string;
|
||||
type: string;
|
||||
status: string;
|
||||
created_at: string;
|
||||
total_count: number | null;
|
||||
processed_count: number | null;
|
||||
failed_count: number | null;
|
||||
}[]
|
||||
>`
|
||||
select id, type, status, created_at, total_count, processed_count, failed_count
|
||||
from imports
|
||||
where id = ${params.id}
|
||||
limit 1
|
||||
`;
|
||||
|
||||
if (!imp) {
|
||||
return Response.json({ error: "not_found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const counts = await db<
|
||||
{
|
||||
total: number;
|
||||
ready: number;
|
||||
failed: number;
|
||||
processing: number;
|
||||
new_count: number;
|
||||
}[]
|
||||
>`
|
||||
select
|
||||
count(*)::int as total,
|
||||
count(*) filter (where status = 'ready')::int as ready,
|
||||
count(*) filter (where status = 'failed')::int as failed,
|
||||
count(*) filter (where status = 'processing')::int as processing,
|
||||
count(*) filter (where status = 'new')::int as new_count
|
||||
from assets
|
||||
where source_key like ${`staging/${imp.id}/%`}
|
||||
`;
|
||||
|
||||
return Response.json({ ...imp, asset_counts: counts[0] ?? null });
|
||||
}
|
||||
Reference in New Issue
Block a user