{
  "name": "notification alert",
  "nodes": [
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT u.name AS user_name, u.email AS user_email, ulm.match_score, ulm.recommendation_strength, lp.product_name AS loan_name, lp.interest_rate, lp.max_amount, lp.min_amount, ulm.user_id, ulm.product_id FROM user_loan_matches ulm JOIN users u ON ulm.user_id = u.user_id JOIN loan_products lp ON ulm.product_id = lp.id -- Optional: Add a WHERE clause to filter only matches that haven't been notified yet (if using this query for Workflow C) \nWHERE ulm.is_notified = FALSE;",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        224,
        0
      ],
      "id": "503ce09a-857b-4a76-8ece-d96f19377a15",
      "name": "Execute a SQL query",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Group all products by user\nconst users = {};\n\nitems.forEach(item => {\n  const row = item.json;\n  const uid = row.user_id;\n\n  if (!users[uid]) {\n    users[uid] = {\n      name: row.user_name,\n      email: row.user_email,\n      products: []\n    };\n  }\n\n  users[uid].products.push({\n    loan_name: row.loan_name,\n    interest_rate: row.interest_rate,\n    min_amount: row.min_amount,\n    max_amount: row.max_amount,\n    match_score: row.match_score,\n    recommendation_strength: row.recommendation_strength\n  });\n});\n\n// Convert grouped data into email items for SES\nreturn Object.values(users).map(user => {\n  // Build loan recommendation section\n  const listText = user.products\n    .map(p => \n      `\u2022 ${p.loan_name}\n     - Interest Rate: ${p.interest_rate}%\n     - Amount Range: ${p.min_amount} to ${p.max_amount}\n     - Match Score: ${p.match_score}\n     - Strength: ${p.recommendation_strength}\\n`\n    )\n    .join(\"\\n\");\n\n  return {\n    json: {\n      to: user.email,\n      subject: `Your Personalized Loan Recommendations`,\n      body: `\nHello ${user.name},\n\nBased on your profile, we found the following loan options for you:\n\n${listText}\n\nYou can explore these offers and choose the best one.\n\nRegards,\nLoan Engine Team\n`\n    }\n  };\n});\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        0
      ],
      "id": "ba93e0fe-eb6d-4ebe-b5d6-dd613f5a8fde",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -128,
        0
      ],
      "id": "9ddbd5d1-c848-4ae4-9de1-eb3dcfda6cd6",
      "name": "When clicking \u2018Execute workflow\u2019"
    }
  ],
  "connections": {
    "Execute a SQL query": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        []
      ]
    },
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Execute a SQL query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1638e77b-76ed-4948-89a3-41b0bd287afd",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "Z49V1WgZwnbNUmqK",
  "tags": []
}