Commit Graph

3 Commits

Author SHA1 Message Date
AI Assistant
40ad46c026 fix: Parse wizard_plan from edit_fields_parsed structure
Problem:
- wizard_plan comes in nested structure: edit_fields_parsed.wizard_plan_parsed
- Old SQL looked for wizard_plan at root level (partial.p->'wizard_plan')
- Result: wizard_plan was always NULL even though it existed in payload

Solution:
- Updated wizard_plan_parsed CTE to check multiple locations:
  1. edit_fields_parsed.wizard_plan_parsed (already parsed object) 
  2. edit_fields_raw.body.wizard_plan (escaped JSON string)
  3. Root level wizard_plan (backward compatibility)
  4. Database fallback (if not in payload)

- Updated wizard_answers_parsed CTE:
  1. edit_fields_raw.body.wizard_answers (string) 
  2. edit_fields_parsed.body.wizard_answers (string)
  3. Root level (backward compatibility)

- Updated session_id, unified_id, contact_id, phone extraction:
  COALESCE() checks edit_fields_parsed.body and edit_fields_raw.body

Input structure from n8n workflow form_get:
{
  "payload_partial_json": {
    "edit_fields_parsed": {
      "wizard_plan_parsed": {...},  ← TARGET
      "body": {
        "session_id": "sess_xxx",  ← ALSO HERE
        "unified_id": "usr_xxx",
        "wizard_answers": "{...}"
      }
    }
  }
}

Now: wizard_plan preserves correctly when uploading files 
2025-11-21 16:10:57 +03:00
AI Assistant
60a67c7e37 fix: Preserve wizard_plan and AI fields from DB when updating claim
Problem:
- When uploading files on Step 3, wizard_plan was reset to NULL
- wizard_plan is created on Step 2 (StepDescription) and saved by claimsave_primary
- form_get workflow on Step 3 doesn't receive wizard_plan again
- Old SQL was overwriting wizard_plan with NULL

Solution:
- Add 'existing_claim' CTE to read current payload from DB
- Modified all parsers to fallback to DB values if field not in incoming payload:
  * wizard_plan_parsed - preserves generated wizard plan
  * answers_prefill_parsed - preserves AI-generated prefill
  * coverage_report_parsed - preserves coverage analysis
  * ai_agent1_facts_parsed - preserves fact extraction
  * ai_agent13_rag_parsed - preserves RAG analysis
  * problem_description_parsed - preserves user description

Flow:
1. Step 2: User describes problem → claimsave_primary saves wizard_plan 
2. Step 3: User uploads files → form_get/claimsave preserves wizard_plan from DB 

File: docs/SQL_CLAIMSAVE_UPSERT_SIMPLE.sql
Next: Update n8n workflow 'form_get' node 'claimsave' with this SQL
2025-11-21 16:04:55 +03:00
AI Assistant
d6b17baa7d feat: Add PostgreSQL fields and workflow for form without files
Database changes:
- Add unified_id, contact_id, phone columns to clpr_claims table
- Create indexes for fast lookup by these fields
- Migrate existing data from payload to new columns
- SQL migration: docs/SQL_ALTER_CLPR_CLAIMS_ADD_FIELDS.sql

SQL improvements:
- Simplify claimsave query: remove complex claim_lookup logic
- Use UPSERT (INSERT ON CONFLICT) with known claim_id
- Always return claim (fix NULL issue)
- Store unified_id, contact_id, phone directly in table columns
- SQL: docs/SQL_CLAIMSAVE_UPSERT_SIMPLE.sql

Workflow enhancements:
- Add branch for form submissions WITHOUT files
- Create 6 new nodes: extract, prepare, save, redis, respond
- Separate flow for has_files=false in IF node
- Instructions: docs/N8N_FORM_GET_NO_FILES_INSTRUCTIONS.md
- Node config: docs/N8N_FORM_GET_NO_FILES_BRANCH.json

Migration stats:
- Total claims: 81
- With unified_id: 77
- Migrated from payload: 2

Next steps:
1. Add 6 nodes to n8n workflow form_get (ID: 8ZVMTsuH7Cmw7snw)
2. Connect TRUE branch of IF node to extract_webhook_data_no_files
3. Test form submission without files
4. Verify PostgreSQL save and Redis event
2025-11-21 15:57:18 +03:00