{
  "name": "Enviar Cota\u00e7\u00e3o ao Cliente",
  "nodes": [
    {
      "parameters": {
        "resource": "spreadsheet",
        "title": "={{$json.quotation_name}} - {{$json.client_name}}",
        "options": {}
      },
      "id": "54f1101f-1856-4a6a-ad87-748d96045e36",
      "name": "Criar Planilha",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        144,
        -112
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "={{$node[\"Criar Planilha\"].json[\"spreadsheetId\"]}}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "P\u00e1gina1",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineAcrossItems",
          "value": {},
          "matchingColumns": [],
          "schema": [
            {
              "id": "spreadsheetId",
              "displayName": "spreadsheetId",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "properties",
              "displayName": "properties",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "sheets",
              "displayName": "sheets",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "spreadsheetUrl",
              "displayName": "spreadsheetUrl",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "6140ae78-72f3-4a4b-a410-33ea7bed4873",
      "name": "Gravar Itens",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        352,
        -112
      ],
      "alwaysOutputData": true,
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "enviar-cotacao",
        "options": {}
      },
      "id": "e4d48e33-7608-4284-9311-c1565c037e27",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        -256,
        -112
      ]
    },
    {
      "parameters": {
        "jsCode": "const body = $node[\"Webhook\"].json[\"body\"];\nconst items = body[\"items\"];\n\nlet totalGeral = 0;\nconst tableRows = items.map(item => {\n  const itemTotal = (item.price || 0) * (item.quantity || 1);\n  totalGeral += itemTotal;\n  return {\n    \"Produto\": item.product,\n    \"Unid\": item.unit,\n    \"Qtd\": item.quantity,\n    \"Pre\u00e7o Unit\": item.price,\n    \"Total\": itemTotal,\n    \"Fornecedor\": item.store,\n    \"Link para Compra\": item.link\n  };\n});\n\nreturn [{\n  json: {\n    client_email: body.client_email,\n    client_name: body.client_name,\n    quotation_name: body.name,\n    internal_code: body.internal_code,\n    items_to_append: tableRows,\n    total_geral: totalGeral\n  }\n}];"
      },
      "id": "933b2bef-22de-4c41-9ae9-20a5c5c762b0",
      "name": "Normalizar Dados",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -48,
        -112
      ]
    },
    {
      "parameters": {
        "operation": "move",
        "fileId": {
          "__rl": true,
          "value": "={{$node[\"Criar Planilha\"].json[\"spreadsheetId\"]}}",
          "mode": "id"
        },
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "1WnooUiTQKIO_SVUsCeP133HRX2F8WIKJ",
          "mode": "id"
        }
      },
      "id": "3cb65dc8-a7cc-4c03-94ca-bd740da9ef19",
      "name": "Mover para Pasta",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        544,
        -112
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "share",
        "fileId": {
          "__rl": true,
          "value": "={{$node[\"Criar Planilha\"].json[\"spreadsheetId\"]}}",
          "mode": "id"
        },
        "permissionsUi": {
          "permissionsValues": {
            "role": "reader",
            "type": "anyone"
          }
        },
        "options": {}
      },
      "id": "3fd8d089-662c-4b1f-b2e3-7b96704012eb",
      "name": "Google Drive Share",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        752,
        -112
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "sendTo": "={{$node[\"Normalizar Dados\"].json[\"client_email\"]}}",
        "subject": "=Sua Cota\u00e7\u00e3o est\u00e1 pronta: {{$node[\"Normalizar Dados\"].json[\"quotation_name\"]}}",
        "message": "=<html> <body style=\"font-family: Arial, sans-serif; line-height: 1.6; color: #333;\">   <div style=\"max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #eee; border-radius: 10px;\">     <h2 style=\"color: #2D3748;\">Ol\u00e1, {{$node[\"Normalizar Dados\"].json[\"client_name\"]}}!</h2>          <p>Informamos que sua cota\u00e7\u00e3o <strong>{{$node[\"Normalizar Dados\"].json[\"internal_code\"]}}</strong> foi finalizada com sucesso.</p>          <p>O arquivo consolidado com os melhores pre\u00e7os e fornecedores encontrados est\u00e1 dispon\u00edvel no Google Drive:</p>          <div style=\"text-align: center; margin: 30px 0;\">       <a href=\"{{ $('Criar Planilha').item.json.spreadsheetUrl }}\"           style=\"display: inline-block; padding: 14px 28px; background-color: #2D3748; color: #ffffff; text-decoration: none; border-radius: 8px; font-weight: bold;\">          Acessar Planilha de Cota\u00e7\u00e3o       </a>     </div>      <div style=\"background-color: #f7fafc; padding: 15px; border-radius: 8px; margin-bottom: 20px;\">       <p style=\"margin: 0;\"><strong>Resumo da Cota\u00e7\u00e3o:</strong></p>       <ul style=\"margin-top: 5px;\">         <li><strong>Cota\u00e7\u00e3o:</strong> {{$node[\"Normalizar Dados\"].json[\"quotation_name\"]}}</li>         <li><strong>Valor Total Estimado:</strong> R$ {{$node[\"Normalizar Dados\"].json[\"total_geral\"].toFixed(2)}}</li>       </ul>     </div>      <p style=\"font-size: 14px; color: #718096;\">Este \u00e9 um e-mail autom\u00e1tico enviado pelo sistema BuscaPrecosWeb.</p>   </div> </body> </html>",
        "options": {}
      },
      "id": "8237bd05-4f91-4300-a0dc-83b2870c6b2d",
      "name": "Gmail",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        944,
        -112
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Criar Planilha": {
      "main": [
        [
          {
            "node": "Gravar Itens",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gravar Itens": {
      "main": [
        [
          {
            "node": "Mover para Pasta",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Normalizar Dados",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalizar Dados": {
      "main": [
        [
          {
            "node": "Criar Planilha",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mover para Pasta": {
      "main": [
        [
          {
            "node": "Google Drive Share",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive Share": {
      "main": [
        [
          {
            "node": "Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "256d6750-5473-433c-a423-f560fac15071",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "lJCDVNy4luo7dtop",
  "tags": []
}