Problem:
- After wizard form submission, need to wait for claim data from n8n
- Claim data comes via Redis channel claim:plan:{session_token}
- Need to display confirmation form with claim data
Solution:
1. Backend: Added SSE endpoint /api/v1/claim-plan/{session_token}
- Subscribes to Redis channel claim:plan:{session_token}
- Streams claim data from n8n to frontend
- Handles timeouts and errors gracefully
2. Frontend: Added subscription to claim:plan channel
- StepWizardPlan: After form submission, subscribes to SSE
- Waits for claim_plan_ready event
- Shows loading message while waiting
- On success: saves claimPlanData and shows confirmation step
3. New component: StepClaimConfirmation
- Displays claim confirmation form in iframe
- Receives claimPlanData from parent
- Generates HTML form (placeholder - should call n8n for real HTML)
- Handles confirmation/cancellation via postMessage
4. ClaimForm: Added conditional step for confirmation
- Shows StepClaimConfirmation when showClaimConfirmation=true
- Step appears after StepWizardPlan
- Only visible when claimPlanData is available
Flow:
1. User fills wizard form → submits
2. Form data sent to n8n via /api/v1/claims/wizard
3. Frontend subscribes to SSE /api/v1/claim-plan/{session_token}
4. n8n processes data → publishes to Redis claim:plan:{session_token}
5. Backend receives → streams to frontend via SSE
6. Frontend receives → shows StepClaimConfirmation
7. User confirms → proceeds to next step
Files:
- backend/app/api/events.py: Added stream_claim_plan endpoint
- frontend/src/components/form/StepWizardPlan.tsx: Added subscribeToClaimPlan
- frontend/src/components/form/StepClaimConfirmation.tsx: New component
- frontend/src/pages/ClaimForm.tsx: Added confirmation step to steps array
80 lines
2.8 KiB
JavaScript
80 lines
2.8 KiB
JavaScript
// n8n Code node (Run Once) — prepare object for Redis
|
||
const items = $input.all();
|
||
|
||
// 1) Найти первый подходящий элемент с parsed.obj
|
||
let main = null;
|
||
for (const it of items) {
|
||
const j = it.json;
|
||
if (!j) continue;
|
||
// возможные места
|
||
if (j.parsed && j.parsed.obj) { main = j.parsed.obj; break; }
|
||
if (j.parsed && j.parsed.ok && j.parsed.obj) { main = j.parsed.obj; break; }
|
||
if (j.output) {
|
||
// если output — строка JSON
|
||
try {
|
||
const parsed = JSON.parse(j.output);
|
||
if (parsed && parsed.wizard_plan) { main = parsed; break; }
|
||
} catch (e) {}
|
||
}
|
||
if (j.json && j.json.wizard_plan) { main = j.json; break; }
|
||
}
|
||
if (!main) {
|
||
// последний шанс: взять items[0].json
|
||
main = items[0] ? (items[0].json || items[0]) : null;
|
||
}
|
||
if (!main) {
|
||
throw new Error('Не удалось найти parsed.obj в входных данных');
|
||
}
|
||
|
||
// 2) Гарантии структуры
|
||
main.wizard_plan = main.wizard_plan || {};
|
||
main.coverage_report = main.coverage_report || {};
|
||
main.coverage_report.docs_received = main.coverage_report.docs_received || [];
|
||
main.wizard_plan.risks = main.wizard_plan.risks || ['DOCS_STATUS_UNKNOWN','EXPECTATION_UNSET'];
|
||
main.wizard_plan.deadlines = main.wizard_plan.deadlines || [
|
||
{ type: 'USER_UPLOAD_TTL', duration_hours: 48 },
|
||
{ type: 'USER_APPROVAL_TTL', duration_hours: 24 }
|
||
];
|
||
|
||
// 3) Добавить примерный документ (state/cities) — если ещё нет такого id
|
||
const exampleId = 'example_state_cities_json';
|
||
const already = main.coverage_report.docs_received.find(d => d.id === exampleId);
|
||
if (!already) {
|
||
const exampleDoc = {
|
||
id: exampleId,
|
||
name: 'state_cities_example.json',
|
||
type: 'application/json',
|
||
uploaded_at: new Date().toISOString(),
|
||
content: {
|
||
state: 'California',
|
||
cities: ['Los Angeles', 'San Francisco', 'San Diego']
|
||
}
|
||
};
|
||
main.coverage_report.docs_received.push(exampleDoc);
|
||
}
|
||
|
||
// 4) session token / key
|
||
// Получаем session_token из разных источников (приоритет: Edit Fields11 > Redis Trigger)
|
||
const sessionToken = $('Edit Fields11').first().json.session_token
|
||
|| $('Redis Trigger').first().json.message.session_id
|
||
|| null;
|
||
|
||
// Если session_token недоступен, генерируем временный ключ
|
||
if (!sessionToken) {
|
||
console.warn('⚠️ session_token не найден, используем временный ключ');
|
||
}
|
||
|
||
// Используем session_token для Redis ключа (claim_id будет сгенерирован позже)
|
||
const redisKey = `ocr_events:${sessionToken || 'temp-' + Date.now()}`;
|
||
|
||
// 5) Возвращаем объект для следующего Redis node
|
||
return [{
|
||
json: {
|
||
redis_key: redisKey,
|
||
redis_value: main
|
||
}
|
||
}];
|
||
|
||
|
||
|