File size: 11,804 Bytes
f3e5f4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e97cd69
f3e5f4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e360c0
 
 
f3e5f4f
 
 
0e360c0
 
 
f3e5f4f
 
 
0e360c0
 
 
f3e5f4f
 
 
 
 
 
0e360c0
f3e5f4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e97cd69
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { Component, Inject, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule, FormArray } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
import { MatTabsModule } from '@angular/material/tabs';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { MatDividerModule } from '@angular/material/divider';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatChipsModule } from '@angular/material/chips';
import { ApiService } from '../../services/api.service';

@Component({
  selector: 'app-api-edit-dialog',
  standalone: true,
  imports: [
    CommonModule,
    ReactiveFormsModule,
    MatDialogModule,
    MatTabsModule,
    MatFormFieldModule,
    MatInputModule,
    MatSelectModule,
    MatCheckboxModule,
    MatButtonModule,
    MatIconModule,
    MatSnackBarModule,
    MatDividerModule,
    MatExpansionModule,
    MatChipsModule
  ],
  templateUrl: './api-edit-dialog.component.html',
  styleUrls: ['./api-edit-dialog.component.scss']
})
export default class ApiEditDialogComponent implements OnInit {
  form!: FormGroup;
  saving = false;
  testing = false;
  testResult: any = null;

  httpMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
  retryStrategies = ['static', 'exponential'];

  // Template variable helpers
  templateVariables = [
    { key: 'variables.origin', desc: 'Origin city from session' },
    { key: 'variables.destination', desc: 'Destination city from session' },
    { key: 'variables.flight_date', desc: 'Flight date from session' },
    { key: 'auth_tokens.api_name.token', desc: 'Auth token for this API' },
    { key: 'config.work_mode', desc: 'Current work mode' }
  ];

  constructor(
    private fb: FormBuilder,
    private apiService: ApiService,
    private snackBar: MatSnackBar,
    public dialogRef: MatDialogRef<ApiEditDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {}

  ngOnInit() {
    this.initializeForm();

    if (this.data.mode === 'edit' && this.data.api) {
      this.populateForm(this.data.api);
    } else if (this.data.mode === 'duplicate' && this.data.api) {
      const duplicateData = { ...this.data.api };
      duplicateData.name = duplicateData.name + '_copy';
      delete duplicateData.last_update_date;
      this.populateForm(duplicateData);
    }
  }

  initializeForm() {
    this.form = this.fb.group({
      // General Tab
      name: ['', [Validators.required, Validators.pattern(/^[a-zA-Z0-9_]+$/)]],
      url: ['', [Validators.required, Validators.pattern(/^https?:\/\/.+/)]],
      method: ['POST', Validators.required],
      body_template: ['{}'],
      timeout_seconds: [10, [Validators.required, Validators.min(1), Validators.max(300)]],
      response_prompt: [''],

      // Headers Tab
      headers: this.fb.array([]),

      // Retry Settings
      retry: this.fb.group({
        retry_count: [3, [Validators.required, Validators.min(0), Validators.max(10)]],
        backoff_seconds: [2, [Validators.required, Validators.min(1), Validators.max(60)]],
        strategy: ['static', Validators.required]
      }),

      // Auth Tab
      auth: this.fb.group({
        enabled: [false],
        token_endpoint: [''],
        response_token_path: ['token'],
        token_request_body: ['{}'],
        token_refresh_endpoint: [''],
        token_refresh_body: ['{}']
      }),

      // Proxy (optional)
      proxy: [''],

      // For race condition handling
      last_update_date: ['']
    });

    // Watch for auth enabled changes
    this.form.get('auth.enabled')?.valueChanges.subscribe(enabled => {
      const authGroup = this.form.get('auth');
      if (enabled) {
        authGroup?.get('token_endpoint')?.setValidators([Validators.required]);
        authGroup?.get('response_token_path')?.setValidators([Validators.required]);
      } else {
        authGroup?.get('token_endpoint')?.clearValidators();
        authGroup?.get('response_token_path')?.clearValidators();
      }
      authGroup?.get('token_endpoint')?.updateValueAndValidity();
      authGroup?.get('response_token_path')?.updateValueAndValidity();
    });
  }

  populateForm(api: any) {
    // Convert headers object to FormArray
    const headersArray = this.form.get('headers') as FormArray;
    headersArray.clear();

    if (api.headers && typeof api.headers === 'object') {
      Object.entries(api.headers).forEach(([key, value]) => {
        headersArray.push(this.createHeaderFormGroup(key, value as string));
      });
    }

    // Convert body_template to JSON string if it's an object
    if (api.body_template && typeof api.body_template === 'object') {
      api.body_template = JSON.stringify(api.body_template, null, 2);
    }

    // Convert auth bodies to JSON strings
    if (api.auth) {
      if (api.auth.token_request_body && typeof api.auth.token_request_body === 'object') {
        api.auth.token_request_body = JSON.stringify(api.auth.token_request_body, null, 2);
      }
      if (api.auth.token_refresh_body && typeof api.auth.token_refresh_body === 'object') {
        api.auth.token_refresh_body = JSON.stringify(api.auth.token_refresh_body, null, 2);
      }
    }

    // Patch form values
    this.form.patchValue(api);

    // Disable name field if editing
    if (this.data.mode === 'edit') {
      this.form.get('name')?.disable();
    }
  }

  get headers() {
    return this.form.get('headers') as FormArray;
  }

  createHeaderFormGroup(key = '', value = ''): FormGroup {
    return this.fb.group({
      key: [key, Validators.required],
      value: [value, Validators.required]
    });
  }

  addHeader() {
    this.headers.push(this.createHeaderFormGroup());
  }

  removeHeader(index: number) {
    this.headers.removeAt(index);
  }

  insertTemplateVariable(field: string, variable: string) {
    const control = field.includes('.') 
      ? this.form.get(field)
      : this.form.get(field);
    
    if (control) {
      const currentValue = control.value || '';
      const newValue = currentValue + `{{${variable}}}`;
      control.setValue(newValue);
    }
  }

  validateJSON(field: string): boolean {
    const control = this.form.get(field);
    if (!control || !control.value) return true;

    try {
      JSON.parse(control.value);
      return true;
    } catch {
      return false;
    }
  }

  async testAPI() {
    // Validate form first
    const generalValid = this.form.get('url')?.valid && this.form.get('method')?.valid;
    if (!generalValid) {
      this.snackBar.open('Please fill in required fields first', 'Close', { duration: 3000 });
      return;
    }

    this.testing = true;
    this.testResult = null;

    try {
      const testData = this.prepareAPIData();
      
      // For test, we'll use a sample request
      const result = await this.apiService.testAPI(testData).toPromise();
      this.testResult = result;

      if (result.success) {
        this.snackBar.open('API test successful!', 'Close', { duration: 3000 });
      } else {
        this.snackBar.open(`API test failed: ${result.error}`, 'Close', { 
          duration: 5000,
          panelClass: 'error-snackbar' 
        });
      }
    } catch (error: any) {
      this.testResult = {
        success: false,
        error: error.message || 'Test failed'
      };
      this.snackBar.open('API test failed', 'Close', { 
        duration: 3000,
        panelClass: 'error-snackbar' 
      });
    } finally {
      this.testing = false;
    }
  }

  prepareAPIData(): any {
    const formValue = this.form.getRawValue();

    // Convert headers array back to object
    const headers: any = {};
    formValue.headers.forEach((h: any) => {
      if (h.key && h.value) {
        headers[h.key] = h.value;
      }
    });

    // Parse JSON fields
    let body_template = {};
    let auth_token_request_body = {};
    let auth_token_refresh_body = {};

    try {
      body_template = formValue.body_template ? JSON.parse(formValue.body_template) : {};
    } catch (e) {
      console.error('Invalid body_template JSON:', e);
    }

    try {
      auth_token_request_body = formValue.auth.token_request_body ? JSON.parse(formValue.auth.token_request_body) : {};
    } catch (e) {
      console.error('Invalid auth token_request_body JSON:', e);
    }

    try {
      auth_token_refresh_body = formValue.auth.token_refresh_body ? JSON.parse(formValue.auth.token_refresh_body) : {};
    } catch (e) {
      console.error('Invalid auth token_refresh_body JSON:', e);
    }

    // Prepare final data
    const apiData: any = {
      name: formValue.name,
      url: formValue.url,
      method: formValue.method,
      headers,  // Now it's a dictionary object
      body_template,
      timeout_seconds: formValue.timeout_seconds,
      retry: formValue.retry,
      response_prompt: formValue.response_prompt
    };

    // Add proxy if specified
    if (formValue.proxy) {
      apiData.proxy = formValue.proxy;
    }

    // Add auth if enabled
    if (formValue.auth.enabled) {
      apiData.auth = {
        enabled: true,
        token_endpoint: formValue.auth.token_endpoint,
        response_token_path: formValue.auth.response_token_path,
        token_request_body: auth_token_request_body
      };

      if (formValue.auth.token_refresh_endpoint) {
        apiData.auth.token_refresh_endpoint = formValue.auth.token_refresh_endpoint;
        apiData.auth.token_refresh_body = auth_token_refresh_body;
      }
    }

    // Add last_update_date for edit mode
    if (this.data.mode === 'edit' && formValue.last_update_date) {
      apiData.last_update_date = formValue.last_update_date;
    }

    return apiData;
  }

  async save() {
    if (this.form.invalid) {
      // Mark all fields as touched to show validation errors
      Object.keys(this.form.controls).forEach(key => {
        this.form.get(key)?.markAsTouched();
      });
      
      // Check specific JSON fields
      const jsonFields = ['body_template', 'auth.token_request_body', 'auth.token_refresh_body'];
      for (const field of jsonFields) {
        if (!this.validateJSON(field)) {
          this.snackBar.open(`Invalid JSON in ${field}`, 'Close', { 
            duration: 3000,
            panelClass: 'error-snackbar' 
          });
          return;
        }
      }

      this.snackBar.open('Please fix validation errors', 'Close', { duration: 3000 });
      return;
    }

    this.saving = true;
    try {
      const apiData = this.prepareAPIData();

      if (this.data.mode === 'create' || this.data.mode === 'duplicate') {
        await this.apiService.createAPI(apiData).toPromise();
        this.snackBar.open('API created successfully', 'Close', { duration: 3000 });
      } else {
        await this.apiService.updateAPI(this.data.api.name, apiData).toPromise();
        this.snackBar.open('API updated successfully', 'Close', { duration: 3000 });
      }

      this.dialogRef.close(true);
    } catch (error: any) {
      const message = error.error?.detail || 
        (this.data.mode === 'create' ? 'Failed to create API' : 'Failed to update API');
      this.snackBar.open(message, 'Close', { 
        duration: 5000, 
        panelClass: 'error-snackbar' 
      });
    } finally {
      this.saving = false;
    }
  }

  cancel() {
    this.dialogRef.close(false);
  }
}