feat: add N8N workflows and auto-setup configuration

- Add parapharmacy-scraper workflow (scheduled every 3 days)
- Add parapharmacy-manual-scraper workflow (webhook triggered)
- Configure N8N to auto-create owner account (skips /setup)
- Add N8N environment variables to docker-compose.yml
- Add README with setup and usage instructions
This commit is contained in:
Antoni Nuñez Romeu
2026-07-16 12:46:20 +02:00
parent 0c2f1cf928
commit 1c70f0a914
4 changed files with 462 additions and 0 deletions
+89
View File
@@ -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
@@ -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 = /<div class=\"product-card[^\"]*\"[^>]*>([\\s\\S]*?)<\\/div>/gi;\nconst nameRegex = /<h3[^>]*>([^<]+)<\\/h3>/i;\nconst priceRegex = /class=\"price[^\"]*\"[^>]*>([^<]+)<\\/span>/i;\nconst linkRegex = /<a[^>]*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"
}
]
}
+182
View File
@@ -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 = /<div class=\"product-card[^\"]*\"[^>]*>([\\s\\S]*?)<\\/div>/gi;\nconst nameRegex = /<h3[^>]*>([^<]+)<\\/h3>/i;\nconst priceRegex = /class=\"price[^\"]*\"[^>]*>([^<]+)<\\/span>/i;\nconst linkRegex = /<a[^>]*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"
}
]
}