File size: 7,492 Bytes
a62f8b4
4e003e4
 
 
 
 
 
 
 
 
a62f8b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8fa21c
 
 
 
a62f8b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a024174
 
 
 
 
 
 
 
 
a62f8b4
 
 
 
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// API configuration
const API_BASE_URL = (() => {
    if (location.hostname === 'localhost' || location.hostname === '127.0.0.1') {
        return 'http://localhost:4000/api';
    }
    if (location.hostname.includes('.hf.space')) {
        return 'https://tataaicoltd-api.hf.space/api';
    }
    return '/api';
})();

class ApiClient {
    constructor(baseUrl = API_BASE_URL) {
        this.baseUrl = baseUrl;
    }

    async request(endpoint, options = {}) {
        const url = `${this.baseUrl}${endpoint}`;
        const apiKey = sessionStorage.getItem('admin_api_key') || '';
        const config = {
            headers: {
                'Content-Type': 'application/json',
                'X-API-Key': apiKey,
                ...options.headers,
            },
            ...options,
        };

        try {
            const response = await fetch(url, config);
            const data = await response.json();

            if (!response.ok) {
                const err = new Error(data.detail || data.error || `HTTP ${response.status}`);
                err.status = response.status;
                err.data = data;   // full response body available as err.data
                throw err;
            }

            return data;
        } catch (error) {
            console.error('API Error:', error);
            throw error;
        }
    }

    // Health check
    async healthCheck() {
        return this.request('/health');
    }

    // ==================== CATEGORIES ====================
    
    async getCategories(search = null) {
        let endpoint = '/categories';
        if (search) {
            endpoint += `?search=${encodeURIComponent(search)}`;
        }
        return this.request(endpoint);
    }

    async getCategoryById(categoryId) {
        return this.request(`/categories/${categoryId}`);
    }

    async createCategory(category) {
        return this.request('/categories', {
            method: 'POST',
            body: JSON.stringify(category),
        });
    }

    async updateCategory(categoryId, category) {
        return this.request(`/categories/${categoryId}`, {
            method: 'PUT',
            body: JSON.stringify(category),
        });
    }

    async deleteCategory(categoryId) {
        return this.request(`/categories/${categoryId}`, {
            method: 'DELETE',
        });
    }

    // ==================== INTENTS ====================

    async getIntents(filters = {}) {
        let endpoint = '/intents';
        const params = new URLSearchParams();

        if (filters.search) {
            params.append('search', filters.search);
        }
        if (filters.categoryId) {
            params.append('category_id', filters.categoryId);
        }
        if (filters.priority) {
            params.append('priority', filters.priority);
        }

        const queryString = params.toString();
        if (queryString) {
            endpoint += `?${queryString}`;
        }

        return this.request(endpoint);
    }

    async getIntentById(intentId) {
        return this.request(`/intents/${intentId}`);
    }

    async getIntentsByCategory(categoryId) {
        return this.request(`/categories/${categoryId}/intents`);
    }

    async createIntent(intent) {
        return this.request('/intents', {
            method: 'POST',
            body: JSON.stringify(intent),
        });
    }

    async updateIntent(intentId, intent) {
        return this.request(`/intents/${intentId}`, {
            method: 'PUT',
            body: JSON.stringify(intent),
        });
    }

    async deleteIntent(intentId) {
        return this.request(`/intents/${intentId}`, {
            method: 'DELETE',
        });
    }

    // ==================== MAPS ====================

    async getCities() {
        return this.request('/cities');
    }

    async getCityById(cityId) {
        return this.request(`/cities/${cityId}`);
    }

    async createCity(name) {
        return this.request('/cities', {
            method: 'POST',
            body: JSON.stringify({ name }),
        });
    }

    async updateCity(cityId, data) {
        return this.request(`/cities/${cityId}`, {
            method: 'PUT',
            body: JSON.stringify(data),
        });
    }

    async deleteCity(cityId) {
        return this.request(`/cities/${cityId}`, { method: 'DELETE' });
    }

    async getDistricts(cityId = null) {
        const params = cityId ? `?city_id=${cityId}` : '';
        return this.request(`/districts${params}`);
    }

    async getDistrictById(districtId) {
        return this.request(`/districts/${districtId}`);
    }

    async createDistrict(name, cityId, lname = null) {
        const body = { name, city: parseInt(cityId) };
        if (lname) body.lname = lname;
        return this.request('/districts', {
            method: 'POST',
            body: JSON.stringify(body),
        });
    }

    async updateDistrict(districtId, data) {
        return this.request(`/districts/${districtId}`, {
            method: 'PUT',
            body: JSON.stringify(data),
        });
    }

    async deleteDistrict(districtId) {
        return this.request(`/districts/${districtId}`, { method: 'DELETE' });
    }

    async getStreets(districtId = null, cityId = null) {
        const params = new URLSearchParams();
        if (districtId) params.append('district_id', districtId);
        else if (cityId) params.append('city_id', cityId);
        const qs = params.toString();
        return this.request(`/streets${qs ? '?' + qs : ''}`);
    }

    async getAllStreets() {
        return this.request('/streets');
    }

    async getStreetById(streetId) {
        return this.request(`/streets/${streetId}`);
    }

    async createStreet(name, districtId, cityId, type = null) {
        const body = { name, district_id: parseInt(districtId), city_id: parseInt(cityId) };
        if (type) body.type = type;
        return this.request('/streets', {
            method: 'POST',
            body: JSON.stringify(body),
        });
    }

    async updateStreet(streetId, data) {
        return this.request(`/streets/${streetId}`, {
            method: 'PUT',
            body: JSON.stringify(data),
        });
    }

    async deleteStreet(streetId) {
        return this.request(`/streets/${streetId}`, { method: 'DELETE' });
    }

    // ==================== POSITIONS ====================

    async getPositions(streetId = null) {
        const params = streetId ? `?street_id=${streetId}` : '';
        return this.request(`/positions${params}`);
    }

    async getPositionById(positionId) {
        return this.request(`/positions/${positionId}`);
    }

    async createPosition(data) {
        return this.request('/positions', {
            method: 'POST',
            body: JSON.stringify(data),
        });
    }

    async updatePosition(positionId, data) {
        return this.request(`/positions/${positionId}`, {
            method: 'PUT',
            body: JSON.stringify(data),
        });
    }

    async deletePosition(positionId) {
        return this.request(`/positions/${positionId}`, { method: 'DELETE' });
    }

    // ==================== PATHFINDER ====================

    async pathfind(startId, endId) {
        return this.request('/pathfind', {
            method: 'POST',
            body: JSON.stringify({ start_id: startId, end_id: endId }),
        });
    }
}

// Global singleton used by app.js
const api = new ApiClient();