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.
This commit is contained in:
@@ -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() {
|
||||
<td className="py-2 px-3">
|
||||
<input
|
||||
type="text"
|
||||
value={folder.extracted.location.site}
|
||||
value={folder.extracted.location?.site ?? ''}
|
||||
onChange={(e) =>
|
||||
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() {
|
||||
<td className="py-2 px-3">
|
||||
<input
|
||||
type="text"
|
||||
value={folder.extracted.location.zone}
|
||||
value={folder.extracted.location?.zone ?? ''}
|
||||
onChange={(e) =>
|
||||
updateFolder(folder.id, {
|
||||
extracted: {
|
||||
...folder.extracted,
|
||||
location: {
|
||||
...folder.extracted.location,
|
||||
site: folder.extracted.location?.site ?? '',
|
||||
zone: e.target.value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user