{
  "name": "Notion L5: Tasks to Calendar",
  "nodes": [
    {
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.4,
      "position": [-760, 40],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 15
            }
          ]
        }
      }
    },
    {
      "name": "Tasks needing an event",
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [-520, -120],
      "parameters": {
        "resource": "databasePage",
        "operation": "getAll",
        "databaseId": {
          "__rl": true,
          "mode": "url",
          "value": "https://www.notion.so/YOUR_NOTION_DATABASE_ID?v=YOUR_VIEW_ID"
        },
        "returnAll": true,
        "filterType": "json",
        "filterJson": "{\n  \"and\": [\n    { \"property\": \"Due Date\", \"date\": { \"is_not_empty\": true } },\n    { \"property\": \"Status\", \"status\": { \"does_not_equal\": \"Done\" } },\n    { \"property\": \"Calendar Event ID\", \"rich_text\": { \"is_empty\": true } }\n  ]\n}",
        "options": {}
      },
      "credentials": {
        "notionApi": {
          "id": "REPLACE_ME",
          "name": "Notion"
        }
      }
    },
    {
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [-280, -120],
      "parameters": {
        "batchSize": 1,
        "options": {}
      }
    },
    {
      "name": "Claim the task",
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [-40, -40],
      "parameters": {
        "resource": "databasePage",
        "operation": "update",
        "pageId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Loop Over Items').item.json.id }}"
        },
        "propertiesUi": {
          "propertyValues": [
            {
              "key": "Calendar Event ID|rich_text",
              "textContent": "=pending:{{ $execution.id }}"
            }
          ]
        },
        "options": {}
      },
      "credentials": {
        "notionApi": {
          "id": "REPLACE_ME",
          "name": "Notion"
        }
      }
    },
    {
      "name": "Create calendar event",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 1.3,
      "position": [200, -40],
      "parameters": {
        "resource": "event",
        "operation": "create",
        "calendar": {
          "__rl": true,
          "mode": "list",
          "value": "REPLACE_WITH_YOUR_CALENDAR_ID",
          "cachedResultName": "REPLACE WITH YOUR CALENDAR"
        },
        "start": "={{ $('Loop Over Items').item.json.properties['Due Date'].date.start }}",
        "end": "={{ DateTime.fromISO($('Loop Over Items').item.json.properties['Due Date'].date.start).plus({ days: 1 }).toISODate() }}",
        "additionalFields": {
          "summary": "={{ $('Loop Over Items').item.json.properties.Name.title[0].plain_text }}",
          "allday": "yes"
        }
      },
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "REPLACE_ME",
          "name": "Google Calendar"
        }
      }
    },
    {
      "name": "Write event ID back",
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [440, -40],
      "parameters": {
        "resource": "databasePage",
        "operation": "update",
        "pageId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Loop Over Items').item.json.id }}"
        },
        "propertiesUi": {
          "propertyValues": [
            {
              "key": "Calendar Event ID|rich_text",
              "textContent": "={{ $json.id }}"
            }
          ]
        },
        "options": {}
      },
      "credentials": {
        "notionApi": {
          "id": "REPLACE_ME",
          "name": "Notion"
        }
      }
    },
    {
      "name": "Completed tasks with an event",
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [-520, 220],
      "parameters": {
        "resource": "databasePage",
        "operation": "getAll",
        "databaseId": {
          "__rl": true,
          "mode": "url",
          "value": "https://www.notion.so/YOUR_NOTION_DATABASE_ID?v=YOUR_VIEW_ID"
        },
        "returnAll": true,
        "filterType": "json",
        "filterJson": "{\n  \"and\": [\n    { \"property\": \"Status\", \"status\": { \"equals\": \"Done\" } },\n    { \"property\": \"Calendar Event ID\", \"rich_text\": { \"is_not_empty\": true } },\n    { \"property\": \"Calendar Event ID\", \"rich_text\": { \"does_not_contain\": \"pending:\" } }\n  ]\n}",
        "options": {}
      },
      "credentials": {
        "notionApi": {
          "id": "REPLACE_ME",
          "name": "Notion"
        }
      }
    },
    {
      "name": "Delete calendar event",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 1.3,
      "onError": "continueRegularOutput",
      "position": [-280, 220],
      "parameters": {
        "resource": "event",
        "operation": "delete",
        "calendar": {
          "__rl": true,
          "mode": "list",
          "value": "REPLACE_WITH_YOUR_CALENDAR_ID",
          "cachedResultName": "REPLACE WITH YOUR CALENDAR"
        },
        "eventId": "={{ $json.properties['Calendar Event ID'].rich_text[0].plain_text }}",
        "options": {}
      },
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "REPLACE_ME",
          "name": "Google Calendar"
        }
      }
    },
    {
      "name": "Clear event ID",
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [-40, 220],
      "parameters": {
        "resource": "databasePage",
        "operation": "update",
        "pageId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Completed tasks with an event').item.json.id }}"
        },
        "propertiesUi": {
          "propertyValues": [
            {
              "key": "Calendar Event ID|rich_text",
              "textContent": ""
            }
          ]
        },
        "options": {}
      },
      "credentials": {
        "notionApi": {
          "id": "REPLACE_ME",
          "name": "Notion"
        }
      }
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Tasks needing an event",
            "type": "main",
            "index": 0
          },
          {
            "node": "Completed tasks with an event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tasks needing an event": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Claim the task",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claim the task": {
      "main": [
        [
          {
            "node": "Create calendar event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create calendar event": {
      "main": [
        [
          {
            "node": "Write event ID back",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write event ID back": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Completed tasks with an event": {
      "main": [
        [
          {
            "node": "Delete calendar event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete calendar event": {
      "main": [
        [
          {
            "node": "Clear event ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {}
}
