-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path+server.ts
More file actions
37 lines (30 loc) · 1.13 KB
/
+server.ts
File metadata and controls
37 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import {
ensureExtractionTables,
processExtractionJobStep,
scheduleExtractionStep
} from '$lib/server/extraction-jobs';
export const POST: RequestHandler = async (event) => {
const db = event.platform?.env.loop_extract_emails_prod;
if (!db) return json({ ok: false, error: 'D1 binding not configured' }, { status: 500 });
let payload: unknown;
try {
payload = await event.request.json();
} catch {
return json({ ok: false, error: 'Invalid JSON body' }, { status: 400 });
}
if (typeof payload !== 'object' || payload === null) {
return json({ ok: false, error: 'Invalid payload' }, { status: 400 });
}
const { jobId, jobKey } = payload as { jobId?: unknown; jobKey?: unknown };
if (typeof jobId !== 'string' || typeof jobKey !== 'string') {
return json({ ok: false, error: 'jobId and jobKey are required' }, { status: 400 });
}
await ensureExtractionTables(db);
const { done } = await processExtractionJobStep({ db, jobId, jobKey });
if (!done) {
scheduleExtractionStep(event, { jobId, jobKey });
}
return json({ ok: true, done });
};