{
  "name": "Rodopi Dent - Settings",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "={{$parameter.httpMethod}}",
        "path": "settings-webhook",
        "responseMode": "responseNode",
        "options": {
          "allowedOrigins": "*"
        }
      },
      "id": "webhook-settings",
      "name": "Webhook - Settings",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.method }}",
              "value2": "GET"
            }
          ]
        }
      },
      "id": "if-get",
      "name": "GET or POST?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "1hv4XAfHhScA40Bm1kQ3I-Ih4SJuCBpOJxTOYDNb167g"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Settings"
        },
        "options": {
          "returnAllMatches": true
        }
      },
      "id": "sheets-read-settings",
      "name": "Read Settings",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        440,
        -100
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Convert array of {key, value} to object\nconst settings = $('Read Settings').all();\nconst result = {};\n\nsettings.forEach(item => {\n  const key = item.json.key;\n  let value = item.json.value;\n  \n  // Try to parse JSON values\n  try {\n    value = JSON.parse(value);\n  } catch (e) {\n    // Keep as string if not valid JSON\n  }\n  \n  result[key] = value;\n});\n\nreturn [{ json: { success: true, settings: result } }];"
      },
      "id": "code-format-get",
      "name": "Format Settings",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        -100
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              }
            ]
          }
        }
      },
      "id": "response-get",
      "name": "Respond GET",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        880,
        -100
      ]
    },
    {
      "parameters": {
        "jsCode": "// Handle POST - update settings\nconst body = $('Webhook - Settings').first().json.body;\nconst updates = [];\n\nObject.entries(body).forEach(([key, value]) => {\n  updates.push({\n    key: key,\n    value: typeof value === 'object' ? JSON.stringify(value) : String(value)\n  });\n});\n\nreturn updates.map(u => ({ json: u }));"
      },
      "id": "code-prepare-update",
      "name": "Prepare Updates",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        100
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "1hv4XAfHhScA40Bm1kQ3I-Ih4SJuCBpOJxTOYDNb167g"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Settings"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "value": "={{ $json.value }}"
          }
        },
        "options": {},
        "matchingColumns": [
          "key"
        ]
      },
      "id": "sheets-update-settings",
      "name": "Update Settings",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        660,
        100
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"success\": true,\n  \"message\": \"\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438\u0442\u0435 \u0441\u0430 \u043e\u0431\u043d\u043e\u0432\u0435\u043d\u0438\"\n}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              }
            ]
          }
        }
      },
      "id": "response-post",
      "name": "Respond POST",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        880,
        100
      ]
    }
  ],
  "connections": {
    "Webhook - Settings": {
      "main": [
        [
          {
            "node": "GET or POST?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GET or POST?": {
      "main": [
        [
          {
            "node": "Read Settings",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Prepare Updates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Settings": {
      "main": [
        [
          {
            "node": "Format Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Settings": {
      "main": [
        [
          {
            "node": "Respond GET",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Updates": {
      "main": [
        [
          {
            "node": "Update Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Settings": {
      "main": [
        [
          {
            "node": "Respond POST",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "Rodopi Dent"
    }
  ]
}