{
  "id": "5FARC6I3Fg8f95vl",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Create personalised calendar views with n8n and Baserow",
  "tags": [],
  "nodes": [
    {
      "id": "8fe5e096-0787-4492-ba12-0106deb69040",
      "name": "Create a token",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        640,
        -320
      ],
      "parameters": {
        "url": "={{ $json[\"API host\"] }}/api/user/token-auth/ ",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"email\": \"{{ $json[\"Baserow user\"] }}\",\n  \"password\": \"{{ $json[\"Baserow password\"] }}\"\n}",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "8242ae21-eeb8-4770-b1a2-56cb8dcda8ae",
      "name": "Share the view",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1984,
        -320
      ],
      "parameters": {
        "url": "={{$('Set Baserow credentials').item.json[\"API host\"]}}/api/database/views/{{ $json.view }}/",
        "method": "PATCH",
        "options": {},
        "jsonBody": "={\n  \"ical_public\": true\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ $('Set table and field ids').item.json['JWT Token'] }}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "6e2389c4-887d-41d1-8fb1-05ec6e66d2b6",
      "name": "Update the url's",
      "type": "n8n-nodes-base.baserow",
      "position": [
        2208,
        -320
      ],
      "parameters": {
        "rowId": "={{ $('Get all records from filter table').item.json.id }}",
        "tableId": "={{ $('Set table and field ids').item.json[\"Filter table\"] }}",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "={{ $('Set table and field ids').item.json[\"Ics field\"] }}",
              "fieldValue": "={{ $json.ical_feed_url }}"
            },
            {
              "fieldId": "={{ $('Set table and field ids').item.json[\"View link field\"] }}",
              "fieldValue": "=https://baserow.cloudron.getbaserow.com/database/1478/table/{{ $('Set table and field ids').item.json['Date table'] }}/{{ $json.id }}"
            }
          ]
        },
        "operation": "update",
        "databaseId": "={{ $('Set table and field ids').item.json[\"Database ID\"] }}"
      },
      "credentials": {
        "baserowApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "553bca65-18b6-4f7c-aed3-825d24dcbc9d",
      "name": "When clicking \u2018Execute workflow\u2019",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        192,
        -320
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "d8eb647c-e37e-4e6b-b610-d885f736a1aa",
      "name": "Set Baserow credentials",
      "type": "n8n-nodes-base.set",
      "position": [
        416,
        -320
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "0f3be469-0daa-42cf-9eef-132796dd65b6",
              "name": "Baserow user",
              "type": "string",
              "value": "<your_username>"
            },
            {
              "id": "7cdacda6-64fa-4de0-9e11-8c353a16b1f3",
              "name": "Baserow password",
              "type": "string",
              "value": "<your_password>"
            },
            {
              "id": "4367d108-f9dc-4790-b081-98026fc960af",
              "name": "API host",
              "type": "string",
              "value": "https://api.baserow.io"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "33022440-77d5-49bd-92ae-697339d38ece",
      "name": "Set table and field ids",
      "type": "n8n-nodes-base.set",
      "position": [
        864,
        -320
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "99f16ae5-a6ad-4c57-a443-b2e820c46995",
              "name": "JWT Token",
              "type": "string",
              "value": "=JWT {{ $json.access_token }}"
            },
            {
              "id": "62b8dd41-ee49-4152-8513-ec2d2ea51c86",
              "name": "Date table",
              "type": "number",
              "value": 671901
            },
            {
              "id": "0f0e697f-7695-44f6-a543-b0db05bfe863",
              "name": "Date field",
              "type": "number",
              "value": 5533823
            },
            {
              "id": "40dcf948-7c16-4354-8e5e-6f2d2c1801ff",
              "name": "Filter field",
              "type": "number",
              "value": 5533824
            },
            {
              "id": "8a999462-a0e1-4c30-bb8e-56f8f76b47c7",
              "name": "Status field",
              "type": "number",
              "value": 5533825
            },
            {
              "id": "cd9461c4-a741-4b6a-bd5a-78e16d8d12d3",
              "name": "Filter table",
              "type": "number",
              "value": "=671897"
            },
            {
              "id": "ebb373eb-b467-40e4-88c0-6bec9ee1e358",
              "name": "Database ID",
              "type": "number",
              "value": 288404
            },
            {
              "id": "e955f70b-7907-41a1-813f-7f5c8a728783",
              "name": "Ics field",
              "type": "string",
              "value": "5533800"
            },
            {
              "id": "a500c9dc-e4c7-4134-8d22-33eb72d6699f",
              "name": "View link field",
              "type": "string",
              "value": "5533805"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "6a904462-086b-4399-bae2-b6bbd44a90da",
      "name": "Get all records from filter table",
      "type": "n8n-nodes-base.baserow",
      "position": [
        1088,
        -320
      ],
      "parameters": {
        "tableId": "={{ $json[\"Filter table\"] }}",
        "databaseId": "={{$json[\"Database ID\"] }}",
        "additionalOptions": {}
      },
      "credentials": {
        "baserowApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ff4e198b-f2c7-45de-b592-9d52592e695e",
      "name": "Create new calendar view",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1312,
        -320
      ],
      "parameters": {
        "url": "={{$('Set Baserow credentials').item.json[\"API host\"]}}/api/database/views/table/{{ $('Set table and field ids').item.json['Date table'] }}/",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n\"name\": \"Calendar for {{ $json.Name }}\",\n\"type\": \"calendar\",\n\"ownership_type\": \"collaborative\",\n\"filter_type\": \"OR\",\n\"date_field\": {{ $('Set table and field ids').item.json['Date field'] }}\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ $('Set table and field ids').item.json['JWT Token'] }}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "4a575169-7bc2-40af-99e0-49526c7485ed",
      "name": "Create filter",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1536,
        -320
      ],
      "parameters": {
        "url": "={{$('Set Baserow credentials').item.json[\"API host\"]}}/api/database/views/{{ $('Create new calendar view').item.json.id }}/filters/",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"field\": {{ $('Set table and field ids').item.json['Filter field'] }},\n  \"type\": \"link_row_has\",\n  \"value\": {{ $('Get all records from filter table').item.json.id }}\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ $('Set table and field ids').item.json['JWT Token'] }}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "aeb2c293-f21d-4b51-959e-830b476a568f",
      "name": "Set background color",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1760,
        -320
      ],
      "parameters": {
        "url": "={{$('Set Baserow credentials').item.json[\"API host\"]}}/api/database/views/{{ $json.view }}/decorations/",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n\"type\": \"background_color\",\n\"value_provider_type\": \"single_select_color\",\n\"value_provider_conf\": {\n\"field_id\": {{ $('Set table and field ids').item.json['Status field']}}\n},\n\"order\": 1\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ $('Set table and field ids').item.json['JWT Token'] }}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "aa0258a7-9f64-454b-bb49-60d145cb92be",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -592,
        -976
      ],
      "parameters": {
        "width": 624,
        "height": 2048,
        "content": "## Create personalised calendar views with n8n and Baserow\n\nThis n8n template demonstrates how to automatically generate **personalized calendar views** in Baserow, based on a chosen date field and a filter. Having a personalized view with only information that is relevant to you makes it easy to integrate with external calendar tools like Outlook or Google Calendar.\n\nUse cases are many:\n\n- Task management (deadlines per staff member)\n- Customer management (appointments per customer)\n- Inventory management (delivery dates per supplier)\n\n## Good to know\n\n- You only need **a Date field** (e.g., a task deadline, due date, appointment date) and **a Link to table field** (e.g., a customer, employee, product) to make this work.\n- The generated calendar views can be shared as `.ics` files and imported into any external calendar application.\n- Authentication is done through a JWT token constructed from your Baserow username and password.\n\n## How it works\n\n- **Set Baserow credentials**:\n    - Allows you to enter your Baserow credentials (username + password) and the API host path. The host is by default https://api.baserow.io, but you can change this in case you are self-hosting.\n    - The information is required to generate a JWT token that authenticates all future HTTP request nodes to create and configure the view.\n- **Create a token**:\n    - Generates a JWT token based on the information provided in the previous node.\n- **Set table and field ids**:\n    - Stores the generated JWT token and allows you to enter the ids of the tables and fields required to run the automation.\n- **Get all records from filter table**\n    - Gets all the records from the table you want to filter on. This is the table that has a Link to table field referencing the table with the Date field. Each record from this table will get it\u2019s own view.\n    - Some examples: Customers, Employees and Products.\n- **Create new calendar view**\n    - Calls the API endpoint `/api/database/views/table/{table_id}` to create a new view. Check the [Baserow API documentation](https://api.baserow.io/api/redoc/#tag/Database-table-views/operation/create_database_table_view) for further details.\n    - The body of this requests configures the new view by setting among other things a name and the date field\n- **Create filter**\n    - Calls the API endpoint `/api/database/views/{view_id}/filters/` to set a filter on the view so that it only shows the records that are relevant. This filter is based on the Link to table field that is set in earlier steps.  Check the [Baserow API documentation](https://api.baserow.io/api/redoc/#tag/Database-table-view-filters/operation/create_database_table_view_filter) for further details.\n- **Set background color**\n    - Calls the API endpoint `/api/database/views/{view_id}/decorations/` to set a a color on the background or left side of each item. By default, the color is based on a single select field, but it is also possible to use a condition.  Check the [Baserow API documentation](https://api.baserow.io/api/redoc/#tag/Database-table-view-decorations/operation/create_database_table_view_decoration) for further details.\n- **Share the view**\n    - Calls the API endpoint `/api/database/views/{view_id}` to update the current view. It updates the `ical_public` property to `true` so that an ics link is created. Check the [Baserow API documentation](https://api.baserow.io/api/redoc/#tag/Database-table-views/operation/update_database_table_view) for further details.\n- **Update the url\u2019s**\n    - Updates all the records in the table you want to filter on to fill in the url to the new generated view and the url to the ics file. This can be useful if you want to build an application on top of your database.\n\n## How to use\n\n- The **Manual Trigger** node is provided as an example, but you can replace it with other triggers such as a webhook\n- The included [Baserow SOP template](https://baserow.io/templates/standard-operating-procedures) works perfectly as a base schema to try out this workflow.\n\n## Requirements\n\n- Baserow account (cloud or self-hosted)\n- A Baserow database with a table that has a Date field and a Link to Table field\n\n## Customising this workflow\n\n- Change the **date field** used to generate the calendars (e.g., deadline \u2192 appointment date).\n- Adjust the **filters** to match your context (staff, customer, product, etc.).\n- Configure which fields are shown using the `/api/database/view/{view_id}/field-options/` endpoint. Check the [Baserow API documentation](https://api.baserow.io/api/redoc/#tag/Database-table-views/operation/update_database_table_view_field_options) for further details.\n- Add or remove optional steps such as coloring by status or sharing the ics feed.\n- Extend the workflow to notify staff when a new view has been created for them."
      },
      "typeVersion": 1
    },
    {
      "id": "73f1b096-a887-4fa3-93a8-02b6c7867719",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        320,
        -512
      ],
      "parameters": {
        "color": 7,
        "width": 336,
        "content": "# Authentication\nreplace <your_username> and <your_password> with your actual Baserow credentials."
      },
      "typeVersion": 1
    },
    {
      "id": "aebc9dfd-f631-4e85-9536-1919da7cc4dc",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        688,
        -624
      ],
      "parameters": {
        "color": 7,
        "width": 592,
        "height": 272,
        "content": "# Required tables and fields\n* **Date table**: id of the table holding the Date field. For example Tasks\n* **Date field**: id of the field that holds the date\n* **Filter field**: id of the Link to table field you want to filter on\n* **Status field**: is optional. Only fill this in if you have a single select field that you want to use as a background or left border color\n* **Filter table**: id of the table the Filter field is referencing to\n* **Database ID**: id of the entire database\n* **Ics field**: optional. Only fill this in if you want to store the url to the ics file\n* **View link field**: is optional. Only fill this in if you want to store the link to the view"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "d30fb5d0-424d-45a5-8bf5-ae78726c833e",
  "connections": {
    "Create filter": {
      "main": [
        [
          {
            "node": "Set background color",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create a token": {
      "main": [
        [
          {
            "node": "Set table and field ids",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Share the view": {
      "main": [
        [
          {
            "node": "Update the url's",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set background color": {
      "main": [
        [
          {
            "node": "Share the view",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Baserow credentials": {
      "main": [
        [
          {
            "node": "Create a token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set table and field ids": {
      "main": [
        [
          {
            "node": "Get all records from filter table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create new calendar view": {
      "main": [
        [
          {
            "node": "Create filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get all records from filter table": {
      "main": [
        [
          {
            "node": "Create new calendar view",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Set Baserow credentials",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}