Files
ims/lib/incidents/bulk-import.ts
T
admin 7cddc332f8 fix(bulk-import): type safety and unused imports
Remove unused db import, fix type casting for database query result,
correct requireAdmin() usage in API routes.
2026-08-16 15:37:45 +08:00

191 lines
5.3 KiB
TypeScript

import { createDeepSeekClient } from '@/lib/claude/client'
import { withUser } from '@/lib/db/with-user'
import { incidents, evidenceFiles, sites, zones } from '@/lib/db/schema'
import { uploadEvidenceFile } from '@/lib/storage/evidence'
import { writeAuditLog } from '@/lib/db/audit'
import { eq, sql } from 'drizzle-orm'
export type ExtractedIncidentMetadata = {
description: string
date: string
location: { site: string; zone: string }
incident_type?: 'env' | 'asset' | 'security' | 'fire'
priority?: 'low' | 'med' | 'high'
witnesses?: Array<{ name: string; contact?: string }>
}
export type FileInfo = {
path: string
name: string
mimeType: string
buffer: Buffer
}
export type CreatedIncident = {
id: string
reference_no: string
file_count: number
}
export async function analyzePdfsWithDeepSeek(
pdfTexts: string[]
): Promise<ExtractedIncidentMetadata> {
const client = createDeepSeekClient()
const prompt = `Analyze the following incident document(s) and extract structured data.
Return ONLY valid JSON, no markdown:
Document text:
${pdfTexts.join('\n---\n')}
Extract and return JSON:
{
"description": "summary of what happened (required)",
"date": "incident date in YYYY-MM-DD format (required)",
"location": {
"site": "site name (required)",
"zone": "zone/area name (required)"
},
"incident_type": "env|asset|security|fire (optional)",
"priority": "low|med|high (optional)",
"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).`
try {
const response = await client.messages.create({
model: 'deepseek-chat',
max_tokens: 500,
messages: [
{
role: 'user',
content: prompt
}
]
})
const text = response.content[0]?.type === 'text' ? response.content[0].text : ''
const cleaned = text.replace(/^```json\n?|\n?```$/g, '').trim()
const data = JSON.parse(cleaned)
return {
description: data.description || '',
date: data.date || '',
location: {
site: data.location?.site || '',
zone: data.location?.zone || ''
},
incident_type: data.incident_type,
priority: data.priority,
witnesses: data.witnesses
}
} catch (error) {
console.error('DeepSeek extraction failed:', error)
return {
description: '',
date: '',
location: { site: '', zone: '' }
}
}
}
export async function createBulkIncidents(
metadata: ExtractedIncidentMetadata[],
userId: string,
files: FileInfo[]
): Promise<CreatedIncident[]> {
const created: CreatedIncident[] = []
for (const meta of metadata) {
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))
.limit(1)
if (!siteResult.length) {
throw new Error(`Site not found: ${meta.location.site}`)
}
const site = siteResult[0]
// Get zone ID
const zoneResult = await txDb
.select()
.from(zones)
.where(eq(zones.name, meta.location.zone))
.limit(1)
if (!zoneResult.length) {
throw new Error(`Zone not found: ${meta.location.zone}`)
}
const zone = zoneResult[0]
// Generate reference_no (SITE-YYYYMM-0001)
const [year, month] = meta.date.split('-').slice(0, 2)
const prefix = `${site.code}-${year}${month}`
const existing = await txDb.execute(
sql`SELECT COUNT(*) as cnt FROM ${incidents} WHERE ${incidents.reference_no} LIKE ${prefix + '-%'}`
)
const count = (existing as { rows: Array<{ cnt: number }> }).rows?.[0]?.cnt || 0
const nextNum = parseInt(String(count)) + 1
const reference_no = `${prefix}-${String(nextNum).padStart(4, '0')}`
// Create incident (status = closed)
const [incident] = await txDb
.insert(incidents)
.values({
reference_no,
description: meta.description,
incident_date: meta.date,
site_id: site.id,
zone_id: zone.id,
incident_type: meta.incident_type || 'other',
priority: meta.priority || 'med',
status: 'closed',
created_by: userId
})
.returning({ id: incidents.id })
// Attach evidence files
for (const file of files) {
const filePath = await uploadEvidenceFile(file.buffer, file.mimeType)
await txDb.insert(evidenceFiles).values({
incident_id: incident.id,
file_name: file.name,
file_type: file.mimeType,
file_url: filePath,
file_size: file.buffer.length,
uploaded_by: userId,
uploaded_at: new Date()
})
}
// Audit log
await writeAuditLog(txDb, {
table: 'incidents',
record_id: incident.id,
action: 'bulk_import',
changed_by: userId,
old_value: null,
new_value: JSON.stringify({ reference_no, description: meta.description })
})
return { id: incident.id, reference_no, fileCount: files.length }
})
created.push({
id: result.id,
reference_no: result.reference_no,
file_count: result.fileCount
})
}
return created
}