{
  "nodes": [
    {
      "id": "4fe13927-84cb-4227-9daa-d6cef72d10b9",
      "name": "CarrierNameLookup",
      "type": "n8n-nodes-base.code",
      "position": [
        740,
        320
      ],
      "parameters": {
        "jsCode": "var carrierCodes={}\nJSON.parse($('Get Carrier Codes').first().json.data).data.forEach(datum=>{\n  carrierCodes[datum.iataCode] = {icao:datum.icaoCode, name:datum.commonName}\n})\nreturn carrierCodes"
      },
      "typeVersion": 2
    },
    {
      "id": "cb0ab93c-5fc5-402d-8ac9-672960b14112",
      "name": "Gmail",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2080,
        400
      ],
      "parameters": {
        "message": "=Hi! We just found a bargain flight:\nDeparture Time: {{ $json.time }}\n[{{ $json.legs[0].carrier }}] {{ $json.duration }} flight from {{ $('FromTo').first().json.from }} to {{ $('FromTo').first().json.to }}\n",
        "options": {},
        "subject": "=Bargain Flight Found! {{ $('FromTo').first().json.from }} -> {{ $('FromTo').first().json.to }} @ {{ $json.price }} on {{ $json.time }}"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "2f98b3e2-8a25-496e-89f3-b1ebe7e33192",
      "name": "Get Dates",
      "type": "n8n-nodes-base.code",
      "position": [
        940,
        300
      ],
      "parameters": {
        "jsCode": "const getNextSevenDays = () => {\n    const dates = [];\n    const today = new Date();\n\n    for (const i of [7, 14]) {\n        const nextDate = new Date(today);\n        nextDate.setDate(today.getDate() + i);\n        \n        // Format the date as YYYY-MM-DD\n        const formattedDate = nextDate.toISOString().split('T')[0];\n        dates.push({date:formattedDate});\n    }\n\n    return dates;\n};\n\nreturn getNextSevenDays()"
      },
      "typeVersion": 2
    },
    {
      "id": "3d8cf3fa-6ce7-422a-978f-afe2884c1e1a",
      "name": "Merge & Extract",
      "type": "n8n-nodes-base.code",
      "position": [
        1660,
        400
      ],
      "parameters": {
        "jsCode": "//Merge\nresult = []\nfor (const item of $input.all()) {\n result = result.concat(JSON.parse(item.json.data).data)\n}\n\n//Extract data fields\nfinal_result = []\nfor (x of result){\n  let legs = x.itineraries[0].segments.map(y=>{\n          let a = $('CarrierNameLookup').item.json[y.carrierCode];\n           let carrier = a.name? a.name: a.icao;\n           return {carrier:carrier, duration:y.duration}})\n\n\n  console.log(x.itineraries[0].segments[0].departure.at)\n  let duration = x.itineraries[0].duration\n  let price = x.price.total+' '+x.price.currency\n\n  final_result.push({legs:legs, time:x.itineraries[0].segments[0].departure.at, duration:duration, price:price})\n}\n\nreturn final_result"
      },
      "typeVersion": 2
    },
    {
      "id": "89df1c9b-c863-4cf5-88a2-18793d542f02",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1200,
        240
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "5595e34d-3736-42f6-ad64-e7f3c72c7f0a",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "position": [
        1060,
        440
      ],
      "parameters": {
        "amount": 3
      },
      "typeVersion": 1.1
    },
    {
      "id": "550005ad-ea97-4d83-90ac-67c7c583f2dc",
      "name": "Under Price",
      "type": "n8n-nodes-base.filter",
      "position": [
        1880,
        400
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "bc2a9b61-41eb-45b1-9ee3-00fe211dadc3",
              "operator": {
                "type": "number",
                "operation": "lt"
              },
              "leftValue": "={{ parseFloat($json.price) }}",
              "rightValue": 600
            }
          ]
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "ce1beef1-4189-4cd7-b8c6-dd5bef2d9963",
      "name": "FromTo",
      "type": "n8n-nodes-base.set",
      "position": [
        560,
        320
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "1944c696-6cfd-4d4d-8f3d-31cb89b37d3d",
              "name": "from",
              "type": "string",
              "value": "LHR"
            },
            {
              "id": "9c4d5ac9-fa75-4fa7-a369-2b0493150203",
              "name": "to",
              "type": "string",
              "value": "JFK"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a4956257-28ce-4014-b549-ad413264c012",
      "name": "Amadeus Flight Search",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1340,
        440
      ],
      "parameters": {
        "url": "https://test.api.amadeus.com/v2/shopping/flight-offers",
        "options": {},
        "sendQuery": true,
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "queryParameters": {
          "parameters": [
            {
              "name": "originLocationCode",
              "value": "={{ $('FromTo').item.json.from }}"
            },
            {
              "name": "destinationLocationCode",
              "value": "={{ $('FromTo').item.json.to }}"
            },
            {
              "name": "adults",
              "value": "1"
            },
            {
              "name": "departureDate",
              "value": "={{ $json.date }}"
            }
          ]
        }
      },
      "credentials": {
        "oAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "b7a41d45-799d-4f65-a904-f8fa82e59620",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        260,
        -320
      ],
      "parameters": {
        "width": 490.02125360646824,
        "height": 538.1571460797832,
        "content": "# Amadeus Flight Bargains\nEvery day checks for bargain flights for an itinerary and price target of your choosing, and emails you if it finds a match.\n\n## Setup\n1. Create an api account on https://developers.amadeus.com/\n2. In **Amadeus Flight Search**, connect to Oauth2 API:\n  -- Grant Type - Client Credentials\n  -- Access Token URL - https://test.api.amadeus.com/v1/security/oauth2/token\n  -- Client ID/Secret - from your account\n3. Set your details in **Gmail**\n4. Set your desired Origin/Destination airports in FromTo\n5. Set the dates ahead you wish to search in **Get Dates** (default is 7 days and 14 days)\n6. Set the price target in **Under Price**\n\n## Test\nHit 'Test workflow'!"
      },
      "typeVersion": 1
    },
    {
      "id": "88126395-c96a-4905-87db-57ad19cead23",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        360,
        320
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 8
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "0fa74451-6053-470c-b5c5-9b25fd2e5b55",
      "name": "Get Carrier Codes",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        360,
        600
      ],
      "parameters": {
        "url": "https://test.api.amadeus.com/v1/reference-data/airlines",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api"
      },
      "credentials": {
        "oAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    }
  ],
  "connections": {
    "Wait": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "FromTo": {
      "main": [
        [
          {
            "node": "CarrierNameLookup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Dates": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Under Price": {
      "main": [
        [
          {
            "node": "Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Merge & Extract",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Amadeus Flight Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge & Extract": {
      "main": [
        [
          {
            "node": "Under Price",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "FromTo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CarrierNameLookup": {
      "main": [
        [
          {
            "node": "Get Dates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Amadeus Flight Search": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}