diff --git a/docker-compose.yml b/docker-compose.yml index 1dd5cbd..191b7a8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -118,19 +118,29 @@ services: ports: - "5678:5678" environment: + # Owner account (skips /setup) + N8N_USER_MANAGEMENT_DISABLED: "false" + N8N_OWNER_EMAIL: ${N8N_EMAIL:-admin@farmafinder.com} + N8N_OWNER_PASSWORD: ${N8N_PASSWORD:-change-me} + # Auth N8N_BASIC_AUTH_ACTIVE: "true" N8N_BASIC_AUTH_USER: ${N8N_USER:-admin} N8N_BASIC_AUTH_PASSWORD: ${N8N_PASSWORD:-change-me} + # Database DB_TYPE: postgresdb DB_POSTGRESDB_HOST: postgres DB_POSTGRESDB_DATABASE: farmafinder DB_POSTGRESDB_USER: farmafinder DB_POSTGRESDB_PASSWORD: ${PG_PASSWORD:-change-me-in-production} + # Network N8N_HOST: localhost N8N_PORT: 5678 N8N_PROTOCOL: http + # Webhook URL for external calls + WEBHOOK_URL: http://localhost:5678/ volumes: - n8n_data:/home/node/.n8n + - ./n8n/workflows:/home/node/workflows depends_on: - postgres diff --git a/n8n/README.md b/n8n/README.md new file mode 100644 index 0000000..743de0d --- /dev/null +++ b/n8n/README.md @@ -0,0 +1,89 @@ +# N8N Workflows for FarmaFinder Parapharmacy + +## Configuración Inicial + +Al iniciar N8N por primera vez, se creará automáticamente una cuenta de administrador: + +- **Email**: admin@farmafinder.com (configurable en `.env`) +- **Password**: change-me (configurable en `.env`) + +### Variables de Entorno + +```bash +# En el archivo .env de la raíz del proyecto +N8N_USER=admin +N8N_PASSWORD=change-me +N8N_EMAIL=admin@farmafinder.com +``` + +## Workflows Incluidos + +### 1. Parapharmacy Scraper (Automático) + +- **Trigger**: Cada 3 días a las 2:00 AM +- **Función**: Scraping automático de Promofarma +- **Estado**: Inactivo por defecto (activar después de configurar) + +### 2. Parapharmacy Manual Scraper (Webhook) + +- **Trigger**: POST a `/webhook/scrape-parapharmacy` +- **Función**: Scraping bajo demanda +- **Estado**: Activo por defecto + +## Uso del Webhook Manual + +```bash +# Ejecutar scraping con queries por defecto +curl -X POST http://localhost:5678/webhook/scrape-parapharmacy + +# Ejecutar scraping con queries específicas +curl -X POST http://localhost:5678/webhook/scrape-parapharmacy \ + -H "Content-Type: application/json" \ + -d '{"queries": ["crema hidratante", "protector solar"]}' +``` + +## Endpoints de la API de Parafarmacia + +Los workflows envían los productos scrapeados a: + +``` +POST http://parapharmacy-api:3002/api/products/bulk +``` + +## Monitoreo + +- **N8N Dashboard**: http://localhost:5678 +- **Historial de ejecuciones**: http://localhost:5678/executions +- **Logs**: `docker logs n8n` + +## Adding More Sources + +Para añadir nuevas fuentes de scraping: + +1. Crear un nuevo nodo HTTP Request en el workflow +2. Añadir un nodo Code para extraer los productos +3. Conectar al nodo "Send to Parapharmacy API" +4. Activar el workflow + +## Troubleshooting + +### N8N muestra /setup + +Si N8N muestra la página de configuración, verifica que las variables de entorno estén configuradas: + +```bash +N8N_OWNER_EMAIL=admin@farmafinder.com +N8N_OWNER_PASSWORD=change-me +``` + +### Webhook no funciona + +1. Verifica que el workflow esté activo +2. Revisa el historial de ejecuciones en N8N +3. Verifica los logs: `docker logs n8n` + +### Productos no se guardan + +1. Verifica que parapharmacy-api esté ejecutándose +2. Revisa los logs de la API: `docker logs parapharmacy-api` +3. Verifica que MongoDB esté conectado diff --git a/n8n/workflows/parapharmacy-manual-scraper.json b/n8n/workflows/parapharmacy-manual-scraper.json new file mode 100644 index 0000000..545a8ca --- /dev/null +++ b/n8n/workflows/parapharmacy-manual-scraper.json @@ -0,0 +1,181 @@ +{ + "name": "Parapharmacy Manual Scraper (Webhook)", + "nodes": [ + { + "parameters": { + "httpMethod": "POST", + "path": "scrape-parapharmacy", + "options": {} + }, + "id": "webhook-trigger", + "name": "Webhook Trigger", + "type": "n8n-nodes-base.webhook", + "typeVersion": 2, + "position": [220, 300], + "webhookId": "scrape-parapharmacy" + }, + { + "parameters": { + "values": { + "string": [ + { + "name": "queries", + "value": "={{ $json.body?.queries || 'crema hidratante cara,protector solar,leche corporal' }}" + } + ] + }, + "options": {} + }, + "id": "set-queries", + "name": "Set Queries", + "type": "n8n-nodes-base.set", + "typeVersion": 3.4, + "position": [440, 300] + }, + { + "parameters": { + "fieldToSplitOut": "queries", + "options": {} + }, + "id": "split-queries", + "name": "Split Queries", + "type": "n8n-nodes-base.splitOut", + "typeVersion": 1, + "position": [660, 300] + }, + { + "parameters": { + "method": "GET", + "url": "https://www.promofarma.com/es/search?q={{ encodeURIComponent($json.queries) }}", + "options": { + "timeout": 30000 + } + }, + "id": "scrape-promofarma", + "name": "Scrape Promofarma", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [880, 300] + }, + { + "parameters": { + "jsCode": "// Extract products from Promofarma HTML\nconst html = $input.first().json.data;\nconst products = [];\n\nconst productRegex = /
]*>([\\s\\S]*?)<\\/div>/gi;\nconst nameRegex = /]*>([^<]+)<\\/h3>/i;\nconst priceRegex = /class=\"price[^\"]*\"[^>]*>([^<]+)<\\/span>/i;\nconst linkRegex = /]*href=\"([^\"]+)\"[^>]*>/i;\n\nlet match;\nwhile ((match = productRegex.exec(html)) !== null) {\n const card = match[1];\n const name = nameRegex.exec(card)?.[1]?.trim();\n const price = priceRegex.exec(card)?.[1]?.trim();\n const link = linkRegex.exec(card)?.[1];\n \n if (name) {\n products.push({\n name,\n price: parseFloat(price?.replace(/[^\\d.,]/g, '').replace(',', '.')) || 0,\n source_url: link?.startsWith('http') ? link : `https://www.promofarma.com${link}`,\n source: 'promofarma',\n source_product_id: link?.match(/\\/p\\/([^/]+)/)?.[1] || Date.now().toString()\n });\n }\n}\n\nreturn products.map(p => ({ json: p }));" + }, + "id": "extract-promofarma", + "name": "Extract Products", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [1100, 300] + }, + { + "parameters": { + "method": "POST", + "url": "http://parapharmacy-api:3002/api/products/bulk", + "sendBody": true, + "specifyBody": "json", + "jsonBody": "={{ JSON.stringify({ products: $input.all().map(item => item.json) }) }}", + "options": {} + }, + "id": "send-to-api", + "name": "Send to Parapharmacy API", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [1320, 300] + }, + { + "parameters": { + "respondWith": "json", + "responseBody": "={{ { success: true, products: $input.all().length } }}" + }, + "id": "respond", + "name": "Respond", + "type": "n8n-nodes-base.respondToWebhook", + "typeVersion": 1.1, + "position": [1540, 300] + } + ], + "connections": { + "Webhook Trigger": { + "main": [ + [ + { + "node": "Set Queries", + "type": "main", + "index": 0 + } + ] + ] + }, + "Set Queries": { + "main": [ + [ + { + "node": "Split Queries", + "type": "main", + "index": 0 + } + ] + ] + }, + "Split Queries": { + "main": [ + [ + { + "node": "Scrape Promofarma", + "type": "main", + "index": 0 + } + ] + ] + }, + "Scrape Promofarma": { + "main": [ + [ + { + "node": "Extract Products", + "type": "main", + "index": 0 + } + ] + ] + }, + "Extract Products": { + "main": [ + [ + { + "node": "Send to Parapharmacy API", + "type": "main", + "index": 0 + } + ] + ] + }, + "Send to Parapharmacy API": { + "main": [ + [ + { + "node": "Respond", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": true, + "settings": { + "executionOrder": "v1" + }, + "versionId": "1", + "tags": [ + { + "name": "parapharmacy" + }, + { + "name": "scraper" + }, + { + "name": "webhook" + } + ] +} diff --git a/n8n/workflows/parapharmacy-scraper.json b/n8n/workflows/parapharmacy-scraper.json new file mode 100644 index 0000000..6c5903a --- /dev/null +++ b/n8n/workflows/parapharmacy-scraper.json @@ -0,0 +1,182 @@ +{ + "name": "Parapharmacy Scraper", + "nodes": [ + { + "parameters": { + "rule": { + "interval": [ + { + "field": "cronExpression", + "expression": "0 2 */3 * *" + } + ] + } + }, + "id": "schedule-trigger", + "name": "Schedule (Every 3 days at 2am)", + "type": "n8n-nodes-base.scheduleTrigger", + "typeVersion": 1.2, + "position": [220, 300] + }, + { + "parameters": { + "values": { + "string": [ + { + "name": "queries", + "value": "crema hidratante cara,protector solar,leche corporal,champú bebé,aceite bebé,crema pañal,vitaminas bebé,formula lactea" + } + ] + }, + "options": {} + }, + "id": "set-queries", + "name": "Set Search Queries", + "type": "n8n-nodes-base.set", + "typeVersion": 3.4, + "position": [440, 300] + }, + { + "parameters": { + "fieldToSplitOut": "queries", + "options": {} + }, + "id": "split-queries", + "name": "Split Queries", + "type": "n8n-nodes-base.splitOut", + "typeVersion": 1, + "position": [660, 300] + }, + { + "parameters": { + "method": "GET", + "url": "https://www.promofarma.com/es/search?q={{ encodeURIComponent($json.queries) }}", + "options": { + "timeout": 30000 + } + }, + "id": "scrape-promofarma", + "name": "Scrape Promofarma", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [880, 200] + }, + { + "parameters": { + "jsCode": "// Extract products from Promofarma HTML\nconst html = $input.first().json.data;\nconst products = [];\n\n// Simple regex extraction (adjust selectors based on actual HTML)\nconst productRegex = /
]*>([\\s\\S]*?)<\\/div>/gi;\nconst nameRegex = /]*>([^<]+)<\\/h3>/i;\nconst priceRegex = /class=\"price[^\"]*\"[^>]*>([^<]+)<\\/span>/i;\nconst linkRegex = /]*href=\"([^\"]+)\"[^>]*>/i;\n\nlet match;\nwhile ((match = productRegex.exec(html)) !== null) {\n const card = match[1];\n const name = nameRegex.exec(card)?.[1]?.trim();\n const price = priceRegex.exec(card)?.[1]?.trim();\n const link = linkRegex.exec(card)?.[1];\n \n if (name) {\n products.push({\n name,\n price: parseFloat(price?.replace(/[^\\d.,]/g, '').replace(',', '.')) || 0,\n source_url: link?.startsWith('http') ? link : `https://www.promofarma.com${link}`,\n source: 'promofarma',\n source_product_id: link?.match(/\\/p\\/([^/]+)/)?.[1] || Date.now().toString()\n });\n }\n}\n\nreturn products.map(p => ({ json: p }));" + }, + "id": "extract-promofarma", + "name": "Extract Products", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [1100, 200] + }, + { + "parameters": { + "method": "POST", + "url": "http://parapharmacy-api:3002/api/products/bulk", + "sendBody": true, + "bodyParameters": { + "parameters": [] + }, + "specifyBody": "json", + "jsonBody": "={{ JSON.stringify({ products: $input.all().map(item => item.json) }) }}", + "options": {} + }, + "id": "send-to-api", + "name": "Send to Parapharmacy API", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [1320, 200] + }, + { + "parameters": {}, + "id": "no-operation", + "name": "No Op", + "type": "n8n-nodes-base.noOp", + "typeVersion": 1, + "position": [1540, 200] + } + ], + "connections": { + "Schedule (Every 3 days at 2am)": { + "main": [ + [ + { + "node": "Set Search Queries", + "type": "main", + "index": 0 + } + ] + ] + }, + "Set Search Queries": { + "main": [ + [ + { + "node": "Split Queries", + "type": "main", + "index": 0 + } + ] + ] + }, + "Split Queries": { + "main": [ + [ + { + "node": "Scrape Promofarma", + "type": "main", + "index": 0 + } + ] + ] + }, + "Scrape Promofarma": { + "main": [ + [ + { + "node": "Extract Products", + "type": "main", + "index": 0 + } + ] + ] + }, + "Extract Products": { + "main": [ + [ + { + "node": "Send to Parapharmacy API", + "type": "main", + "index": 0 + } + ] + ] + }, + "Send to Parapharmacy API": { + "main": [ + [ + { + "node": "No Op", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": false, + "settings": { + "executionOrder": "v1" + }, + "versionId": "1", + "tags": [ + { + "name": "parapharmacy" + }, + { + "name": "scraper" + } + ] +}