{
  "name": "Prometiam: check a new Xero supplier",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "daysInterval": 1,
              "triggerAtHour": 7
            }
          ]
        }
      },
      "id": "3b7a1c10-5d2e-4f60-8a11-0c9e7d200001",
      "name": "Every morning",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "resource": "contact",
        "operation": "getAll",
        "organizationId": "",
        "returnAll": true,
        "options": {}
      },
      "id": "3b7a1c10-5d2e-4f60-8a11-0c9e7d200002",
      "name": "Read Xero contacts",
      "type": "n8n-nodes-base.xero",
      "typeVersion": 1.1,
      "position": [
        240,
        0
      ],
      "credentials": {
        "xeroOAuth2Api": {
          "name": "Xero account"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "// Which Xero contacts to check now. Reads the identifiers from what the Xero node returns.\n// Field names below are the ones to confirm on your first run (page: \"Before you rely on it\").\nconst MAX_PER_RUN = 25;                       // protects your credits: the rest waits for the next run\nconst ISO = { 'united kingdom': 'GB', 'uk': 'GB', 'ireland': 'IE', 'spain': 'ES', 'france': 'FR', 'germany': 'DE',\n  'norway': 'NO', 'netherlands': 'NL', 'belgium': 'BE', 'sweden': 'SE', 'denmark': 'DK', 'finland': 'FI', 'poland': 'PL' };\nconst seen = $getWorkflowStaticData('global');\nseen.checked = seen.checked || {};\nconst out = [];\nfor (const item of $input.all()) {\n  const c = item.json;\n  const id = c.ContactID || c.ContactId;\n  if (!id || seen.checked[id]) continue;\n  if (c.ContactStatus && String(c.ContactStatus).toUpperCase() !== 'ACTIVE') continue;\n  const addr = (c.Addresses || []).find(a => a.Country) || {};\n  const raw = String(addr.Country || '').trim();\n  const country = raw.length === 2 ? raw.toUpperCase() : (ISO[raw.toLowerCase()] || '');\n  out.push({ json: {\n    contactId: id,\n    name: c.Name || '',\n    country,\n    companyNumber: String(c.CompanyNumber || c.companyNumber || '').replace(/\\s+/g, ''),\n    taxNumber: String(c.TaxNumber || c.taxNumber || '').replace(/[\\s.-]+/g, ''),\n  } });\n  if (out.length >= MAX_PER_RUN) break;\n}\nfor (const o of out) seen.checked[o.json.contactId] = new Date().toISOString().slice(0, 10);\nreturn out;\n"
      },
      "id": "3b7a1c10-5d2e-4f60-8a11-0c9e7d200003",
      "name": "Pick contacts to check",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        0
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://api.prometiam.com/functions/v1/risk-api/companies/check",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "country",
              "value": "={{ $json.country }}"
            },
            {
              "name": "company_number",
              "value": "={{ $json.companyNumber }}"
            },
            {
              "name": "checks",
              "value": "record,insolvency"
            }
          ]
        },
        "options": {}
      },
      "id": "3b7a1c10-5d2e-4f60-8a11-0c9e7d200004",
      "name": "Company check",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        720,
        0
      ],
      "onError": "continueRegularOutput",
      "credentials": {
        "httpHeaderAuth": {
          "name": "Prometiam API key"
        }
      }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.prometiam.com/functions/v1/risk-api/vat/{{ $('Pick contacts to check').item.json.taxNumber }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "id": "3b7a1c10-5d2e-4f60-8a11-0c9e7d200005",
      "name": "VIES VAT check",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        960,
        0
      ],
      "onError": "continueRegularOutput",
      "credentials": {
        "httpHeaderAuth": {
          "name": "Prometiam API key"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "// One line per checked supplier: what came back, and whether a person should look at it.\nconst contacts = $('Pick contacts to check').all();\nconst checks = $('Company check').all();\nconst out = [];\nconst norm = s => String(s || '').toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim();\n$input.all().forEach((vatItem, i) => {\n  const contact = contacts[i].json;\n  const check = checks[i].json;\n  const vat = vatItem.json;\n  const flags = [];\n  const d = check.data;\n  if (check.error || !d || !d.record) {\n    flags.push('not_checked: needs a country and a company number on the contact');\n  } else {\n    if (d.record.status_group !== 'active') flags.push('register_status_' + d.record.status);\n    if (d.insolvency && d.insolvency.status === 'found') flags.push('insolvency_notice_found');\n    if (d.insolvency && d.insolvency.status === 'not_available') flags.push('info: country has no insolvency notices here, not a clear result');\n    if (norm(d.record.company_name) !== norm(contact.name)) flags.push('info: name in Xero differs from the register: ' + d.record.company_name);\n  }\n  if (contact.taxNumber) {\n    if (vat.error || !vat.data) flags.push('vat_not_confirmed: VIES gave no answer (outage, or a country VIES does not cover)');\n    else if (vat.data.is_valid === false) flags.push('vat_invalid_in_vies');\n  } else if (d && d.record && d.record.vat_number) {\n    flags.push('info: no tax number in Xero; the register-based number is ' + d.record.vat_number + ' (' + d.record.vat_number_source + ')');\n  }\n  const hard = flags.filter(f => !f.startsWith('info:'));\n  out.push({ json: { ...contact, review: hard.length > 0, flags } });\n});\nreturn out;\n"
      },
      "id": "3b7a1c10-5d2e-4f60-8a11-0c9e7d200006",
      "name": "Decide",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1200,
        0
      ]
    }
  ],
  "connections": {
    "Every morning": {
      "main": [
        [
          {
            "node": "Read Xero contacts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Xero contacts": {
      "main": [
        [
          {
            "node": "Pick contacts to check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pick contacts to check": {
      "main": [
        [
          {
            "node": "Company check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Company check": {
      "main": [
        [
          {
            "node": "VIES VAT check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "VIES VAT check": {
      "main": [
        [
          {
            "node": "Decide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
