File size: 5,209 Bytes
d8635c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// 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();