{
  "name": "pingwa — staff check-in to Google Sheets",
  "nodes": [
    {
      "parameters": {
        "content": "## pingwa staff check-in → Google Sheets\n\nEach report a staff member taps in WhatsApp (Start shift, End shift, Job done) becomes one row in your Sheet.\n\n1. **pingwa report** — a Webhook trigger with *Raw Body* on. Use its Production URL as the actions' `target_url` (no `sign_query` needed: n8n reads headers).\n2. **Check the signature** — reads `PINGWA_SECRET_START`, `PINGWA_SECRET_END`, `PINGWA_SECRET_DONE` from n8n's environment, each `<action id>:<secret>` (never pasted into the workflow). A wrong signature or an old request gives no row. Self-hosted n8n: set `NODE_FUNCTION_ALLOW_BUILTIN=crypto` (and `N8N_BLOCK_ENV_ACCESS_IN_NODE=false` if your n8n blocks it).\n3. **Add the row** — pick your Sheet (cells are written as plain text, never a formula); columns Time, Name, Action, Run, Answer 1-3 (add them as the first row).\n\nGuide: https://pingwa.dev/check-in",
        "height": 420,
        "width": 420,
        "color": 4
      },
      "id": "c1a0f0de-0001-4c00-9000-00000000a001",
      "name": "About this workflow",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -560,
        -220
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "pingwa-check-in",
        "responseMode": "onReceived",
        "options": {
          "rawBody": true
        }
      },
      "id": "c1a0f0de-0002-4c00-9000-00000000a002",
      "name": "pingwa report",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -80,
        0
      ],
      "webhookId": "c1a0f0de-0002-4c00-9000-00000000b002"
    },
    {
      "parameters": {
        "jsCode": "// pingwa staff check-in: verify the signature, then one row per report.\n// No secret is pasted here: set PINGWA_SECRET_START, PINGWA_SECRET_END and\n// PINGWA_SECRET_DONE in n8n's environment, each <action id>:<secret>\n// (the secret is shown once, when the action is created).\nconst crypto = require('crypto');\nconst NAMES = ['PINGWA_SECRET_START', 'PINGWA_SECRET_END', 'PINGWA_SECRET_DONE'];\nconst SECRETS = {};\nfor (const name of NAMES) {\n  let line = '';\n  try { line = String($env[name] || ''); } catch (e) { line = ''; }  // env access blocked\n  const colon = line.indexOf(':');\n  if (colon > 0) SECRETS[line.slice(0, colon).trim()] = line.slice(colon + 1).trim();\n}\nif (!Object.keys(SECRETS).length) {\n  throw new Error('No PINGWA_SECRET_* environment variable found: see https://pingwa.dev/check-in');\n}\nconst MAX_AGE_MS = 6 * 60 * 60 * 1000;\n\nconst out = [];\nfor (let i = 0; i < $input.all().length; i++) {\n  const item = $input.all()[i];\n  const raw = await this.helpers.getBinaryDataBuffer(i, 'data');  // the exact bytes\n  const event = JSON.parse(raw.toString('utf8'));\n  const secret = SECRETS[event.action_id];\n  const given = String((item.json.headers || {})['x-pingwa-signature'] || '');\n  const want = secret\n    ? 'sha256=' + crypto.createHmac('sha256', secret).update(raw).digest('hex') : '';\n  if (!secret || given.length !== want.length\n      || !crypto.timingSafeEqual(Buffer.from(given), Buffer.from(want))) {\n    continue;  // not from pingwa, or a changed body: no row\n  }\n  if (!(Date.parse(event.created_at) > Date.now() - MAX_AGE_MS)) {\n    continue;  // an old request sent again\n  }\n  const row = {\n    'Time': event.created_at,\n    'Name': (event.actor || {}).name || '',\n    'Action': event.label || '',\n    'Run': event.event_id,\n  };\n  (event.answers || []).forEach((a, n) => {\n    const v = a.value;\n    row['Answer ' + (n + 1)] = v === null || v === undefined ? ''\n      : a.type === 'photo' ? v.url  // works for 5 minutes: download it in a next node\n      : a.type === 'location' ? v.lat + ',' + v.lng : String(v);\n  });\n  out.push({ json: row });\n}\nreturn out;\n"
      },
      "id": "c1a0f0de-0003-4c00-9000-00000000a003",
      "name": "Check the signature",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        160,
        0
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "url"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": []
        },
        "options": {
          "cellFormat": "RAW"
        }
      },
      "id": "c1a0f0de-0004-4c00-9000-00000000a004",
      "name": "Add the row",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        400,
        0
      ]
    }
  ],
  "connections": {
    "pingwa report": {
      "main": [
        [
          {
            "node": "Check the signature",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check the signature": {
      "main": [
        [
          {
            "node": "Add the row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  }
}
