AndyVROMO

Fetch/Create Site and Create Job

Feb 19th, 2020
735
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 properties "siteName", "siteAddress", "siteLatitude", "siteLongitude", "siteBlueprint"
  3. const siteName = input.siteName
  4. const siteAddress = input.siteAddress
  5. const siteLatitude = input.siteLatitude
  6. const siteLongitude = input.siteLongitude
  7. const siteBlueprint = input.siteBlueprint
  8.  
  9. // return the first item in an array
  10. const head = ([x]) => x
  11.  
  12. // for convenience we are using an asynchronous function
  13. const process = async () => {
  14.   // get all sites
  15.   const sites = await fetch(
  16.     `https://api.vromo.io/v2/graph/role/sites?access_token=${access_token}`, {
  17.       method: "GET"
  18.     }
  19.   ).then(res => res.json())
  20.  
  21.   // filter sites by site name and get the first match
  22.   let site = head(sites.items.filter(site => site.contact.name === siteName))
  23.  
  24.   // if our filter returned no results create the site and return the assign the created site to "site"
  25.   if (!site) {
  26.     site = await fetch(
  27.         `https://api.vromo.io/v2/graph/role/sites?access_token=${access_token}`, {
  28.           method: "POST",
  29.           headers: {
  30.             "Content-Type": "application/json"
  31.           },
  32.           body: JSON.stringify([{
  33.             contact: {
  34.               name: siteName,
  35.               address: siteAddress
  36.             },
  37.             coords: {
  38.               lat: siteLatitude,
  39.               lon: siteLongitude
  40.             },
  41.             blueprint: {
  42.               id: siteBlueprint
  43.             }
  44.           }])
  45.         }
  46.       )
  47.       .then(res => res.json())
  48.       .then(result => head(result.items))
  49.   }
  50.  
  51.   // create job on site
  52.   return fetch(
  53.     `https://api.vromo.io/v2/graph/role/sites/${site.id}/jobs?access_token=${access_token}`, {
  54.       method: "POST",
  55.       headers: {
  56.         "Content-Type": "application/json"
  57.       },
  58.       body: JSON.stringify([{
  59.         name: "Order Number #483920",
  60.         contact: {
  61.           name: "Mike",
  62.           phone: "+353832224444",
  63.           email: "mike@gmail.com"
  64.         },
  65.         attr: [{
  66.             key: "Chef Instructions",
  67.             value: "No onions on burger please"
  68.           },
  69.           {
  70.             key: "Driver Instructions",
  71.             value: "Please do not ring the doorbell"
  72.           }
  73.         ],
  74.         tasks: [{
  75.             timeframe: {
  76.               by: 1578585415881
  77.             }
  78.           },
  79.           {
  80.             timeframe: {
  81.               by: 1578589038037
  82.             },
  83.             zone: {
  84.               name: "Customer",
  85.               address: "VROMO, Phibsborough Tower, Dublin 7, Ireland",
  86.               coords: {
  87.                 lat: 53.355473,
  88.                 lon: -6.270598
  89.               }
  90.             }
  91.           }
  92.         ]
  93.       }])
  94.     }
  95.   ).then(res => res.json())
  96. }
  97.  
  98. // call the "process" function and print the created job to the console
  99. process().then(createdJob => console.log(createdJob))
Add Comment
Please, Sign In to add comment