|
|
import { DocumentFile, Innovation } from '../types'; |
|
|
|
|
|
export interface ProcessResponse { |
|
|
id: string; |
|
|
file_name: string; |
|
|
answer: string; |
|
|
classification: string; |
|
|
} |
|
|
|
|
|
export const backendService = { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
processDocument: async (file: DocumentFile, workingGroup: string, meeting: string): Promise<Innovation | null> => { |
|
|
if (!file.url) { |
|
|
console.warn(`Skipping file ${file.filename}: No URL`); |
|
|
return null; |
|
|
} |
|
|
|
|
|
try { |
|
|
const payload = { |
|
|
file_id: file.id, |
|
|
filename: file.filename, |
|
|
working_group: workingGroup, |
|
|
meeting: meeting, |
|
|
type: file.type, |
|
|
status: file.status || "Unknown", |
|
|
agenda_item: file.agendaItem || "Unknown", |
|
|
url: file.url |
|
|
}; |
|
|
|
|
|
const response = await fetch(`/process`, { |
|
|
method: 'POST', |
|
|
headers: { 'Content-Type': 'application/json' }, |
|
|
body: JSON.stringify(payload) |
|
|
}); |
|
|
|
|
|
if (!response.ok) { |
|
|
console.error(`Backend processing failed for ${file.filename}: ${response.statusText}`); |
|
|
return null; |
|
|
} |
|
|
|
|
|
const data: ProcessResponse = await response.json(); |
|
|
|
|
|
|
|
|
return { |
|
|
id: file.id, |
|
|
file_name: file.filename, |
|
|
answer: data.answer, |
|
|
classification: "UNCLASSIFIED", |
|
|
} as Innovation; |
|
|
|
|
|
} catch (error) { |
|
|
console.error(`Error processing document ${file.filename}:`, error); |
|
|
return null; |
|
|
} |
|
|
}, |
|
|
|
|
|
getPatterns: async (): Promise<any[]> => { |
|
|
try { |
|
|
const response = await fetch(`/patterns`); |
|
|
if (response.ok) { |
|
|
return await response.json(); |
|
|
} |
|
|
return []; |
|
|
} catch (error) { |
|
|
console.error("Error fetching patterns:", error); |
|
|
return []; |
|
|
} |
|
|
}, |
|
|
|
|
|
analyzeContent: async (patternId: number, fileId?: string, text?: string): Promise<{ content: string; result_id: number; methodology: string; context: string; problem: string; pattern_name: string } | null> => { |
|
|
try { |
|
|
const response = await fetch(`/analyze`, { |
|
|
method: 'POST', |
|
|
headers: { 'Content-Type': 'application/json' }, |
|
|
body: JSON.stringify({ |
|
|
pattern_id: patternId, |
|
|
file_id: fileId, |
|
|
text: text |
|
|
}) |
|
|
}); |
|
|
|
|
|
if (response.ok) { |
|
|
const data = await response.json(); |
|
|
return { content: data.content, result_id: data.id, methodology: data.methodology, context: data.context, problem: data.problem, pattern_name: data.pattern_name }; |
|
|
} |
|
|
return null; |
|
|
} catch (error) { |
|
|
console.error("Error analyzing content:", error); |
|
|
return null; |
|
|
} |
|
|
}, |
|
|
|
|
|
saveClassification: async (resultId: number, classification: string): Promise<boolean> => { |
|
|
try { |
|
|
const response = await fetch(`/classify`, { |
|
|
method: 'POST', |
|
|
headers: { 'Content-Type': 'application/json' }, |
|
|
body: JSON.stringify({ result_id: resultId, classification }) |
|
|
}); |
|
|
return response.ok; |
|
|
} catch (error) { |
|
|
console.error("Error saving classification:", error); |
|
|
return false; |
|
|
} |
|
|
}, |
|
|
|
|
|
fetchClassifiedInnovations: async (): Promise<any[]> => { |
|
|
try { |
|
|
const response = await fetch(`/results`); |
|
|
if (response.ok) { |
|
|
return await response.json(); |
|
|
} |
|
|
return []; |
|
|
} catch (error) { |
|
|
console.error("Error fetching classified innovations:", error); |
|
|
return []; |
|
|
} |
|
|
}, |
|
|
|
|
|
fetchAllFiles: async (): Promise<DocumentFile[]> => { |
|
|
try { |
|
|
const response = await fetch(`/get_all`); |
|
|
if (response.ok) { |
|
|
const rows = await response.json(); |
|
|
|
|
|
|
|
|
return rows.map((row: any) => ({ |
|
|
id: row[0], |
|
|
filename: row[7], |
|
|
size: 'Unknown', |
|
|
type: row[3], |
|
|
uploaded: true, |
|
|
status: row[4], |
|
|
agendaItem: row[5], |
|
|
url: '' |
|
|
})); |
|
|
} |
|
|
return []; |
|
|
} catch (error) { |
|
|
console.error("Error fetching all files:", error); |
|
|
return []; |
|
|
} |
|
|
} |
|
|
}; |
|
|
|