{
  "name": "FlowPantry - calendar-mileage-auto-log",
  "flow": [
    {
      "id": 1,
      "module": "gateway:CustomWebHook",
      "version": 1,
      "parameters": {
        "hook": null,
        "maxResults": 1
      },
      "mapper": {},
      "metadata": {
        "designer": {
          "x": 0,
          "y": 0
        }
      }
    },
    {
      "id": 2,
      "module": "http:ActionSendData",
      "version": 3,
      "parameters": {},
      "mapper": {
        "url": "https://routes.googleapis.com/directions/v2:computeRoutes",
        "method": "post",
        "headers": [
          {
            "name": "Content-Type",
            "value": "application/json"
          },
          {
            "name": "X-Goog-Api-Key",
            "value": "YOUR_GOOGLE_MAPS_API_KEY"
          },
          {
            "name": "X-Goog-FieldMask",
            "value": "routes.distanceMeters,routes.duration"
          }
        ],
        "qs": [],
        "serializeUrl": false,
        "parseResponse": true,
        "shareCookies": false,
        "rejectUnauthorized": true,
        "followRedirect": true,
        "useQuerystring": false,
        "gzip": true,
        "useMtls": false,
        "timeout": "",
        "bodyType": "raw",
        "contentType": "application/json",
        "inputMethod": "jsonString",
        "data": "{\"origin\": {\"address\": \"{{1.origin}}\"}, \"destination\": {\"address\": \"{{1.destination}}\"}, \"travelMode\": \"DRIVE\", \"routingPreference\": \"TRAFFIC_UNAWARE\", \"units\": \"IMPERIAL\"}"
      },
      "metadata": {
        "designer": {
          "x": 300,
          "y": 0
        }
      }
    },
    {
      "id": 3,
      "module": "builtin:BasicRouter",
      "version": 1,
      "parameters": {},
      "metadata": {
        "designer": {
          "x": 600,
          "y": 0
        }
      },
      "routes": [
        {
          "flow": [
            {
              "id": 4,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": {},
              "filter": {
                "name": "Looks right",
                "conditions": [
                  [
                    {
                      "a": "{{2.data.routes[].distanceMeters}}",
                      "o": "exist"
                    },
                    {
                      "a": "{{2.data.routes[].distanceMeters}}",
                      "o": "number:greater",
                      "b": "0"
                    },
                    {
                      "a": "{{2.data.routes[].distanceMeters}}",
                      "o": "number:lessorequal",
                      "b": "321868"
                    }
                  ]
                ]
              },
              "mapper": {
                "url": "SHEET_WEBHOOK_URL",
                "method": "post",
                "headers": [],
                "qs": [],
                "serializeUrl": false,
                "parseResponse": true,
                "shareCookies": false,
                "rejectUnauthorized": true,
                "followRedirect": true,
                "useQuerystring": false,
                "gzip": true,
                "useMtls": false,
                "timeout": "",
                "bodyType": "raw",
                "contentType": "application/json",
                "inputMethod": "jsonString",
                "data": "{\"trip_date\": \"{{1.trip_date}}\", \"event\": \"{{1.purpose}}\", \"origin\": \"{{1.origin}}\", \"destination\": \"{{1.destination}}\", \"leg\": \"{{1.leg}}\", \"meters\": \"{{2.data.routes[].distanceMeters}}\", \"status\": \"auto\", \"event_id\": \"{{1.event_id}}\"}"
              },
              "metadata": {
                "designer": {
                  "x": 900,
                  "y": -100
                }
              }
            }
          ]
        },
        {
          "flow": [
            {
              "id": 5,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": {},
              "filter": {
                "name": "Needs a human",
                "conditions": [
                  [
                    {
                      "a": "{{2.data.routes[].distanceMeters}}",
                      "o": "notexist"
                    }
                  ]
                ]
              },
              "mapper": {
                "url": "SHEET_WEBHOOK_URL",
                "method": "post",
                "headers": [],
                "qs": [],
                "serializeUrl": false,
                "parseResponse": true,
                "shareCookies": false,
                "rejectUnauthorized": true,
                "followRedirect": true,
                "useQuerystring": false,
                "gzip": true,
                "useMtls": false,
                "timeout": "",
                "bodyType": "raw",
                "contentType": "application/json",
                "inputMethod": "jsonString",
                "data": "{\"trip_date\": \"{{1.trip_date}}\", \"event\": \"{{1.purpose}}\", \"origin\": \"{{1.origin}}\", \"destination\": \"{{1.destination}}\", \"status\": \"needs_review\", \"maps_status\": \"{{2.data.error.message}}\", \"event_id\": \"{{1.event_id}}\"}"
              },
              "metadata": {
                "designer": {
                  "x": 900,
                  "y": 100
                }
              }
            },
            {
              "id": 6,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": {},
              "mapper": {
                "url": "https://api.resend.com/emails",
                "method": "post",
                "headers": [
                  {
                    "name": "Authorization",
                    "value": "Bearer YOUR_RESEND_API_KEY"
                  }
                ],
                "qs": [],
                "serializeUrl": false,
                "parseResponse": true,
                "shareCookies": false,
                "rejectUnauthorized": true,
                "followRedirect": true,
                "useQuerystring": false,
                "gzip": true,
                "useMtls": false,
                "timeout": "",
                "bodyType": "raw",
                "contentType": "application/json",
                "inputMethod": "jsonString",
                "data": "{\"from\": \"YOUR BUSINESS <you@yourdomain.com>\", \"to\": \"OWNER_EMAIL\", \"subject\": \"Check mileage: {{1.purpose}}\", \"text\": \"Routes API could not return a distance for {{1.origin}} → {{1.destination}} on {{1.trip_date}} ({{2.data.error.message}}). Logged as needs_review.\"}"
              },
              "metadata": {
                "designer": {
                  "x": 1200,
                  "y": 100
                }
              }
            }
          ]
        },
        {
          "flow": [
            {
              "id": 7,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": {},
              "filter": {
                "name": "OK but zero or too far",
                "conditions": [
                  [
                    {
                      "a": "{{2.data.routes[].distanceMeters}}",
                      "o": "exist"
                    },
                    {
                      "a": "{{2.data.routes[].distanceMeters}}",
                      "o": "number:greater",
                      "b": "321868"
                    }
                  ],
                  [
                    {
                      "a": "{{2.data.routes[].distanceMeters}}",
                      "o": "exist"
                    },
                    {
                      "a": "{{2.data.routes[].distanceMeters}}",
                      "o": "number:lessorequal",
                      "b": "0"
                    }
                  ]
                ]
              },
              "mapper": {
                "url": "SHEET_WEBHOOK_URL",
                "method": "post",
                "headers": [],
                "qs": [],
                "serializeUrl": false,
                "parseResponse": true,
                "shareCookies": false,
                "rejectUnauthorized": true,
                "followRedirect": true,
                "useQuerystring": false,
                "gzip": true,
                "useMtls": false,
                "timeout": "",
                "bodyType": "raw",
                "contentType": "application/json",
                "inputMethod": "jsonString",
                "data": "{\"trip_date\": \"{{1.trip_date}}\", \"event\": \"{{1.purpose}}\", \"origin\": \"{{1.origin}}\", \"destination\": \"{{1.destination}}\", \"meters\": \"{{2.data.routes[].distanceMeters}}\", \"status\": \"needs_review\", \"review_notes\": \"zero miles or over 200 miles — check address\", \"event_id\": \"{{1.event_id}}\"}"
              },
              "metadata": {
                "designer": {
                  "x": 900,
                  "y": 300
                }
              }
            },
            {
              "id": 8,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": {},
              "mapper": {
                "url": "https://api.resend.com/emails",
                "method": "post",
                "headers": [
                  {
                    "name": "Authorization",
                    "value": "Bearer YOUR_RESEND_API_KEY"
                  }
                ],
                "qs": [],
                "serializeUrl": false,
                "parseResponse": true,
                "shareCookies": false,
                "rejectUnauthorized": true,
                "followRedirect": true,
                "useQuerystring": false,
                "gzip": true,
                "useMtls": false,
                "timeout": "",
                "bodyType": "raw",
                "contentType": "application/json",
                "inputMethod": "jsonString",
                "data": "{\"from\": \"YOUR BUSINESS <you@yourdomain.com>\", \"to\": \"OWNER_EMAIL\", \"subject\": \"Check mileage (zero or long trip): {{1.purpose}}\", \"text\": \"Distance is {{2.data.routes[].distanceMeters}} meters (zero or over 200 miles) for {{1.origin}} → {{1.destination}}. Logged as needs_review.\"}"
              },
              "metadata": {
                "designer": {
                  "x": 1200,
                  "y": 300
                }
              }
            }
          ]
        }
      ]
    }
  ],
  "metadata": {
    "instant": true,
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": false,
      "slots": null,
      "confidential": false,
      "dataloss": false,
      "dlq": false,
      "freshVariables": false
    },
    "designer": {
      "orphans": []
    },
    "zone": "us1.make.com"
  }
}
