{
  "name": "E-Sign Logic Engine (Workflow 138)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "docusign-connect-webhook",
        "options": {}
      },
      "name": "DocuSign Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "upsert",
        "baseId": "appYOURBASEID",
        "table": "E_Sign_Envelopes",
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Envelope_ID": "={{$json.body.envelopeId}}",
            "Current_Status": "={{$json.body.status}}",
            "Viewed_At": "={{$json.body.viewedAt || ''}}",
            "Signed_At": "={{$json.body.signedAt || ''}}",
            "Recipient_Email": "={{$json.body.email}}"
          }
        },
        "options": {
          "externalIdColumn": "Envelope_ID"
        }
      },
      "name": "Airtable: Update State",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "airtableApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.body.status}}",
              "operation": "equal",
              "value2": "Delivered"
            }
          ]
        }
      },
      "name": "Check: Viewed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "amount": 2,
        "unit": "hours"
      },
      "name": "Wait 2 Hours",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        850,
        200
      ]
    },
    {
      "parameters": {
        "operation": "get",
        "baseId": "appYOURBASEID",
        "table": "E_Sign_Envelopes",
        "id": "={{$node[\"Airtable: Update State\"].json.id}}"
      },
      "name": "Check Current Status",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [
        1050,
        200
      ],
      "credentials": {
        "airtableApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.fields.Current_Status}}",
              "operation": "notEqual",
              "value2": "Completed"
            }
          ]
        }
      },
      "name": "Still Unsigned?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1250,
        200
      ]
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gemini-1.5-flash",
          "mode": "list"
        },
        "prompt": "=Client viewed {{$node[\"Check Current Status\"].json.fields.Document_Name}} 2 hours ago but didn't sign. Draft a polite, low-pressure SMS asking if they have technical issues or specific clause questions. Mention the property {{$node[\"Check Current Status\"].json.fields.Address}}.",
        "options": {}
      },
      "name": "Gemini: Draft Nudge",
      "type": "n8n-nodes-base.googleGeminiChat",
      "typeVersion": 1,
      "position": [
        1450,
        150
      ],
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "from": "+15550001234",
        "to": "={{$node[\"Check Current Status\"].json.fields.Phone}}",
        "message": "={{$json.output}}"
      },
      "name": "Twilio: Send SMS",
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1650,
        150
      ],
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "DocuSign Webhook": {
      "main": [
        [
          {
            "node": "Airtable: Update State",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Airtable: Update State": {
      "main": [
        [
          {
            "node": "Check: Viewed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check: Viewed?": {
      "main": [
        [
          {
            "node": "Wait 2 Hours",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 2 Hours": {
      "main": [
        [
          {
            "node": "Check Current Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Current Status": {
      "main": [
        [
          {
            "node": "Still Unsigned?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Still Unsigned?": {
      "main": [
        [
          {
            "node": "Gemini: Draft Nudge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini: Draft Nudge": {
      "main": [
        [
          {
            "node": "Twilio: Send SMS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}