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]