heymenn's picture
fix backend
ced1993
import { DocumentFile, Innovation } from '../types';
export interface ProcessResponse {
id: string;
file_name: string;
answer: string;
classification: string;
}
export const backendService = {
/**
* Sends a document to the backend for processing.
* The backend handles extraction, storage (SQLite), and mock refinement.
*/
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();
// Map response to Innovation type
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();
// Map tuple to DocumentFile:
// [file_id, working_group, meeting, type, status, agenda_item, content, filename, timestamp]
return rows.map((row: any) => ({
id: row[0],
filename: row[7],
size: 'Unknown',
type: row[3],
uploaded: true, // It's from DB, so treated as processed/uploaded
status: row[4],
agendaItem: row[5],
url: '' // Not stored explicitly usually, or maybe we don't need it for display
}));
}
return [];
} catch (error) {
console.error("Error fetching all files:", error);
return [];
}
}
};