AndyVROMO

Create Job on Site with Google Maps API

Feb 19th, 2020
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Provide your VROMO access_token and Google Maps Geocode API Key
  2. const access_token = "{{access_token}}"
  3. const googleMapsAPIKey = "{{google_maps_api_key}}"
  4.  
  5. // assumes an object called "input" exists with properties "siteName", "siteAddress", "deliveryAddress", "siteBlueprint"
  6. const siteName = input.siteName
  7. const siteAddress = input.siteAddress
  8. const deliveryAddress = input.deliveryAddress
  9. const siteBlueprint = input.siteBlueprint
  10.  
  11. // get the first element of an array
  12. const head = ([x]) => x
  13.  
  14. // google maps geocode api call that return the coords "0, 0" on failure
  15. const googleMapsLookup = (address, apiKey) =>
  16.   fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURI(address)}&key=${apiKey}`)
  17.   .then(res => res.json())
  18.   .then(data => {
  19.     const { lat, lng } = data.results[0].geometry.location
  20.     return { lat, lon: lng }
  21.   })
  22.   .catch(() => ({ lat: 0, lon: 0 }))
  23.  
  24. const process = async () => {
  25.   // get all sites
  26.   const sites = await fetch(
  27.     `https://api.vromo.io/v2/graph/role/sites?access_token=${access_token}`, {
  28.       method: "GET"
  29.     }
  30.   ).then(res => res.json())
  31.  
  32.   // filter sites by site name and get the first one
  33.   let site = head(sites.items.filter(site => site.contact.name === siteName))
  34.  
  35.   // if there was no match found for site, get the coords for the site via the Google Maps API then create the site using those coords
  36.   if (!site) {
  37.     const siteCoords = await googleMapsLookup(siteAddress, googleMapsAPIKey)
  38.  
  39.     site = await fetch(
  40.         `https://api.vromo.io/v2/graph/role/sites?access_token=${access_token}`, {
  41.           method: "POST",
  42.           headers: {
  43.             "Content-Type": "application/json"
  44.           },
  45.           body: JSON.stringify([{
  46.             contact: {
  47.               name: siteName,
  48.               address: siteAddress
  49.             },
  50.             coords: {
  51.               lat: siteCoords.lat,
  52.               lon: siteCoords.lon
  53.             },
  54.             blueprint: {
  55.               id: siteBlueprint
  56.             }
  57.           }])
  58.         }
  59.       )
  60.       .then(res => res.json())
  61.       .then(result => head(result.items))
  62.   }
  63.  
  64.   // get the delivery address's coordinates from the Google Maps API Key
  65.   const deliveryCoords = await googleMapsLookup(deliveryAddress, googleMapsAPIKey)
  66.  
  67.   // create job on site
  68.   return fetch(
  69.     `https://api.vromo.io/v2/graph/role/sites/${site.id}/jobs?access_token=${access_token}`, {
  70.       method: "POST",
  71.       headers: {
  72.         "Content-Type": "application/json"
  73.       },
  74.       body: JSON.stringify([{
  75.         name: "Order Number #483920",
  76.         contact: {
  77.           name: "Mike",
  78.           phone: "+353832224444",
  79.           email: "mike@gmail.com"
  80.         },
  81.         attr: [{
  82.             key: "Chef Instructions",
  83.             value: "No onions on burger please"
  84.           },
  85.           {
  86.             key: "Driver Instructions",
  87.             value: "Please do not ring the doorbell"
  88.           }
  89.         ],
  90.         tasks: [{
  91.             timeframe: {
  92.               by: 1578585415881
  93.             }
  94.           },
  95.           {
  96.             timeframe: {
  97.               by: 1578589038037
  98.             },
  99.             zone: {
  100.               name: "Customer",
  101.               address: deliveryAddress,
  102.               coords: {
  103.                 lat: deliveryCoords.lat,
  104.                 lon: deliveryCoords.lon
  105.               }
  106.             }
  107.           }
  108.         ]
  109.       }])
  110.     }
  111.   ).then(res => res.json())
  112. }
  113.  
  114. process().then(createdJob => console.log(createdJob))
Add Comment
Please, Sign In to add comment