{
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -368,
        1024
      ],
      "id": "842cce6b-e772-4cf8-adfa-dcec6cbf6b01",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 10
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -368,
        832
      ],
      "id": "44c69bdc-0f02-48a9-9155-d983748cc5b0",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "url": "={{ $json.threads_page }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "sort",
              "value": "DESC"
            },
            {
              "name": "limit",
              "value": "10"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        80,
        976
      ],
      "id": "3992dccb-7d13-411d-bfd6-88f8b992d9e2",
      "name": "Get Threads",
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "url": "=https://api.addons.prestashop.com/request/seller/threads/{{ $json.id_community_thread }}/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "id_thread",
              "value": "={{ $json.id_community_thread }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        752,
        832
      ],
      "id": "a9068101-3042-4b6b-85a0-90028c7371d6",
      "name": "Get Messages",
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "threads.data",
        "options": {}
      },
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        304,
        832
      ],
      "id": "a24cea7c-f63b-49b3-9840-8356fd623c1e",
      "name": "Split Threads"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "d313ad6e-0c1b-483b-a389-ff7563d7184e",
              "leftValue": "={{ $json.id_product }}",
              "rightValue": 19389,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.2,
      "position": [
        528,
        832
      ],
      "id": "c5528141-b302-44c1-9142-184d2755ac78",
      "name": "DP Only"
    },
    {
      "parameters": {
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "value": "jOqXP2wTnt7XyZy3",
          "mode": "list",
          "cachedResultName": "Support Scraper",
          "cachedResultUrl": "/projects/ONnV6e2KrwWbdnuZ/datatables/jOqXP2wTnt7XyZy3"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyValue": "1"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        -144,
        976
      ],
      "id": "86e77bb8-a00d-41c2-8323-180bd97f5e3d",
      "name": "Get row(s)"
    },
    {
      "parameters": {
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "value": "jOqXP2wTnt7XyZy3",
          "mode": "list",
          "cachedResultName": "Support Scraper",
          "cachedResultUrl": "/projects/ONnV6e2KrwWbdnuZ/datatables/jOqXP2wTnt7XyZy3"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyValue": "1"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "threads_page": "={{ $json.threads.next_page_url }}"
          },
          "matchingColumns": [
            "threads_page"
          ],
          "schema": [
            {
              "id": "threads_page",
              "displayName": "threads_page",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        304,
        1024
      ],
      "id": "bcc8c38b-ee25-45f4-a72d-708b7c51c726",
      "name": "Update row(s)"
    },
    {
      "parameters": {
        "jsCode": "let threads = []\n\nfor(const item of $input.all()){\n  console.log(item)\n  let id_thread = 0\n  let content = \"\";\n  for (const message of item.json.messages.data.reverse()) {\n    id_thread = message.id_community_thread;\n    // console.log({id_thread})\n    content += `<p><strong>${message.from}</strong>: ${message.message}</p>`\n  }\n  threads.push({id_thread, content})\n}\n\nreturn {threads};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        976,
        832
      ],
      "id": "3b3d59c1-084d-42f0-9596-1da7c4a6261f",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "operation": "createFromText",
        "content": "={{ $('Markdown').item.json.content }}",
        "name": "=thread-{{ $('Loop Over Messages').item.json.id_thread }}.md",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "1OeK7pont2_2PTgjYudSjj68UNAsgLdPt",
          "mode": "list",
          "cachedResultName": "scraper",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1OeK7pont2_2PTgjYudSjj68UNAsgLdPt"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        2320,
        736
      ],
      "id": "ffb7fe9e-c71c-492e-a79b-c40fa40051fe",
      "name": "Create file from text",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        1424,
        832
      ],
      "id": "51c33eae-8b58-40a0-b2e6-24321fa9425c",
      "name": "Loop Over Messages"
    },
    {
      "parameters": {
        "fieldToSplitOut": "threads",
        "options": {}
      },
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        1200,
        832
      ],
      "id": "cb320200-2be5-4f2d-bb9b-5fb9a7ecf7e2",
      "name": "Split Out"
    },
    {
      "parameters": {
        "html": "={{ $json.content }}",
        "destinationKey": "content",
        "options": {}
      },
      "type": "n8n-nodes-base.markdown",
      "typeVersion": 1,
      "position": [
        1648,
        592
      ],
      "id": "b2ede0ea-bc10-4c2d-b758-34a2b4ea7c1d",
      "name": "Markdown"
    },
    {
      "parameters": {
        "operation": "update",
        "fileId": {
          "__rl": true,
          "value": "={{ $('Search files and folders').item.json.id }}",
          "mode": "id"
        },
        "changeFileContent": true,
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        2768,
        664
      ],
      "id": "4e9116cd-0294-41de-a9a2-095423a6f19f",
      "name": "Update file",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "searchMethod": "query",
        "queryString": "=name = 'thread-{{ $json.id_thread }}.md' and trashed=false",
        "returnAll": true,
        "filter": {},
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1872,
        592
      ],
      "id": "28edc7f8-41fb-4b76-be5b-91605dced98c",
      "name": "Search files and folders",
      "alwaysOutputData": true,
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "toText",
        "sourceProperty": "content",
        "options": {}
      },
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        2544,
        544
      ],
      "id": "b82bd628-7bf8-4f87-8b03-b23f05ef470d",
      "name": "Convert to File"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "19eba4af-2bc6-4204-8331-62f1c7206240",
              "name": "content",
              "value": "={{ $('Markdown').item.json.content }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2320,
        544
      ],
      "id": "caf08342-1510-4d2c-b925-0531a4ea7c27",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.id }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    },
                    "id": "b746120d-c022-414c-90b7-33c0670a9e0e"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "File exists"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "e171352f-d9f8-459d-b309-5807344e9207",
                    "leftValue": "={{ $json.id }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "notExists",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "File missing"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.3,
      "position": [
        2096,
        592
      ],
      "id": "4b40ac38-948c-4ddf-b49c-9260cbbfc82f",
      "name": "Switch"
    }
  ],
  "connections": {
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Get row(s)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get row(s)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Threads": {
      "main": [
        [
          {
            "node": "Update row(s)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split Threads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Messages": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Threads": {
      "main": [
        [
          {
            "node": "DP Only",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "DP Only": {
      "main": [
        [
          {
            "node": "Get Messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get row(s)": {
      "main": [
        [
          {
            "node": "Get Threads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update row(s)": {
      "main": [
        []
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create file from text": {
      "main": [
        [
          {
            "node": "Loop Over Messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Messages": {
      "main": [
        [],
        [
          {
            "node": "Markdown",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Loop Over Messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Markdown": {
      "main": [
        [
          {
            "node": "Search files and folders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update file": {
      "main": [
        [
          {
            "node": "Loop Over Messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search files and folders": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to File": {
      "main": [
        [
          {
            "node": "Update file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Convert to File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create file from text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": true
  }
}