Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const access_token = "{{access_token}}"
- // assumes a variable called "input" with a properties "siteName", "siteAddress", "siteLatitude", "siteLongitude", "siteBlueprint"
- const siteName = input.siteName
- const siteAddress = input.siteAddress
- const siteLatitude = input.siteLatitude
- const siteLongitude = input.siteLongitude
- const siteBlueprint = input.siteBlueprint
- // return the first item in an array
- const head = ([x]) => x
- // for convenience we are using an asynchronous function
- const process = async () => {
- // get all sites
- const sites = await fetch(
- `https://api.vromo.io/v2/graph/role/sites?access_token=${access_token}`, {
- method: "GET"
- }
- ).then(res => res.json())
- // filter sites by site name and get the first match
- let site = head(sites.items.filter(site => site.contact.name === siteName))
- // if our filter returned no results create the site and return the assign the created site to "site"
- if (!site) {
- site = await fetch(
- `https://api.vromo.io/v2/graph/role/sites?access_token=${access_token}`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify([{
- contact: {
- name: siteName,
- address: siteAddress
- },
- coords: {
- lat: siteLatitude,
- lon: siteLongitude
- },
- blueprint: {
- id: siteBlueprint
- }
- }])
- }
- )
- .then(res => res.json())
- .then(result => head(result.items))
- }
- // create job on site
- return fetch(
- `https://api.vromo.io/v2/graph/role/sites/${site.id}/jobs?access_token=${access_token}`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify([{
- name: "Order Number #483920",
- contact: {
- name: "Mike",
- phone: "+353832224444",
- email: "mike@gmail.com"
- },
- attr: [{
- key: "Chef Instructions",
- value: "No onions on burger please"
- },
- {
- key: "Driver Instructions",
- value: "Please do not ring the doorbell"
- }
- ],
- tasks: [{
- timeframe: {
- by: 1578585415881
- }
- },
- {
- timeframe: {
- by: 1578589038037
- },
- zone: {
- name: "Customer",
- address: "VROMO, Phibsborough Tower, Dublin 7, Ireland",
- coords: {
- lat: 53.355473,
- lon: -6.270598
- }
- }
- }
- ]
- }])
- }
- ).then(res => res.json())
- }
- // call the "process" function and print the created job to the console
- process().then(createdJob => console.log(createdJob))
Add Comment
Please, Sign In to add comment