From 172ca4a29e2c58e453c24116dde7c00283ef6415 Mon Sep 17 00:00:00 2001 From: weeihan Date: Thu, 20 Aug 2026 07:50:22 +0800 Subject: [PATCH] fix(bulk-import): handle missing location data from DeepSeek analysis Make location optional in ExtractedIncidentMetadata. Skip incidents without site/zone instead of crashing. Handle null location in UI form inputs with empty string defaults. Fixes batch creation errors when PDF documents don't contain clear location information for DeepSeek to extract. --- components/admin/bulk-import-form.tsx | 12 ++++++------ lib/incidents/bulk-import.ts | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/components/admin/bulk-import-form.tsx b/components/admin/bulk-import-form.tsx index 1b0fc1e..db79d01 100644 --- a/components/admin/bulk-import-form.tsx +++ b/components/admin/bulk-import-form.tsx @@ -53,7 +53,7 @@ export function BulkImportForm() { } const canImport = folders.every( - (f) => f.extracted.description && f.extracted.date && f.extracted.location.site + (f) => f.extracted.description && f.extracted.date && f.extracted.location?.site ) const handleImport = async () => { @@ -155,14 +155,14 @@ export function BulkImportForm() { updateFolder(folder.id, { extracted: { ...folder.extracted, location: { - ...folder.extracted.location, - site: e.target.value + site: e.target.value, + zone: folder.extracted.location?.zone ?? '' } } }) @@ -174,13 +174,13 @@ export function BulkImportForm() { updateFolder(folder.id, { extracted: { ...folder.extracted, location: { - ...folder.extracted.location, + site: folder.extracted.location?.site ?? '', zone: e.target.value } } diff --git a/lib/incidents/bulk-import.ts b/lib/incidents/bulk-import.ts index 04b0b5d..4f20d8a 100644 --- a/lib/incidents/bulk-import.ts +++ b/lib/incidents/bulk-import.ts @@ -8,7 +8,7 @@ import { eq, sql } from 'drizzle-orm' export type ExtractedIncidentMetadata = { description: string date: string - location: { site: string; zone: string } + location?: { site: string; zone: string } | null incident_type?: 'env' | 'asset' | 'security' | 'fire' priority?: 'low' | 'med' | 'high' witnesses?: Array<{ name: string; contact?: string }> @@ -52,7 +52,7 @@ Extract and return JSON: "witnesses": [{"name": "name", "contact": "contact info"}] (optional array) } -If a field is not found in the document, omit it (except required fields must be present).` +If a field is not found in the document, omit it. location is optional - use null if not found.` try { const response = await client.chat.completions.create({ @@ -99,16 +99,22 @@ export async function createBulkIncidents( const created: CreatedIncident[] = [] for (const meta of metadata) { + // Skip incidents without location + if (!meta.location?.site || !meta.location?.zone) { + console.warn('Skipping incident without location:', meta.description) + continue + } + const result = await withUser(userId, async (txDb) => { // Get site ID from site name const siteResult = await txDb .select() .from(sites) - .where(eq(sites.name, meta.location.site)) + .where(eq(sites.name, meta.location!.site)) .limit(1) if (!siteResult.length) { - throw new Error(`Site not found: ${meta.location.site}`) + throw new Error(`Site not found: ${meta.location!.site}`) } const site = siteResult[0] @@ -117,11 +123,11 @@ export async function createBulkIncidents( const zoneResult = await txDb .select() .from(zones) - .where(eq(zones.name, meta.location.zone)) + .where(eq(zones.name, meta.location!.zone)) .limit(1) if (!zoneResult.length) { - throw new Error(`Zone not found: ${meta.location.zone}`) + throw new Error(`Zone not found: ${meta.location!.zone}`) } const zone = zoneResult[0]