{
  "name": "RGL8R SIMA Screening",
  "nodes": [
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.RGL8R_BASE_URL }}/api/auth/token/integration",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "={{ $credentials.rgl8rApiKey }}"
            }
          ]
        },
        "options": {}
      },
      "id": "token-exchange",
      "name": "Token Exchange",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [250, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.RGL8R_BASE_URL }}/api/sima/batch",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $node[\"Token Exchange\"].json.access_token }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "body": {
          "json": {
            "skus": null,
            "runPolicy": "always",
            "screeningAuthority": "US"
          }
        },
        "options": {}
      },
      "id": "enqueue-screening",
      "name": "Enqueue Screening",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [470, 300]
    },
    {
      "parameters": {
        "amount": 10,
        "unit": "seconds"
      },
      "id": "wait",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [690, 300]
    },
    {
      "parameters": {
        "jsCode": "// $runIndex is a built-in n8n variable that tracks how many times this node\n// has executed in the current workflow run. It persists across loop iterations\n// regardless of what upstream nodes put in the item JSON.\nconst MAX_POLLS = 40;\nif ($runIndex >= MAX_POLLS) {\n  throw new Error(`Poll limit exceeded (${MAX_POLLS} attempts, ~${MAX_POLLS * 10}s). Job may be stalled: ${$node['Enqueue Screening'].json.jobId}`);\n}\nreturn [{ json: { pollAttempt: $runIndex + 1, maxPolls: MAX_POLLS } }]"
      },
      "id": "poll-guard",
      "name": "Poll Guard",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [910, 300]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ $env.RGL8R_BASE_URL }}/api/jobs/{{ $node[\"Enqueue Screening\"].json.jobId }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $node[\"Token Exchange\"].json.access_token }}"
            }
          ]
        },
        "options": {}
      },
      "id": "check-status",
      "name": "Check Job Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1130, 300]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.status }}",
              "operation": "equal",
              "value2": "COMPLETED"
            }
          ]
        }
      },
      "id": "if-completed",
      "name": "Completed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [1350, 300]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.status }}",
              "operation": "equal",
              "value2": "FAILED"
            }
          ]
        }
      },
      "id": "if-failed",
      "name": "Failed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [1570, 200]
    },
    {
      "parameters": {
        "jsCode": "// Extract job-scoped screening summary from the completed job response.\n// This uses the job's own result payload, not the tenant-wide /sima/results endpoint.\n// To fetch per-SKU detail, add a follow-up node calling GET /api/sima/results.\nconst job = $node['Check Job Status'].json;\nreturn [{ json: {\n  jobId: job.id,\n  status: job.status,\n  outcomes: job.outcomes,\n  reconciliation: job.reconciliation,\n  warnings: job.warnings || null,\n  error: job.error || null\n} }]"
      },
      "id": "job-results",
      "name": "Job Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1570, 400]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: {\n  error: 'SIMA screening job failed',\n  jobId: $node['Enqueue Screening'].json.jobId,\n  status: $node['Check Job Status'].json.status,\n  details: $node['Check Job Status'].json.error || 'No error details available'\n} }]"
      },
      "id": "job-failed",
      "name": "Job Failed",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1790, 100]
    }
  ],
  "connections": {
    "Token Exchange": {
      "main": [
        [{ "node": "Enqueue Screening", "type": "main", "index": 0 }]
      ]
    },
    "Enqueue Screening": {
      "main": [
        [{ "node": "Wait", "type": "main", "index": 0 }]
      ]
    },
    "Wait": {
      "main": [
        [{ "node": "Poll Guard", "type": "main", "index": 0 }]
      ]
    },
    "Poll Guard": {
      "main": [
        [{ "node": "Check Job Status", "type": "main", "index": 0 }]
      ]
    },
    "Check Job Status": {
      "main": [
        [{ "node": "Completed?", "type": "main", "index": 0 }]
      ]
    },
    "Completed?": {
      "main": [
        [{ "node": "Job Results", "type": "main", "index": 0 }],
        [{ "node": "Failed?", "type": "main", "index": 0 }]
      ]
    },
    "Failed?": {
      "main": [
        [{ "node": "Job Failed", "type": "main", "index": 0 }],
        [{ "node": "Wait", "type": "main", "index": 0 }]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "instanceId": "rgl8r-template"
  },
  "tags": [
    { "name": "rgl8r" },
    { "name": "sima" },
    { "name": "compliance" }
  ]
}
