{
  "name": "Client inbox desk",
  "nodes": [
    {
      "id": "ede748e4-c6a7-44fd-9fa9-904bab8e7b5d",
      "name": "Read me first",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -710,
        -610
      ],
      "parameters": {
        "content": "## Client inbox desk\nLabel a client email and it becomes a tracked job: files go to Drive, a page to Notion and a card to Trello.\n\n### You need\n- A Gmail label for client mail\n- A private Drive folder, a Notion jobs database and a Trello board\n- Gmail, Google Drive, Notion, Google Sheets and Trello logins\n- A Telegram bot and your chat ID, for the failure alert\n\n### Set it up\n1. Fill **Business settings**: sheet ID, Gmail search, Drive folder ID, Notion data source and title field, Trello board and list IDs.\n2. Add each login where a step asks. Use the same one in every step for that app. Leave the digest address empty to skip email.\n3. Set your time zone in Workflow settings.\n4. Failure alert: put your chat ID in **Write the failure alert** and your bot in **Send the failure alert**. Then in Workflow settings, set Error workflow to this workflow.\n\n### Test it\n- Put your label on one test email. Click Test workflow with **Process one thread now** picked.\n- The job shows up in your sheet, Notion and Trello.",
        "height": 780,
        "width": 560,
        "color": 4
      }
    },
    {
      "id": "c9465b60-8d1e-5243-abfb-ab631d09115d",
      "name": "Intake and private files",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1270,
        -610
      ],
      "parameters": {
        "content": "## Intake and private files\n\nEach poll processes one eligible Gmail thread, prioritizing the oldest pending update. Files are named by message and attachment position, so a replay can reuse existing uploads.",
        "width": 1300,
        "height": 145,
        "color": 7
      }
    },
    {
      "id": "5f3446a1-ce86-5c22-9b58-e4d66a5f0ce4",
      "name": "Link the job in every app",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        2940,
        -610
      ],
      "parameters": {
        "content": "## Link the job in every app\n\nNotion holds the initial brief and a link to the email thread. Trello owns status. Sheets records the latest processed message with the links. Existing jobs and cards are reused.",
        "width": 2920,
        "height": 145,
        "color": 6
      }
    },
    {
      "id": "de720d51-6768-52ae-a840-a41c5b1203a7",
      "name": "Morning status and follow-up",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1500,
        65
      ],
      "parameters": {
        "content": "## Morning status and follow-up\n\nAt 9 AM, refresh each known job from Trello, record first-observed completions and mail the owner the completed, pending, and blocked counts. Missing or archived cards appear as review items.\n\nJob Reports keeps one dated snapshot per day.",
        "width": 2160,
        "height": 155,
        "color": 5
      }
    },
    {
      "id": "8b466d62-0f97-5bd4-992f-cf07c7a559a7",
      "name": "Business settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        180,
        30
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "b6bb6c56-859b-5e4e-b43e-762776201b94",
              "name": "Spreadsheet ID",
              "type": "string",
              "value": ""
            },
            {
              "id": "fa1c311f-6f95-5792-af7b-099c89232378",
              "name": "Gmail search",
              "type": "string",
              "value": "label:client-work newer_than:14d"
            },
            {
              "id": "9a3c34a7-0779-523d-bbb8-487d5a28121c",
              "name": "Drive folder ID",
              "type": "string",
              "value": ""
            },
            {
              "id": "238633e3-d1d5-5dfb-a874-c9b71886bc26",
              "name": "Notion data source ID",
              "type": "string",
              "value": ""
            },
            {
              "id": "4e0ff9af-3c53-5daf-bc96-ec610dacb845",
              "name": "Notion title property",
              "type": "string",
              "value": "Name"
            },
            {
              "id": "92d99fcb-7fcc-59d4-a906-69ad08b05df8",
              "name": "Trello board ID",
              "type": "string",
              "value": ""
            },
            {
              "id": "34701390-b98d-5577-99a0-db9761c5c0d8",
              "name": "Trello inbox list ID",
              "type": "string",
              "value": ""
            },
            {
              "id": "6fe8ba49-570f-547d-83b1-39093dadbfec",
              "name": "Trello done list ID",
              "type": "string",
              "value": ""
            },
            {
              "id": "7eb0b5f9-5763-580d-9bf5-2190b0f48afd",
              "name": "Trello blocked list ID",
              "type": "string",
              "value": ""
            },
            {
              "id": "048b3402-2a17-5d92-9a3c-e23312cc7a2e",
              "name": "Digest recipient",
              "type": "string",
              "value": ""
            }
          ]
        },
        "options": {}
      },
      "executeOnce": true
    },
    {
      "id": "354ff4c4-073c-58b2-9c6c-579658b633ad",
      "name": "Check labelled Gmail every 15 minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -70,
        -150
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 15
            }
          ]
        }
      }
    },
    {
      "id": "3fb3bb9e-716c-5ca0-9783-e0c13ba0f35d",
      "name": "Report at 9 AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -70,
        30
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "daysInterval": 1,
              "triggerAtHour": 9,
              "triggerAtMinute": 0
            }
          ]
        }
      }
    },
    {
      "id": "ab9643a0-9aaf-5ab1-a578-ca9e8fa83c52",
      "name": "Process one thread now",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -70,
        210
      ],
      "parameters": {}
    },
    {
      "id": "d1d7b527-1db0-5175-9a65-5409ededbcc9",
      "name": "Validate connector settings",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        420,
        30
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const c=$input.first().json;\nconst ids=['Spreadsheet ID','Drive folder ID','Notion data source ID','Trello board ID','Trello inbox list ID','Trello done list ID','Trello blocked list ID'];\nfor(const key of ids) if(!/^[A-Za-z0-9_-]+$/.test(c[key].trim())) throw new Error('Set '+key+' in Business settings.');\nif(!/^\\s*label:/i.test(c['Gmail search'])||!c['Notion title property'].trim()) throw new Error('Set a label-first Gmail search and the Notion title property.');\nif(c['Trello done list ID']===c['Trello blocked list ID']) throw new Error('Done and Blocked must be different Trello lists.');\nreturn [{json:c}];"
      }
    },
    {
      "id": "4a8bd0b5-c036-5f59-91ed-a79d6fd24ff8",
      "name": "Ensure Jobs tab",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        650,
        30
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "create",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $(\"Business settings\").first().json[\"Spreadsheet ID\"].trim() }}"
        },
        "options": {},
        "title": "Jobs"
      },
      "alwaysOutputData": true
    },
    {
      "id": "1f6b0057-f4e1-50e5-aa94-78fb32c5b502",
      "name": "Read Jobs ledger",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        880,
        30
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "read",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $(\"Business settings\").first().json[\"Spreadsheet ID\"].trim() }}"
        },
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Jobs"
        }
      },
      "alwaysOutputData": true
    },
    {
      "id": "8d77e7ad-e277-5517-9672-810ff2000f08",
      "name": "Read Trello cards including archived",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1110,
        30
      ],
      "parameters": {
        "url": "={{ 'https://api.trello.com/1/boards/'+$(\"Business settings\").first().json[\"Trello board ID\"].trim()+'/cards/all' }}",
        "method": "GET",
        "options": {
          "timeout": 45000,
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": false
            }
          }
        },
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "trelloApi",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "fields",
              "value": "id,name,idList,closed,shortUrl"
            }
          ]
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000,
      "executeOnce": true
    },
    {
      "id": "ecb9cba6-bf61-5ecf-8c1b-40a5558aa4be",
      "name": "Morning report run",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1350,
        30
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "97db8356-0c97-5435-80f5-97dd57224e80",
              "leftValue": "={{ $('Report at 9 AM').isExecuted }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "01234783-e0ba-5bf0-9045-264918e8a739",
      "name": "Find labelled messages",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1580,
        -255
      ],
      "parameters": {
        "resource": "message",
        "operation": "getAll",
        "returnAll": true,
        "simple": true,
        "filters": {
          "q": "={{ $(\"Business settings\").first().json[\"Gmail search\"] }}"
        },
        "options": {}
      },
      "alwaysOutputData": true
    },
    {
      "id": "034ba33e-1d79-54d6-a3bc-5de1d8db36b0",
      "name": "Select one new thread update",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1820,
        -255
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const ledger=new Map();\nfor (const {json:r} of $('Read Jobs ledger').all()) {\n  const id=String(r['Thread ID']||'');\n  if(!id) continue;\n  if(ledger.has(id)) throw new Error('Duplicate Thread ID in Jobs: '+id);\n  ledger.set(id,r);\n}\nconst current=new Map();\nfor (const {json:m} of $input.all()) {\n  const thread=String(m.threadId||''), id=String(m.id||'');\n  if(!/^[a-z0-9]+$/i.test(thread)||!/^[a-z0-9]+$/i.test(id)) continue;\n  const time=Number(m.internalDate)||Date.parse(m.Date||m.date||'')||0;\n  const previous=current.get(thread);\n  if(!previous||time>previous.time||(time===previous.time&&id>previous.id)) current.set(thread,{thread,id,time,subject:String(m.Subject||m.subject||'Client request')});\n}\nconst pending=[...current.values()].filter(x=>{\n  const known=ledger.get(x.thread);\n  return !known||(known['Latest Message ID']!==x.id&&x.time>=(Number(known['Latest Message Time'])||0));\n}).sort((a,b)=>a.time-b.time||a.id.localeCompare(b.id));\nif(!pending.length) return [];\nconst job=pending[0];\nreturn [{json:{...job,previous:ledger.get(job.thread)||{},marker:'[Gmail '+job.thread+']',threadUrl:'https://mail.google.com/mail/u/0/#all/'+job.thread}}];"
      }
    },
    {
      "id": "1b58f604-3f1c-5e10-a022-326055346dcc",
      "name": "Read message with attachments",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        2060,
        -255
      ],
      "parameters": {
        "resource": "message",
        "operation": "get",
        "messageId": "={{ $json.id }}",
        "simple": false,
        "options": {
          "downloadAttachments": true,
          "dataPropertyAttachmentsPrefixName": "attachment_"
        }
      }
    },
    {
      "id": "cac598f5-d1fb-53cb-ae8f-02b3a4e451d3",
      "name": "Find files already archived",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2300,
        -255
      ],
      "parameters": {
        "url": "https://www.googleapis.com/drive/v3/files",
        "method": "GET",
        "options": {
          "timeout": 45000
        },
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleDriveOAuth2Api",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "={{ \"'\" + $(\"Business settings\").first().json[\"Drive folder ID\"] + \"' in parents and trashed = false and name contains 'gmail-\" + $(\"Select one new thread update\").first().json[\"id\"] + \"-'\"  }}"
            },
            {
              "name": "fields",
              "value": "nextPageToken,files(id,name,webViewLink)"
            },
            {
              "name": "pageSize",
              "value": "100"
            }
          ]
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "id": "cd3e5a07-24b9-5cf6-970e-24b83ec979a5",
      "name": "Find missing attachment files",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2540,
        -255
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const job=$('Select one new thread update').first().json;\nconst files=$input.first().json;\nif(files.nextPageToken) throw new Error('Too many matching files to identify attachment revisions safely.');\nconst byName=new Map();\nfor(const file of (files.files||[])){\n  if(byName.has(file.name)) throw new Error('Duplicate archived attachment name in Drive: '+file.name);\n  byName.set(file.name,file);\n}\nconst attachments=Object.entries($('Read message with attachments').first().binary||{}).filter(([name])=>name.startsWith('attachment_'));\nif(attachments.length>20) throw new Error('This email has more than 20 attachments. Review the thread before retrying.');\nconst reused=[], missing=[];\nfor(const [index,[property,file]] of attachments.entries()){\n  const name='gmail-'+job.id+'-'+String(index+1)+'-'+(file.fileName||'attachment').replace(/[\\u0000-\\u001f]/g,'').slice(0,155);\n  if(byName.has(name)) reused.push(byName.get(name));\n  else missing.push({name,property});\n}\nreturn [{json:{reused,missing}}];"
      }
    },
    {
      "id": "a247fef0-1398-5a42-b75e-53bf77739ad7",
      "name": "New attachments to archive",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2780,
        -255
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "e9641ca0-9679-576c-9774-eb73d843843d",
              "leftValue": "={{ $json.missing.length > 0 }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "d103219c-ca65-578f-9670-bb92d5ffdb45",
      "name": "Prepare private file uploads",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        3020,
        -325
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const binary=$('Read message with attachments').first().binary;\nreturn $input.first().json.missing.map(file=>({json:{name:file.name},binary:{data:binary[file.property]}}));"
      }
    },
    {
      "id": "b339bc44-f5e4-5751-bf02-a312969101c1",
      "name": "Upload new attachments to Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        3260,
        -325
      ],
      "parameters": {
        "resource": "file",
        "operation": "upload",
        "inputDataFieldName": "data",
        "name": "={{ $json.name }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $(\"Business settings\").first().json[\"Drive folder ID\"] }}"
        },
        "options": {}
      }
    },
    {
      "id": "f382f0a2-abf0-5c4a-bb89-6e1aec0fa92e",
      "name": "Assemble the job and file links",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        3500,
        -255
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const base=$('Select one new thread update').first().json;\nconst found=$('Find missing attachment files').first().json;\nconst fresh=$('Upload new attachments to Drive').isExecuted?$('Upload new attachments to Drive').all().map(({json})=>json):[];\nif(fresh.length!==found.missing.length || fresh.some(f=>!f.id)) throw new Error('Drive did not confirm every new attachment.');\nconst archived=[...found.reused,...fresh].map(file=>file.webViewLink||'https://drive.google.com/file/d/'+file.id+'/view');\nreturn [{json:{...base, archived,title:(base.marker+' '+base.subject).slice(0,180)}}];"
      },
      "executeOnce": true
    },
    {
      "id": "234a35c1-d95d-556d-b8b3-ba0df0e1eb0f",
      "name": "Find the Notion job by Gmail thread",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        3740,
        -255
      ],
      "parameters": {
        "url": "={{ 'https://api.notion.com/v1/data_sources/'+$(\"Business settings\").first().json[\"Notion data source ID\"].trim()+'/query' }}",
        "method": "POST",
        "options": {
          "timeout": 45000
        },
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "notionApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ {page_size:2,filter:{property:$(\"Business settings\").first().json[\"Notion title property\"],title:{starts_with:$json.marker}}} }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Notion-Version",
              "value": "2025-09-03"
            }
          ]
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "id": "d38fd152-aa42-5c87-b821-d282a56ec28e",
      "name": "Reuse or prepare the Notion job",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        3980,
        -255
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const response=$input.first().json;\nconst job=$('Assemble the job and file links').first().json;\nif(!Array.isArray(response.results)||response.results.length>1||response.has_more) throw new Error('Resolve duplicate or unreadable Notion jobs for '+job.marker);\nconst existing=response.results[0] || (job.previous['Notion ID'] && job.previous['Notion URL'] ? {id:job.previous['Notion ID'],url:job.previous['Notion URL']} : null);\nconst setup=$('Business settings').first().json;\nconst paragraphs=['Read the client request: '+job.threadUrl,...job.archived.map(link=>'Intake attachment: '+link)];\nconst notionBody={parent:{type:'data_source_id',data_source_id:setup['Notion data source ID']},\n  properties:{[setup['Notion title property']]:{title:[{text:{content:job.title}}]}},\n  children:paragraphs.map(content=>({object:'block',type:'paragraph',paragraph:{rich_text:[{type:'text',text:{content}}]}}))};\nreturn [{json:{...job,notionUrl:existing?.url||'',notionId:existing?.id||'',notionBody}}];"
      }
    },
    {
      "id": "8e8456e7-770c-574d-a549-68b71182207b",
      "name": "Notion job is new",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        4220,
        -255
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "1a604a7b-6155-57dc-8233-4ba5c07c80fa",
              "leftValue": "={{ !$json.notionId }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "d9123ee1-9644-59ce-8474-dc59f58338f9",
      "name": "Create the Notion job",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        4460,
        -325
      ],
      "parameters": {
        "url": "https://api.notion.com/v1/pages",
        "method": "POST",
        "options": {
          "timeout": 45000
        },
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "notionApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json.notionBody }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Notion-Version",
              "value": "2025-09-03"
            }
          ]
        }
      }
    },
    {
      "id": "ae6488b7-0b28-593d-9817-9a504d4097ee",
      "name": "Find an existing Trello work card",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        4700,
        -255
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const job=$('Reuse or prepare the Notion job').first().json;\nconst page=$('Create the Notion job').isExecuted?$('Create the Notion job').first().json:{id:job.notionId,url:job.notionUrl};\nif(!page.id || !page.url) throw new Error('The Notion job could not be identified.');\nconst board=$('Read Trello cards including archived').first().json;\nif(!Array.isArray(board.body)) throw new Error('Trello card list did not return a usable result.');\nconst matching=board.body.filter(card=>String(card.name||'').startsWith(job.marker)||card.id===String(job.previous['Trello Card ID']||''));\nif(matching.length>1) throw new Error('Multiple Trello cards match '+job.marker);\nconst card=matching[0];\nconst description=['Client email '+job.threadUrl,'Job brief '+page.url,...job.archived].join('\\n');\nreturn [{json:{...job,page,card,description}}];"
      }
    },
    {
      "id": "e512d518-e56c-5a8e-8fc2-9d5cd17abde9",
      "name": "Trello work card is new",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        4940,
        -255
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "77a445b0-ab99-51fc-bc80-accdeb10333b",
              "leftValue": "={{ !$json.card }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "fa6b5ec2-5886-56d5-b5f4-b63719d65a0e",
      "name": "Create the tracked Trello card",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        5180,
        -325
      ],
      "parameters": {
        "url": "https://api.trello.com/1/cards",
        "method": "POST",
        "options": {
          "timeout": 45000
        },
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "trelloApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ {idList:$(\"Business settings\").first().json[\"Trello inbox list ID\"],name:$json.title,desc:$json.description} }}"
      }
    },
    {
      "id": "90ef0848-8021-50c1-a65d-2ebedf08aed5",
      "name": "Record the linked job in Sheets",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        5420,
        -255
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const x=$('Find an existing Trello work card').first().json;\nconst c=$('Create the tracked Trello card').isExecuted?$('Create the tracked Trello card').first().json:x.card;\nif(!c?.id) throw new Error('Trello did not confirm the card.');\nreturn [{json:{'Thread ID':x.thread,'Latest Message ID':x.id,'Latest Message Time':x.time,\n  Subject:x.subject,'Gmail URL':x.threadUrl,'Notion ID':x.page.id,'Notion URL':x.page.url,'Trello Card ID':c.id,'Trello URL':c.shortUrl||c.url||'',\n  'Latest Attachments':x.archived.join('\\n'),'Status':x.previous.Status||'Pending','Completed At':x.previous['Completed At']||'',\n  'Updated At':$now.toISO()}}];"
      }
    },
    {
      "id": "588db0ca-b0c9-5934-a63a-57eb8c31cc97",
      "name": "Upsert job by Gmail thread",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        5660,
        -255
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $(\"Business settings\").first().json[\"Spreadsheet ID\"].trim() }}"
        },
        "options": {
          "cellFormat": "RAW",
          "handlingExtraData": "insertInNewColumn",
          "useAppend": true
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Jobs"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [
            "Thread ID"
          ],
          "schema": []
        }
      }
    },
    {
      "id": "d38ad0f6-293a-5ada-af74-908fceabaf27",
      "name": "Reconcile board status and prepare digest",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1600,
        280
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const ctx=$('Business settings').first().json;\nconst cards=$('Read Trello cards including archived').first().json.body;\nif(!Array.isArray(cards)) throw new Error('Trello board data is unavailable; no job status was overwritten.');\nconst index=new Map(cards.map(x=>[String(x.id),x]));\nconst updates=[], open=[], blocked=[], completed=[], review=[];\nconst start=$now.startOf('day').minus({days:6}).toMillis();\nfor(const {json:r} of $('Read Jobs ledger').all()){\n  if(!r['Thread ID']) continue;\n  const card=index.get(String(r['Trello Card ID']));\n  const label=String(r.Subject||r['Thread ID']);\n  if(!card||card.closed){review.push(label+': card unavailable or archived');continue;}\n  const status=card.idList===ctx['Trello done list ID']?'Completed':card.idList===ctx['Trello blocked list ID']?'Blocked':'Pending';\n  const completion=status==='Completed'?(r.Status==='Completed'&&r['Completed At']?r['Completed At']:$now.toISO()):'';\n  if(status!==r.Status||completion!==(r['Completed At']||'')) updates.push({json:{'Thread ID':r['Thread ID'],Status:status,'Completed At':completion}});\n  if(status==='Pending') open.push(label);\n  if(status==='Blocked') blocked.push(label);\n  if(status==='Completed'&&Date.parse(completion)>=start) completed.push(label);\n}\nconst section=(label,rows)=>label+' ('+rows.length+')\\n'+(rows.slice(0,15).join('\\n')||'None')+(rows.length>15?'\\nMore in Jobs.':'');\nconst message=[section('Completed in the last seven days',completed),section('Pending now',open),\n  section('Blocked now',blocked),section('Needs board review',review),\n  'https://docs.google.com/spreadsheets/d/'+encodeURIComponent(ctx['Spreadsheet ID'])].join('\\n\\n');\nreturn [{json:{updates,message, counts:{completed:completed.length,pending:open.length,blocked:blocked.length,review:review.length}}}];"
      }
    },
    {
      "id": "d76eb34d-5ce6-58ac-bbca-54e594dea3c3",
      "name": "Job status changed",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1840,
        280
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "13d91e26-19be-5360-976b-998eb5204170",
              "leftValue": "={{ $json.updates.length > 0 }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "0af11ea0-2840-5e55-b876-3d7345489bba",
      "name": "Expand changed job rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2080,
        235
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return $input.first().json.updates;"
      }
    },
    {
      "id": "e639aa9f-4d8c-56d1-9e6f-55ca9f5c0a6c",
      "name": "Update job status in Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        2320,
        235
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $(\"Business settings\").first().json[\"Spreadsheet ID\"].trim() }}"
        },
        "options": {
          "cellFormat": "RAW",
          "handlingExtraData": "insertInNewColumn",
          "useAppend": true
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Jobs"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [
            "Thread ID"
          ],
          "schema": []
        }
      }
    },
    {
      "id": "ef1c237c-5068-5cb7-8833-42ad92593ef8",
      "name": "Owner wants a morning email",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        3270,
        280
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "0a1a09e7-7fac-5729-8e0a-cd97a9ae9c95",
              "leftValue": "={{ $(\"Business settings\").first().json[\"Digest recipient\"].trim().length > 0 }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "executeOnce": true
    },
    {
      "id": "079d4aaf-fd38-5f99-b73d-83cccb7e8ceb",
      "name": "Email the morning job report",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        3505,
        240
      ],
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "={{ $(\"Business settings\").first().json[\"Digest recipient\"] }}",
        "subject": "={{ 'Client jobs · '+$now.toISODate() }}",
        "emailType": "text",
        "message": "={{ $(\"Reconcile board status and prepare digest\").first().json.message }}",
        "options": {
          "appendAttribution": false
        }
      }
    },
    {
      "id": "3936a1ed-3990-5957-af73-edf809d134dc",
      "name": "Ensure Job Reports tab",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        2550,
        280
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "create",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $(\"Business settings\").first().json[\"Spreadsheet ID\"].trim() }}"
        },
        "options": {},
        "title": "Job Reports"
      },
      "alwaysOutputData": true,
      "executeOnce": true
    },
    {
      "id": "d5b9ae5d-a80d-5dbb-99ec-dfc27cac809c",
      "name": "Prepare the daily report row",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2790,
        280
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const report=$('Reconcile board status and prepare digest').first().json;\nreturn [{json:{'Report Date':$now.toISODate(),'Checked At':$now.toISO(),\n 'Completed Last 7 Days':report.counts.completed,Pending:report.counts.pending,\n Blocked:report.counts.blocked,'Needs Board Review':report.counts.review,Report:report.message}}];"
      }
    },
    {
      "id": "8d311a95-8039-5a69-bfd3-8ca91e48c284",
      "name": "Save daily snapshot by date",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        3030,
        280
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $(\"Business settings\").first().json[\"Spreadsheet ID\"].trim() }}"
        },
        "options": {
          "cellFormat": "RAW",
          "handlingExtraData": "insertInNewColumn",
          "useAppend": true
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Job Reports"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [
            "Report Date"
          ],
          "schema": []
        }
      }
    },
    {
      "id": "825a304b-60a9-423b-b09c-87a3f29ae5d5",
      "name": "When this workflow fails",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -670,
        250
      ],
      "parameters": {}
    },
    {
      "id": "28f0d200-bfcc-4a70-970e-d7850b807790",
      "name": "Write the failure alert",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -450,
        250
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "f1",
              "name": "telegramChatId",
              "type": "string",
              "value": "YOUR_TELEGRAM_CHAT_ID"
            },
            {
              "id": "f2",
              "name": "message",
              "type": "string",
              "value": "={{ 'A workflow stopped.\\n\\nWorkflow: ' + (($json.workflow || {}).name || 'unknown') + '\\nFailed step: ' + (($json.execution || {}).lastNodeExecuted || 'unknown') + '\\nWhy: ' + String((($json.execution || {}).error || {}).message || 'n8n did not give a reason.').slice(0, 300) + (($json.execution || {}).url ? '\\nOpen the run: ' + $json.execution.url : '') }}"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      }
    },
    {
      "id": "063087b6-e346-434a-9287-77c9c19fdc51",
      "name": "Send the failure alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -230,
        250
      ],
      "parameters": {
        "chatId": "={{ $json.telegramChatId }}",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    }
  ],
  "connections": {
    "Check labelled Gmail every 15 minutes": {
      "main": [
        [
          {
            "node": "Business settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Report at 9 AM": {
      "main": [
        [
          {
            "node": "Business settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process one thread now": {
      "main": [
        [
          {
            "node": "Business settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Business settings": {
      "main": [
        [
          {
            "node": "Validate connector settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate connector settings": {
      "main": [
        [
          {
            "node": "Ensure Jobs tab",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ensure Jobs tab": {
      "main": [
        [
          {
            "node": "Read Jobs ledger",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Jobs ledger": {
      "main": [
        [
          {
            "node": "Read Trello cards including archived",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Trello cards including archived": {
      "main": [
        [
          {
            "node": "Morning report run",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Morning report run": {
      "main": [
        [
          {
            "node": "Reconcile board status and prepare digest",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Find labelled messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find labelled messages": {
      "main": [
        [
          {
            "node": "Select one new thread update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Select one new thread update": {
      "main": [
        [
          {
            "node": "Read message with attachments",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read message with attachments": {
      "main": [
        [
          {
            "node": "Find files already archived",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find files already archived": {
      "main": [
        [
          {
            "node": "Find missing attachment files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find missing attachment files": {
      "main": [
        [
          {
            "node": "New attachments to archive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New attachments to archive": {
      "main": [
        [
          {
            "node": "Prepare private file uploads",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Assemble the job and file links",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare private file uploads": {
      "main": [
        [
          {
            "node": "Upload new attachments to Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload new attachments to Drive": {
      "main": [
        [
          {
            "node": "Assemble the job and file links",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assemble the job and file links": {
      "main": [
        [
          {
            "node": "Find the Notion job by Gmail thread",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find the Notion job by Gmail thread": {
      "main": [
        [
          {
            "node": "Reuse or prepare the Notion job",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reuse or prepare the Notion job": {
      "main": [
        [
          {
            "node": "Notion job is new",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Notion job is new": {
      "main": [
        [
          {
            "node": "Create the Notion job",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Find an existing Trello work card",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create the Notion job": {
      "main": [
        [
          {
            "node": "Find an existing Trello work card",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find an existing Trello work card": {
      "main": [
        [
          {
            "node": "Trello work card is new",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trello work card is new": {
      "main": [
        [
          {
            "node": "Create the tracked Trello card",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Record the linked job in Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create the tracked Trello card": {
      "main": [
        [
          {
            "node": "Record the linked job in Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Record the linked job in Sheets": {
      "main": [
        [
          {
            "node": "Upsert job by Gmail thread",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reconcile board status and prepare digest": {
      "main": [
        [
          {
            "node": "Job status changed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Job status changed": {
      "main": [
        [
          {
            "node": "Expand changed job rows",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Ensure Job Reports tab",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Expand changed job rows": {
      "main": [
        [
          {
            "node": "Update job status in Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update job status in Sheets": {
      "main": [
        [
          {
            "node": "Ensure Job Reports tab",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Owner wants a morning email": {
      "main": [
        [
          {
            "node": "Email the morning job report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ensure Job Reports tab": {
      "main": [
        [
          {
            "node": "Prepare the daily report row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare the daily report row": {
      "main": [
        [
          {
            "node": "Save daily snapshot by date",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save daily snapshot by date": {
      "main": [
        [
          {
            "node": "Owner wants a morning email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When this workflow fails": {
      "main": [
        [
          {
            "node": "Write the failure alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write the failure alert": {
      "main": [
        [
          {
            "node": "Send the failure alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveDataSuccessExecution": "none",
    "saveManualExecutions": false
  },
  "pinData": {}
}
