{
  "meta": {
    "instanceId": "3d99487a7c6bdb160b6791f50f0710054ab55548aa4924b78955b57aa39ef75a",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "1edc9748-bb20-4af5-8b18-e7871646c0c5",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        256,
        144
      ],
      "parameters": {
        "color": 7,
        "width": 352,
        "height": 208,
        "content": "Check if id not locked."
      },
      "typeVersion": 1
    },
    {
      "id": "b13fee31-3784-44dc-8780-0453501396ae",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "color": 6,
        "width": 2704,
        "height": 504,
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "eccd5af0-501a-4764-8405-d2d81d28f701",
      "name": "Redis",
      "type": "n8n-nodes-base.redis",
      "position": [
        272,
        208
      ],
      "parameters": {
        "key": "=lock_{{ $json.body.entity.id }}",
        "options": {},
        "operation": "get",
        "propertyName": "item"
      },
      "credentials": {
        "redis": {
          "id": "credential-id",
          "name": "redis Credential"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "75cc16c7-c77e-4493-9980-573c8ed75623",
      "name": "Filter",
      "type": "n8n-nodes-base.filter",
      "position": [
        496,
        208
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "3fcf7dc9-ee2f-4bbd-a9bc-68c970f4c440",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.item }}",
              "rightValue": "Update"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "1e08db05-04c6-4793-af2e-85f5ac7e5dbc",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        944,
        208
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "dd94bb83-a2c4-451a-b577-7df8a09d644d",
                    "operator": {
                      "type": "string",
                      "operation": "empty",
                      "singleValue": true
                    },
                    "leftValue": "={{$json.property_notes}}",
                    "rightValue": ""
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "2d1b0ba9-13ae-442d-8cc2-f707ac92b8b5",
      "name": "Mapping",
      "type": "n8n-nodes-base.code",
      "position": [
        1168,
        208
      ],
      "parameters": {
        "jsCode": "let output = {};\n\nconst name =$('Get Notion page').first().json.name;\n\nconst sub = $('Get Notion page').first().json.property_subject;\n\noutput.destination = `https://nextcloud.yuvrajsingh.icu/remote.php/dav/files/yuvrajsingh/Documents/Notes/OneNote_Notes/${sub}/${name}.one`;\n\noutput.url = 'http://localhost:3069/open?file=C%3A%5CUsers%5CYuvrajSingh%5CNextcloud%5CDocuments%5CNotes%5COneNote_Notes%5C' + encodeURIComponent(`${sub}\\\\${name}.one`);\n\noutput.name = name;\noutput.subject = sub;\n\nreturn { json: output };"
      },
      "typeVersion": 2
    },
    {
      "id": "4003ddf6-e467-4e40-ab65-5dd6dba8857f",
      "name": "Update task in Notion",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2288,
        112
      ],
      "parameters": {
        "url": "=https://api.notion.com/v1/pages/{{ $('Get Notion page').item.json.id }}",
        "method": "PATCH",
        "options": {},
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "=properties.Notes.url",
              "value": "={{ $('Mapping').item.json.url }}"
            }
          ]
        },
        "nodeCredentialType": "notionApi"
      },
      "credentials": {
        "notionApi": {
          "id": "credential-id",
          "name": "notionApi Credential"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "0b2c3906-6b43-4c21-89fa-0dd3a24f6c62",
      "name": "Store",
      "type": "n8n-nodes-base.redis",
      "position": [
        2512,
        208
      ],
      "parameters": {
        "key": "=lock_{{ $json.id }}",
        "ttl": 80,
        "value": "Updated",
        "expire": true,
        "keyType": "string",
        "operation": "set"
      },
      "credentials": {
        "redis": {
          "id": "credential-id",
          "name": "redis Credential"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a3cd198a-58f4-4d0f-bd20-b70431c51873",
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2064,
        112
      ],
      "parameters": {
        "url": "https://nextcloud.yuvrajsingh.icu/remote.php/dav/files/yuvrajsingh/Documents/Notes/OneNote_Notes/Templates/empty.one",
        "method": "=COPY",
        "options": {},
        "sendHeaders": true,
        "authentication": "predefinedCredentialType",
        "headerParameters": {
          "parameters": [
            {
              "name": "Destination",
              "value": "={{ $('Mapping').item.json.destination }}"
            }
          ]
        },
        "nodeCredentialType": "nextCloudApi"
      },
      "credentials": {
        "nextCloudApi": {
          "id": "credential-id",
          "name": "nextCloudApi Credential"
        }
      },
      "retryOnFail": true,
      "typeVersion": 4.2
    },
    {
      "id": "5ca8d253-a072-416c-95bd-db0e6f380315",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        48,
        208
      ],
      "webhookId": "09aff775-83ef-404f-9b05-1bc806f4dbcd",
      "parameters": {
        "path": "09aff775-83ef-404f-9b05-1bc806f4dbcd",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2
    },
    {
      "id": "7f7db51d-d733-4b7b-b31a-0bdd55557ea1",
      "name": "Get Notion page",
      "type": "n8n-nodes-base.notion",
      "position": [
        720,
        208
      ],
      "parameters": {
        "pageId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Webhook').item.json.body.entity.id }}"
        },
        "resource": "databasePage",
        "operation": "get"
      },
      "credentials": {
        "notionApi": {
          "id": "credential-id",
          "name": "notionApi Credential"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "e619cf72-e119-4590-ac05-91f648c2eb42",
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1392,
        208
      ],
      "parameters": {
        "url": "=https://nextcloud.yuvrajsingh.icu/remote.php/dav/files/yuvrajsingh/Documents/Notes/OneNote_Notes/{{ $json.subject }}",
        "body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">   <d:prop>     <d:displayname/>       </d:prop> </d:propfind>",
        "method": "=PROPFIND",
        "options": {},
        "sendBody": true,
        "contentType": "raw",
        "sendHeaders": true,
        "authentication": "predefinedCredentialType",
        "rawContentType": "application/xml",
        "specifyHeaders": "=keypair",
        "headerParameters": {
          "parameters": [
            {}
          ]
        },
        "nodeCredentialType": "nextCloudApi"
      },
      "credentials": {
        "nextCloudApi": {
          "id": "credential-id",
          "name": "nextCloudApi Credential"
        }
      },
      "retryOnFail": true,
      "typeVersion": 4.2
    },
    {
      "id": "c1790980-6ca9-4f88-a4f7-a75f08725b68",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        1840,
        208
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "9e43574d-a13f-4d54-93f5-c419b6624317",
              "operator": {
                "type": "boolean",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.match }}",
              "rightValue": "={{true}}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "b125ebb7-c655-4b34-bfc1-c845a8c87eea",
      "name": "Match titiles",
      "type": "n8n-nodes-base.code",
      "position": [
        1616,
        208
      ],
      "parameters": {
        "jsCode": "// Robust comparison: only direct children, only .one files, strict basename match\nconst extractHrefValues = (text) => {\n  const regex = /<d:href>(.*?)<\\/d:href>/g;\n  const out = [];\n  let m;\n  while ((m = regex.exec(text)) !== null) out.push(m[1]);\n  return out;\n};\n\n// Normalize consistently\nconst norm = (s) => (s || '').trim().normalize('NFKC').toLowerCase();\n\n// Inputs\nconst xml = $json.data || '';\nconst notionName = norm($('Mapping').first().json.name);\n\n// Optional: pass the collection path you PROPFINDed (exact trailing slash form)\nconst basePath = norm($json.basePath || ''); // e.g., \"/remote.php/dav/files/user/OneNote/Notebook/\" lowercased\n\n// Collect candidate file basenames from direct children only\nconst hrefs = extractHrefValues(xml);\n\n// Keep only hrefs that are direct children of basePath (or if basePath not provided, fallback to .one at any depth)\nconst candidates = hrefs.filter((h) => {\n  if (!h.endsWith('.one')) return false;\n  try {\n    const decoded = decodeURIComponent(h);\n    if (basePath) {\n      // Must start with basePath and have no additional slashes after basePath in the remaining segment\n      if (!decoded.toLowerCase().startsWith($json.basePath.toLowerCase())) return false;\n      const rest = decoded.slice($json.basePath.length);\n      if (rest.includes('/')) return false; // exclude nested paths\n    }\n    return true;\n  } catch {\n    return false;\n  }\n});\n\n// Map to normalized basenames (no extension)\nconst existingNames = candidates.map((h) => {\n  const decoded = decodeURIComponent(h);\n  const last = decoded.split('/').filter(Boolean).pop() || '';\n  const base = last.replace(/\\.[^/.]+$/, '');\n  return norm(base);\n});\n\n// Strict equality against Notion name\nconst match = existingNames.includes(notionName);\n\nreturn [{ json: { match } }]; // This means if condition of equal to met than true.\n"
      },
      "typeVersion": 2
    },
    {
      "id": "85ac01ea-efe2-4ba2-a2f2-b667f905b0d3",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        2064,
        304
      ],
      "parameters": {
        "options": {},
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {}
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "9653487b-8a3d-4dd1-9dfb-ca14f97063ba",
      "name": "Update task in Notion1",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2288,
        304
      ],
      "parameters": {
        "url": "=https://api.notion.com/v1/pages/{{ $('Get Notion page').item.json.id }}",
        "method": "PATCH",
        "options": {},
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "=properties.Notes.url",
              "value": "={{ $('Mapping').item.json.url }}"
            }
          ]
        },
        "nodeCredentialType": "notionApi"
      },
      "credentials": {
        "notionApi": {
          "id": "credential-id",
          "name": "notionApi Credential"
        }
      },
      "typeVersion": 4.2
    }
  ],
  "pinData": {},
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Redis": {
      "main": [
        [
          {
            "node": "Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter": {
      "main": [
        [
          {
            "node": "Get Notion page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Mapping",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mapping": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Redis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Update task in Notion1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Update task in Notion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "Match titiles",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Match titiles": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Notion page": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update task in Notion": {
      "main": [
        [
          {
            "node": "Store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update task in Notion1": {
      "main": [
        [
          {
            "node": "Store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}