{
  "nodes": [
    {
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -550,
        450
      ],
      "parameters": {
        "path": "PuHq2RQsmc3HXB/hook",
        "options": {
          "rawBody": false
        },
        "httpMethod": "POST"
      },
      "typeVersion": 1,
      "id": "node-ac41d08e"
    },
    {
      "name": "Mautic",
      "type": "n8n-nodes-base.mautic",
      "position": [
        1260,
        180
      ],
      "parameters": {
        "email": "={{$(\"@MAIN STUDENT DATA\").item.json[\"student\"][\"email\"]}}",
        "company": 1,
        "options": {},
        "lastName": "={{$(\"@MAIN STUDENT DATA\").item.json[\"student\"][\"lastName\"]}}",
        "firstName": "={{$(\"@MAIN STUDENT DATA\").item.json[\"student\"][\"firstName\"]}}",
        "authentication": "{{ $credentials.oAuth2 }}",
        "additionalFields": {}
      },
      "credentials": {
        "mauticOAuth2Api": "<your credential>"
      },
      "typeVersion": 1,
      "alwaysOutputData": false,
      "id": "node-a642a0cf"
    },
    {
      "name": "Find User",
      "type": "n8n-nodes-base.mautic",
      "position": [
        170,
        260
      ],
      "parameters": {
        "limit": 1,
        "options": {
          "search": "={{$(\"Set Webhook Request\").item.json[\"student\"][\"email\"]}}"
        },
        "operation": "getAll",
        "authentication": "{{ $credentials.oAuth2 }}"
      },
      "credentials": {
        "mauticOAuth2Api": "<your credential>"
      },
      "notesInFlow": false,
      "typeVersion": 1,
      "alwaysOutputData": false,
      "id": "node-8f9bd912"
    },
    {
      "name": "Update User",
      "type": "n8n-nodes-base.mautic",
      "position": [
        1560,
        250
      ],
      "parameters": {
        "options": {},
        "contactId": "={{$(\"@MAIN STUDENT DATA\").item.json[\"userFound\"]}}",
        "operation": "update",
        "updateFields": {
          "email": "={{$(\"@MAIN STUDENT DATA\").item.json[\"student\"][\"email\"]}}",
          "lastName": "={{$(\"@MAIN STUDENT DATA\").item.json[\"student\"][\"lastName\"]}}",
          "firstName": "={{$(\"@MAIN STUDENT DATA\").item.json[\"student\"][\"firstName\"]}}"
        },
        "authentication": "{{ $credentials.oAuth2 }}"
      },
      "credentials": {
        "mauticOAuth2Api": "<your credential>"
      },
      "typeVersion": 1,
      "id": "node-fac3ef36"
    },
    {
      "name": "Tag User",
      "type": "n8n-nodes-base.mautic",
      "position": [
        430,
        670
      ],
      "parameters": {
        "options": {},
        "contactId": "={{$(\"Find User To Tag Sale\").item.json[\"id\"]}}",
        "operation": "update",
        "updateFields": {
          "tags": "={{$(\"Set Webhook Request\").item.json[\"student\"][\"course\"][\"name\"]}}"
        },
        "authentication": "{{ $credentials.oAuth2 }}"
      },
      "credentials": {
        "mauticOAuth2Api": "<your credential>"
      },
      "typeVersion": 1,
      "id": "node-7f5515e6"
    },
    {
      "name": "Unsubscribe User",
      "type": "n8n-nodes-base.mautic",
      "position": [
        2170,
        410
      ],
      "parameters": {
        "options": {},
        "contactId": "={{$(\"@MAIN STUDENT DATA\").item.json[\"userFound\"]}}",
        "operation": "update",
        "updateFields": {
          "tags": "=#unsubscribe"
        },
        "authentication": "{{ $credentials.oAuth2 }}"
      },
      "credentials": {
        "mauticOAuth2Api": "<your credential>"
      },
      "typeVersion": 1,
      "id": "node-75041876"
    },
    {
      "name": "Split Full Name",
      "type": "n8n-nodes-base.code",
      "position": [
        340,
        420
      ],
      "parameters": {
        "jsCode": "const student = items[0].json.student\nstudent.firstName = student.name ? student.name.split(' ').slice(0, -1).join(' ') : ''\nstudent.lastName= student.name ? student.name.split(' ').slice(-1).join(' ') : ''\nitems[0].json.student = student\nreturn items;"
      },
      "typeVersion": 2,
      "id": "node-0f4593d8"
    },
    {
      "name": "If not found return -1",
      "type": "n8n-nodes-base.code",
      "position": [
        450,
        260
      ],
      "parameters": {
        "jsCode": "items[0].json.id = items[0].json.id || -1\nreturn items"
      },
      "typeVersion": 2,
      "id": "node-285ac437"
    },
    {
      "name": "@MAIN STUDENT DATA",
      "type": "n8n-nodes-base.merge",
      "position": [
        900,
        400
      ],
      "parameters": {
        "join": "inner",
        "mode": "mergeByIndex"
      },
      "typeVersion": 1,
      "id": "node-da23188f"
    },
    {
      "name": "Remove unsubscribe",
      "type": "n8n-nodes-base.mautic",
      "position": [
        1770,
        500
      ],
      "parameters": {
        "options": {},
        "contactId": "={{$(\"@MAIN STUDENT DATA\").item.json[\"userFound\"]}}",
        "operation": "update",
        "updateFields": {
          "tags": "=-#unsubscribe"
        },
        "authentication": "{{ $credentials.oAuth2 }}"
      },
      "credentials": {
        "mauticOAuth2Api": "<your credential>"
      },
      "typeVersion": 1,
      "id": "node-1a0b7db0"
    },
    {
      "name": "Find User To Tag Sale",
      "type": "n8n-nodes-base.mautic",
      "position": [
        190,
        670
      ],
      "parameters": {
        "limit": 1,
        "options": {
          "search": "={{$(\"Set Webhook Request\").item.json[\"student\"][\"user\"][\"email\"]}}"
        },
        "operation": "getAll",
        "authentication": "{{ $credentials.oAuth2 }}"
      },
      "credentials": {
        "mauticOAuth2Api": "<your credential>"
      },
      "notesInFlow": false,
      "typeVersion": 1,
      "alwaysOutputData": false,
      "id": "node-72027eef"
    },
    {
      "name": "Set userFound",
      "type": "n8n-nodes-base.set",
      "position": [
        700,
        260
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "userFound",
              "value": "={{$(\"If not found return -1\").item.json[\"id\"]}}"
            }
          ]
        },
        "options": {},
        "keepOnlySet": true
      },
      "typeVersion": 1,
      "id": "node-f7657e14"
    },
    {
      "name": "Switch Webhook Types",
      "type": "n8n-nodes-base.switch",
      "position": [
        -70,
        450
      ],
      "parameters": {
        "rules": {
          "rules": [
            {
              "value2": "User.",
              "operation": "contains"
            },
            {
              "output": 1,
              "value2": "Sale.",
              "operation": "contains"
            }
          ]
        },
        "value1": "={{$(\"Set Webhook Request\").item.json[\"type\"]}}",
        "dataType": "string"
      },
      "typeVersion": 1,
      "id": "node-53d27675"
    },
    {
      "name": "Set Webhook Request",
      "type": "n8n-nodes-base.set",
      "position": [
        -310,
        450
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "student",
              "value": "={{$(\"Webhook\").item.json[\"body\"][\"object\"]}}"
            },
            {
              "name": "type",
              "value": "={{$(\"Webhook\").item.json[\"body\"][\"type\"]}}"
            }
          ]
        },
        "options": {},
        "keepOnlySet": true
      },
      "typeVersion": 1,
      "id": "node-bf0f164a"
    },
    {
      "name": "IF NOT userFound",
      "type": "n8n-nodes-base.if",
      "position": [
        1090,
        400
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$(\"@MAIN STUDENT DATA\").item.json[\"userFound\"]}}",
              "value2": "-1",
              "operation": "regex"
            }
          ]
        }
      },
      "typeVersion": 1,
      "id": "node-56ec6298"
    },
    {
      "name": "Switch User.type",
      "type": "n8n-nodes-base.switch",
      "position": [
        1380,
        420
      ],
      "parameters": {
        "rules": {
          "rules": [
            {
              "value2": "User.updated"
            },
            {
              "output": 1,
              "value2": "User.unsubscribe_from_marketing_emails"
            },
            {
              "output": 2,
              "value2": "=User.subscribe_to_marketing_emails"
            }
          ]
        },
        "value1": "={{$(\"@MAIN STUDENT DATA\").item.json[\"type\"]}}",
        "dataType": "string"
      },
      "typeVersion": 1,
      "id": "node-47779f5a"
    },
    {
      "name": "IF unsubscribe_from_marketing_emails",
      "type": "n8n-nodes-base.if",
      "position": [
        1770,
        250
      ],
      "parameters": {
        "conditions": {
          "string": [],
          "boolean": [
            {
              "value1": "={{$(\"@MAIN STUDENT DATA\").item.json[\"student\"][\"unsubscribe_from_marketing_emails\"]}}",
              "value2": true
            }
          ]
        }
      },
      "typeVersion": 1,
      "id": "node-1da97c49"
    },
    {
      "id": "error-3edc58bc",
      "name": "Error Handler",
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [
        1000,
        400
      ],
      "parameters": {
        "message": "Workflow execution error",
        "options": {}
      }
    }
  ],
  "connections": {},
  "name": "Production Workflow",
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner",
    "errorWorkflow": null,
    "timezone": "UTC",
    "executionTimeout": 3600,
    "maxExecutions": 1000,
    "retryOnFail": true,
    "retryCount": 3
  },
  "meta": {
    "versionId": "1.0.0",
    "createdAt": "2025-09-29T07:07:50.926513",
    "updatedAt": "2025-09-29T07:07:50.926530",
    "owner": "n8n-user",
    "license": "MIT",
    "category": "automation",
    "status": "active",
    "priority": "high",
    "environment": "production"
  },
  "description": "Production-ready workflow: Production Workflow. This workflow has been optimized for production use with comprehensive error handling, security, and documentation.",
  "id": "wfe1ef0640c57b"
}