{
  "updatedAt": "2025-12-07T22:27:41.000Z",
  "createdAt": "2025-12-07T21:09:58.855Z",
  "id": "8akUngiiJ5LZGxhz",
  "name": "Practica 7: Ejercicio guiado",
  "description": null,
  "active": false,
  "isArchived": false,
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        -208
      ],
      "id": "c8e58796-6901-4ea5-a631-2275d58a6ea3",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM task WHERE description IS NOT NULL AND description != '' LIMIT 5;",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        224,
        -208
      ],
      "id": "1a0d3b05-fae0-4255-92ec-fef11ef73861",
      "name": "Execute a SQL query",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "models/gemini-flash-lite-latest",
          "mode": "list",
          "cachedResultName": "models/gemini-flash-lite-latest"
        },
        "messages": {
          "values": [
            {
              "content": "=Eres un asistente de gesti\u00f3n de proyectos. Tu trabajo es analizar la siguiente tarea (en formato JSON) y determinar su prioridad (ALTA, MEDIA o BAJA).\n\nEntrada:\n{{ JSON.stringify($json) }}\n\nInstrucciones:\n1. Analiza el t\u00edtulo y la descripci\u00f3n para decidir la prioridad.\n2. Genera un NUEVO objeto JSON que contenga EXACTAMENTE los mismos campos originales (id, title, description, done), pero a\u00f1ade un nuevo campo llamado \"priority\".\n3. No cambies el texto del t\u00edtulo ni la descripci\u00f3n original.\n\nResponde \u00daNICAMENTE con el JSON v\u00e1lido. No incluyas bloques de c\u00f3digo markdown (```json) ni texto extra.Descripci\u00f3n de la tarea: \"{{ $json.description }}\""
            }
          ]
        },
        "jsonOutput": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "typeVersion": 1,
      "position": [
        448,
        -208
      ],
      "id": "51489e7f-2267-4536-9343-16e106cf8e20",
      "name": "Message a model",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.priority.trim() }}",
                    "rightValue": "ALTA",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "f5c0c276-54ca-4ffe-ac0a-4d226a36055a"
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "d5cdee80-4cd3-4320-ae7c-b0e1fa51ed1c",
                    "leftValue": "={{ $json.priority.trim() }}",
                    "rightValue": "=MEDIA",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.3,
      "position": [
        1024,
        -224
      ],
      "id": "a70091e0-a920-40c9-8f71-496e5963ce77",
      "name": "Switch"
    },
    {
      "parameters": {
        "sendTo": "aek676@inlumine.ual.es",
        "subject": "=\u00a1Alerta de Tarea Prioridad ALTA!",
        "emailType": "text",
        "message": "=La tarea \"{{ $json.title }}\" (ID: {{ $json.id }}) ha sido clasificada como ALTA prioridad por la IA.",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1248,
        -304
      ],
      "id": "635b7320-6e88-4515-b943-b62724d0860e",
      "name": "Send a message",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "numberInputs": 3
      },
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        1248,
        -112
      ],
      "id": "b58d529c-d52f-4ea8-971c-9d17b30827b4",
      "name": "Merge"
    },
    {
      "parameters": {
        "operation": "upsert",
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "task",
          "mode": "list",
          "cachedResultName": "task"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {
            "done": false,
            "id": "={{ $json.id }}",
            "priority": "={{ $json.priority }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "title",
              "displayName": "title",
              "required": true,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": true
            },
            {
              "id": "description",
              "displayName": "description",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": true
            },
            {
              "id": "done",
              "displayName": "done",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "boolean",
              "canBeUsedToMatch": false
            },
            {
              "id": "priority",
              "displayName": "priority",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1472,
        -96
      ],
      "id": "4452b836-f27b-4b20-ae3d-28fcfd8fab71",
      "name": "Insert or update rows in a table",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Recorremos cada elemento que devolvi\u00f3 Gemini\nreturn $input.all().map(item => {\n  // 1. Buscamos el texto donde est\u00e1 la respuesta\n  let rawText = item.json.content.parts[0].text;\n\n  // 2. Limpieza de seguridad: A veces Gemini a\u00f1ade ```json al principio y ``` al final.\n  // Esto lo borra para dejar solo el JSON puro.\n  rawText = rawText.replace(/```json|```/g, '').trim();\n\n  try {\n    // 3. Convertimos el texto a Objeto JSON real\n    const parsedData = JSON.parse(rawText);\n    \n    // 4. Devolvemos el dato limpio\n    return {\n      json: parsedData\n    };\n  } catch (error) {\n    // Si falla (por ejemplo si Gemini habl\u00f3 texto normal), devolvemos el original con error\n    return {\n      json: {\n        error: \"No se pudo parsear el JSON\",\n        original: rawText\n      }\n    };\n  }\n});"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        800,
        -208
      ],
      "id": "e83453d9-b000-431a-b788-53a824504d56",
      "name": "Code in JavaScript"
    }
  ],
  "connections": {
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Execute a SQL query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query": {
      "main": [
        [
          {
            "node": "Message a model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message a model": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ],
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Insert or update rows in a table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "versionId": "0cfd824b-d2b3-4a7f-80b1-23cc8cc5f6fe",
  "activeVersionId": null,
  "versionCounter": 6,
  "triggerCount": 0,
  "tags": [],
  "shared": [
    {
      "updatedAt": "2025-12-07T21:09:58.861Z",
      "createdAt": "2025-12-07T21:09:58.861Z",
      "role": "workflow:owner",
      "workflowId": "8akUngiiJ5LZGxhz",
      "projectId": "jEpZT8mLa3eTbWzT",
      "project": {
        "updatedAt": "2025-11-07T16:30:44.943Z",
        "createdAt": "2025-11-07T16:25:40.556Z",
        "id": "jEpZT8mLa3eTbWzT",
        "name": "Anass El Jabiry <aek676@inlumine.ual.es>",
        "type": "personal",
        "icon": null,
        "description": null,
        "creatorId": "685a70bc-7fcf-484c-be50-a33cd8a1716e"
      }
    }
  ]
}