{
  "name": "API - Issue Creator Credential",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "issue-creator-credential",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.headers['x-acl-auth-key']}}",
              "operation": "equal",
              "value2": "={{$env.ACL_AUTH_KEY}}"
            }
          ]
        }
      },
      "name": "Auth Check",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        200,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const incoming = ($json && ($json.body ?? $json)) ?? ($input && $input.item && $input.item.json && ($input.item.json.body ?? $input.item.json)) ?? {};\nconst body = incoming;\nconst nowIso = new Date().toISOString();\n\nconst computeUniqueHash = (ipfsURI) => {\n  const link = String(ipfsURI || '').trim();\n  if (!link) return `hash-${Date.now()}`;\n  try {\n    const crypto = require('crypto');\n    return 'hash-' + crypto.createHash('sha256').update(link).digest('hex');\n  } catch (e) {\n    let h = 0;\n    for (let i = 0; i < link.length; i++) {\n      h = ((h << 5) - h) + link.charCodeAt(i);\n      h |= 0;\n    }\n    return 'hash-fallback-' + Math.abs(h).toString(16);\n  }\n};\n\nconst makeDoc = (item = {}) => {\n  const tokenId = String(item.tokenId || ('0.0.' + (Math.floor(Math.random() * 9000000) + 1000000)));\n  const serialNumber = String(item.serialNumber || '1');\n  const universityId = item.universityId || null;\n  const studentAccountId = (item.studentAccountId === undefined) ? null : item.studentAccountId;\n  const ipfsURI = item.ipfsURI || 'ipfs://QmTest';\n  const uniqueHash = item.uniqueHash || computeUniqueHash(ipfsURI);\n  const ext = item.externalProofs || {};\n  const externalProofs = {\n    hederaTx: ext.hederaTx || null,\n    xrpTxHash: ext.xrpTxHash || null,\n    algoTxId: ext.algoTxId || null\n  };\n  return {\n    tokenId,\n    serialNumber,\n    universityId,\n    studentAccountId,\n    uniqueHash,\n    ipfsURI,\n    externalProofs,\n    createdAt: nowIso,\n    updatedAt: nowIso,\n    __v: 0\n  };\n};\n\nif (Array.isArray(body)) {\n  const docs = body.map(makeDoc);\n  return docs.map(d => ({ json: d }));\n} else {\n  return { json: makeDoc(body) };\n}"
      },
      "name": "Mint Logic (Stub)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        400,
        0
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "issued_credentials",
        "options": {}
      },
      "name": "Save to Mongo",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        600,
        0
      ],
      "credentials": {
        "mongoDb": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"success\": true,\n  \"data\": {{$json}}\n}",
        "options": {}
      },
      "name": "Respond Success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        800,
        0
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Auth Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Auth Check": {
      "main": [
        [
          {
            "node": "Mint Logic (Stub)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mint Logic (Stub)": {
      "main": [
        [
          {
            "node": "Save to Mongo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save to Mongo": {
      "main": [
        [
          {
            "node": "Respond Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}