Advertisement
AndyVROMO

Fetch Site And Create Job

Feb 18th, 2020
1,311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const access_token = "{{access_token}}"
  2. // assumes a variable called "input" with a property called "siteName"
  3. const siteName = input.siteName
  4.  
  5. // return the first item in an array
  6. const head = ([x]) => x
  7.  
  8. fetch(`https://api.vromo.io/v2/graph/role/sites?access_token=${access_token}`, {
  9.     method: "GET"
  10.   })
  11.   .then(res => res.json())
  12.   .then(sites => {
  13.     // filters the list of all sites down to the site with the given name
  14.     const site = head(sites.items.filter(site => site.contact.name === siteName))
  15.  
  16.     // site.id in the url ensures the job is created on the fetched site
  17.     return fetch(`https://api.vromo.io/v2/graph/role/sites/${site.id}/jobs?access_token=${access_token}`, {
  18.       method: "POST",
  19.       headers: {
  20.         "Content-Type": "application/json"
  21.       },
  22.       body: JSON.stringify(
  23.         [{
  24.           "name": "Order Number #483920",
  25.           "contact": {
  26.             "name": "Mike",
  27.             "phone": "+353832224444",
  28.             "email": "mike@gmail.com"
  29.           },
  30.           "attr": [{
  31.               "key": "Chef Instructions",
  32.               "value": "No onions on burger please"
  33.             },
  34.             {
  35.               "key": "Driver Instructions",
  36.               "value": "Please do not ring the doorbell"
  37.             }
  38.           ],
  39.           "tasks": [{
  40.               "timeframe": {
  41.                 "by": 1578585415881
  42.               }
  43.             },
  44.             {
  45.               "timeframe": {
  46.                 "by": 1578589038037
  47.               },
  48.               "zone": {
  49.                 "name": "Customer",
  50.                 "address": "VROMO, Phibsborough Tower, Dublin 7, Ireland",
  51.                 "coords": {
  52.                   "lat": 53.355473,
  53.                   "lon": -6.270598
  54.                 }
  55.               }
  56.             }
  57.           ]
  58.         }]
  59.       )
  60.     })
  61.   })
  62.   .then(res => res.json())
  63.   .then(data => console.log(data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement