fh / src /utils /backendService_new.ts
Varun10000's picture
Upload 57 files
d8635c9 verified
Raw
History Blame Contribute Delete
5.21 kB
// Backend service for RFP Management System
const API_BASE_URL = '/api';
export interface StoredDocument {
id: string;
name: string;
category?: string;
fileType: string;
uploadDate: string;
size?: number;
contentLength?: number;
processed?: boolean;
}
export interface CurrentRFPDocument {
id: string;
name: string;
fileType: string;
uploadDate: string;
contentLength?: number;
processed?: boolean;
}
export interface DocumentWithContent {
id: string;
name: string;
category?: string;
fileType: string;
uploadDate: string;
content: string;
}
class BackendService {
/**
* Upload a previous RFP document to backend storage
*/
async uploadPreviousRFP(file: File, category: string): Promise<StoredDocument> {
const formData = new FormData();
formData.append('file', file);
formData.append('category', category);
const response = await fetch(`${API_BASE_URL}/upload-previous-rfp`, {
method: 'POST',
body: formData
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Upload failed');
}
return response.json();
}
/**
* Upload current RFP document for processing
*/
async uploadCurrentRFP(file: File): Promise<{ document: CurrentRFPDocument; content: string }> {
const formData = new FormData();
formData.append('file', file);
const response = await fetch(`${API_BASE_URL}/upload-current-rfp`, {
method: 'POST',
body: formData
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Upload failed');
}
return response.json();
}
/**
* Get all previous RFP documents
*/
async getPreviousRFPs(): Promise<StoredDocument[]> {
const response = await fetch(`${API_BASE_URL}/previous-rfps`);
if (!response.ok) {
throw new Error('Failed to fetch previous RFPs');
}
const data = await response.json();
return data.previousRfps || [];
}
/**
* Get previous RFPs by category
*/
async getPreviousRFPsByCategory(category: string): Promise<StoredDocument[]> {
const allRFPs = await this.getPreviousRFPs();
if (category === 'all') {
return allRFPs;
}
return allRFPs.filter(doc => doc.category === category);
}
/**
* Get all current RFP documents
*/
async getCurrentRFPs(): Promise<CurrentRFPDocument[]> {
const response = await fetch(`${API_BASE_URL}/current-rfps`);
if (!response.ok) {
throw new Error('Failed to fetch current RFPs');
}
const data = await response.json();
return data.currentRfps || [];
}
/**
* Get document content by ID
*/
async getDocumentContent(id: string): Promise<DocumentWithContent> {
const response = await fetch(`${API_BASE_URL}/document/${id}/content`);
if (!response.ok) {
throw new Error('Failed to fetch document content');
}
return response.json();
}
/**
* Get current RFP content by ID
*/
async getCurrentRFPContent(id: string): Promise<DocumentWithContent> {
const response = await fetch(`${API_BASE_URL}/current-rfp/${id}/content`);
if (!response.ok) {
throw new Error('Failed to fetch current RFP content');
}
return response.json();
}
/**
* Get documents for AI processing (filtered by category)
*/
async getDocumentsForAI(categoryFilter?: string): Promise<DocumentWithContent[]> {
try {
const documents = await this.getPreviousRFPsByCategory(categoryFilter || 'all');
const documentsWithContent: DocumentWithContent[] = [];
for (const doc of documents) {
try {
const contentData = await this.getDocumentContent(doc.id);
documentsWithContent.push({
id: doc.id,
name: doc.name,
category: doc.category,
fileType: doc.fileType,
uploadDate: doc.uploadDate,
content: contentData.content || ''
});
} catch (error) {
console.warn(`Failed to get content for document ${doc.id}:`, error);
// Continue with other documents
}
}
return documentsWithContent;
} catch (error) {
console.error('Failed to get documents for AI:', error);
return [];
}
}
/**
* Delete a document
*/
async deleteDocument(id: string): Promise<void> {
const response = await fetch(`${API_BASE_URL}/document/${id}`, {
method: 'DELETE'
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Delete failed');
}
}
/**
* Health check
*/
async healthCheck(): Promise<{ status: string; timestamp: string }> {
const response = await fetch(`${API_BASE_URL}/health`);
if (!response.ok) {
throw new Error('Health check failed');
}
return response.json();
}
}
export const backendService = new BackendService();