{
  "kind": "genhealth.workflow_definition",
  "schema_version": 1,
  "workflow": {
    "name": "Provider intake: match or create an extracted patient",
    "description": "Requires an extracted organization-owned document and configured Brightree strategy. Review endpoints are annotations only.",
    "nodes": [
      {
        "id": "match",
        "type": "integration",
        "name": "Check extracted patient",
        "config": {
          "handler": "brightree_check_patient_exists",
          "params": {
            "document_id": "{{trigger.default.document_id}}",
            "force": false
          },
          "_group": "Brightree"
        },
        "error_strategy": "fail_branch"
      },
      {
        "id": "matched_ok",
        "type": "if",
        "name": "Match succeeded?",
        "config": {
          "condition": "{{match.default.success}}"
        }
      },
      {
        "id": "exists",
        "type": "if",
        "name": "Patient exists?",
        "config": {
          "condition": "{{match.default.exists}}"
        }
      },
      {
        "id": "can_create",
        "type": "if",
        "name": "Ready to create?",
        "config": {
          "condition": "{{match.default.ready_for_patient_create}}"
        }
      },
      {
        "id": "create_patient",
        "type": "integration",
        "name": "Create or reuse patient",
        "config": {
          "handler": "brightree_create_patient_from_document",
          "params": {
            "document_id": "{{trigger.default.document_id}}"
          },
          "_group": "Brightree"
        },
        "error_strategy": "fail_branch"
      },
      {
        "id": "created_ok",
        "type": "if",
        "name": "Creation succeeded?",
        "config": {
          "condition": "{{create_patient.default.success}}"
        }
      },
      {
        "id": "existing_patient",
        "type": "comment",
        "name": "Existing patient",
        "config": {
          "heading": "Matched",
          "body": "Use match.default.patient_key for a subsequent reviewed step."
        }
      },
      {
        "id": "creation_success",
        "type": "comment",
        "name": "Patient resolved",
        "config": {
          "heading": "Resolved",
          "body": "Use create_patient.default.patient_key for a subsequent reviewed step."
        }
      },
      {
        "id": "review",
        "type": "comment",
        "name": "Review required",
        "config": {
          "heading": "Review",
          "body": "Stop here; replace this annotation with your configured review task or alert."
        }
      }
    ],
    "edges": [
      {
        "source_node_id": "match",
        "source_output": "default",
        "target_node_id": "matched_ok",
        "target_input": "default"
      },
      {
        "source_node_id": "matched_ok",
        "source_output": "true",
        "target_node_id": "exists",
        "target_input": "default"
      },
      {
        "source_node_id": "matched_ok",
        "source_output": "false",
        "target_node_id": "review",
        "target_input": "default"
      },
      {
        "source_node_id": "exists",
        "source_output": "true",
        "target_node_id": "existing_patient",
        "target_input": "default"
      },
      {
        "source_node_id": "exists",
        "source_output": "false",
        "target_node_id": "can_create",
        "target_input": "default"
      },
      {
        "source_node_id": "can_create",
        "source_output": "true",
        "target_node_id": "create_patient",
        "target_input": "default"
      },
      {
        "source_node_id": "can_create",
        "source_output": "false",
        "target_node_id": "review",
        "target_input": "default"
      },
      {
        "source_node_id": "create_patient",
        "source_output": "default",
        "target_node_id": "created_ok",
        "target_input": "default"
      },
      {
        "source_node_id": "created_ok",
        "source_output": "true",
        "target_node_id": "creation_success",
        "target_input": "default"
      },
      {
        "source_node_id": "created_ok",
        "source_output": "false",
        "target_node_id": "review",
        "target_input": "default"
      },
      {
        "source_node_id": "match",
        "source_output": "error",
        "target_node_id": "review",
        "target_input": "default"
      },
      {
        "source_node_id": "create_patient",
        "source_output": "error",
        "target_node_id": "review",
        "target_input": "default"
      }
    ]
  }
}
