{
  "name": "Shopify drop to your content calendar",
  "nodes": [
    {
      "id": "2df88af1-de40-4d35-995a-8ec8ccc423f0",
      "name": "Read me first",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -1390,
        0
      ],
      "parameters": {
        "content": "## Shopify drop to your content calendar\nEach new Shopify product gets one draft row on your content calendar, with a tagged product link.\n\n### You need\n- A Shopify login\n- A Google Sheets login and a Calendar tab with: Content ID, Shopify Product ID, Campaign, Product URL, Title, Caption, Status, Issue\n- A Telegram bot and your chat ID, for the failure alert\n\n### Set it up\n1. Add your Shopify login to **Shopify product created**.\n2. In **Store settings**, add your store address, starting with https.\n3. Pick the same Calendar tab in both Google Sheets steps.\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- Add a test product. One Draft row shows up on the calendar.\n- Edit the product. No second row shows up.",
        "height": 676,
        "width": 560,
        "color": 4
      }
    },
    {
      "name": "Shopify product created",
      "type": "n8n-nodes-base.shopifyTrigger",
      "typeVersion": 1,
      "id": "873c6f5c-8a87-47cd-ab3d-ce0ef242f870",
      "position": [
        -750,
        0
      ],
      "parameters": {
        "authentication": "accessToken",
        "topic": "products/create"
      }
    },
    {
      "name": "Store settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "id": "f644c867-6922-42bc-a0f4-b279402664e7",
      "position": [
        -510,
        0
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e8263b53-3244-469d-967f-2a9a542878df",
              "name": "storefrontUrl",
              "value": "",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      }
    },
    {
      "name": "Prepare campaign draft",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "id": "d7ca8955-44de-4e99-af92-ca8485654453",
      "position": [
        -270,
        0
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const settings = $input.first().json;\nconst product = settings.body && typeof settings.body === 'object' ? settings.body : settings;\nconst url = String(settings.storefrontUrl || '').trim().replace(/\\/+$/, '');\nif (!/^https:\\/\\/[a-z0-9][a-z0-9.-]+$/i.test(url)) {\n  throw new Error('Set the HTTPS storefront URL in Store settings.');\n}\nconst graphId = String(product.admin_graphql_api_id || '').match(/^gid:\\/\\/shopify\\/Product\\/(\\d+)$/)?.[1];\nconst rawId = typeof product.id === 'number' && !Number.isSafeInteger(product.id) ? '' : String(product.id || '');\nconst productId = graphId || rawId;\nconst handle = String(product.handle || '').trim();\nconst title = String(product.title || '').trim();\nif (!/^\\d+$/.test(productId) || !title || !/^[a-z0-9-]+$/i.test(handle)) {\n  throw new Error('The Shopify event is missing a usable product ID, title, or handle.');\n}\n\nconst contentId = 'shopify-' + productId;\nconst campaign = 'sprkly-caldrive-' + contentId;\nconst link = url + '/products/' + encodeURIComponent(handle) + '?utm_campaign=' + encodeURIComponent(campaign);\nconst variants = Array.isArray(product.variants) ? product.variants : [];\nconst hasStock = variants.some(variant => variant.inventory_management && Number(variant.inventory_quantity) > 0);\nconst issues = [];\nif (product.status !== 'active' || !product.published_at) issues.push('Confirm storefront availability');\nif (!product.image?.src && !(Array.isArray(product.images) && product.images.some(image => image.src))) issues.push('Add product media');\nif (!hasStock) issues.push('Verify inventory before promoting');\nreturn [{ json: {\n  'Content ID': contentId,\n  'Shopify Product ID': productId,\n  'Campaign': campaign,\n  'Product URL': link,\n  Title: title,\n  Caption: title + '\\n' + link,\n  Status: 'Draft',\n  Issue: issues.length ? issues.join('; ') : 'Review caption, final clip and publish time',\n} }];"
      }
    },
    {
      "name": "Read existing calendar",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "id": "96cb001a-5328-49b1-bd8d-6f054f538784",
      "position": [
        -30,
        0
      ],
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "url"
        },
        "sheetName": {
          "__rl": true,
          "value": "Calendar",
          "mode": "name"
        },
        "options": {}
      },
      "alwaysOutputData": true
    },
    {
      "name": "Keep only a new launch",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "id": "f0eb70db-cbf7-45ec-b123-b83d8bb7eb7e",
      "position": [
        210,
        0
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const draft = $('Prepare campaign draft').first().json;\nconst known = $input.all().map(item => item.json);\nconst id = draft['Content ID'];\nreturn known.some(row => String(row['Content ID'] || '').trim() === id) ? [] : [{ json: draft }];"
      }
    },
    {
      "name": "Create the draft calendar row",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "id": "db677dfd-958d-4def-8ab5-6e53e1410110",
      "position": [
        450,
        0
      ],
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "url"
        },
        "sheetName": {
          "__rl": true,
          "value": "Calendar",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Content ID": "={{ $json[\"Content ID\"] }}",
            "Shopify Product ID": "={{ $json[\"Shopify Product ID\"] }}",
            "Campaign": "={{ $json[\"Campaign\"] }}",
            "Product URL": "={{ $json[\"Product URL\"] }}",
            "Title": "={{ $json[\"Title\"] }}",
            "Caption": "={{ $json[\"Caption\"] }}",
            "Status": "={{ $json[\"Status\"] }}",
            "Issue": "={{ $json[\"Issue\"] }}"
          },
          "matchingColumns": [
            "Content ID"
          ],
          "schema": [
            {
              "id": "Content ID",
              "displayName": "Content ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Shopify Product ID",
              "displayName": "Shopify Product ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Campaign",
              "displayName": "Campaign",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Product URL",
              "displayName": "Product URL",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Title",
              "displayName": "Title",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Caption",
              "displayName": "Caption",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Issue",
              "displayName": "Issue",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      }
    },
    {
      "id": "6a17bca9-14b6-410a-b6c5-06daf3c5fa66",
      "name": "When this workflow fails",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -1350,
        756
      ],
      "parameters": {}
    },
    {
      "id": "33e1d44e-d314-44d2-b427-2f72ecec10de",
      "name": "Write the failure alert",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -1130,
        756
      ],
      "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": "ef17e2e4-dbdb-4f9d-9d58-f2f81d6a83f2",
      "name": "Send the failure alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -910,
        756
      ],
      "parameters": {
        "chatId": "={{ $json.telegramChatId }}",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    }
  ],
  "connections": {
    "Shopify product created": {
      "main": [
        [
          {
            "node": "Store settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store settings": {
      "main": [
        [
          {
            "node": "Prepare campaign draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare campaign draft": {
      "main": [
        [
          {
            "node": "Read existing calendar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read existing calendar": {
      "main": [
        [
          {
            "node": "Keep only a new launch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Keep only a new launch": {
      "main": [
        [
          {
            "node": "Create the draft calendar row",
            "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"
  },
  "pinData": {}
}
