{
  "name": "job.6_fsm_mutator",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "69b67ac7-81f5-402e-9ffe-9d31b306d748",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT current_state\nFROM job_applications\nWHERE application_id = {{$json.application_id}};",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        432,
        208
      ],
      "id": "8bd17028-1e28-4a9a-bc2c-007b6d1266b4",
      "name": "Check state matches",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "66ba2f81-c438-4404-86af-b0a4ec046c2e",
              "leftValue": "={{ $json.current_state === $node[\"Select eligible transitions\"].json.from_state }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        640,
        208
      ],
      "id": "f470ce8a-eef6-4c94-828e-73638fa68aef",
      "name": "If"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT\n  ev.event_id      AS fsm_event_id,\n  ev.application_id,\n  ev.payload->>'from_state' AS from_state,\n  ev.payload->>'to_state'   AS to_state\nFROM events ev\nLEFT JOIN events done\n  ON done.event_type = 'STATE_TRANSITIONED'\n AND done.payload->>'fsm_event_id' = ev.event_id::text\nWHERE ev.event_type = 'FSM_TRANSITION_ELIGIBLE'\n  AND done.event_id IS NULL;\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        208,
        208
      ],
      "id": "96967178-7e7b-4238-a459-ca0670203104",
      "name": "Select eligible transitions",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "BEGIN;\n\nUPDATE job_applications\nSET\n  current_state = '{{$node[\"Select eligible transitions\"].json.to_state}}',\n  last_state_change = NOW()\nWHERE application_id = {{$node[\"Select eligible transitions\"].json.application_id}}\n  AND current_state = '{{$node[\"Select eligible transitions\"].json.from_state}}';\n\nINSERT INTO events (\n  event_type,\n  application_id,\n  payload,\n  triggered_by\n) VALUES (\n  'STATE_TRANSITIONED',\n  {{$node[\"Select eligible transitions\"].json.application_id}},\n  jsonb_build_object(\n    'fsm_event_id', {{$node[\"Select eligible transitions\"].json.fsm_event_id}},\n    'from_state', '{{$node[\"Select eligible transitions\"].json.from_state}}',\n    'to_state', '{{$node[\"Select eligible transitions\"].json.to_state}}'\n  ),\n  'system'\n);\n\nCOMMIT;\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        832,
        -16
      ],
      "id": "6053fead-593b-41da-bf57-82db0c4b5653",
      "name": "Apply mutation",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Select eligible transitions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check state matches": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Select eligible transitions": {
      "main": [
        [
          {
            "node": "Check state matches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Apply mutation",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": false
  },
  "versionId": "7d7382cb-f09d-4168-a487-98beb658d246",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "XSGcd0B9a036EEbd",
  "tags": []
}