Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Provide your VROMO access_token and Google Maps Geocode API Key
- const access_token = "{{access_token}}"
- const googleMapsAPIKey = "{{google_maps_api_key}}"
- // assumes an object called "input" exists with properties "siteName", "siteAddress", "deliveryAddress", "siteBlueprint"
- const siteName = input.siteName
- const siteAddress = input.siteAddress
- const deliveryAddress = input.deliveryAddress
- const siteBlueprint = input.siteBlueprint
- // get the first element of an array
- const head = ([x]) => x
- // google maps geocode api call that return the coords "0, 0" on failure
- const googleMapsLookup = (address, apiKey) =>
- fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURI(address)}&key=${apiKey}`)
- .then(res => res.json())
- .then(data => {
- const { lat, lng } = data.results[0].geometry.location
- return { lat, lon: lng }
- })
- .catch(() => ({ lat: 0, lon: 0 }))
- 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 one
- let site = head(sites.items.filter(site => site.contact.name === siteName))
- // 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
- if (!site) {
- const siteCoords = await googleMapsLookup(siteAddress, googleMapsAPIKey)
- 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: siteCoords.lat,
- lon: siteCoords.lon
- },
- blueprint: {
- id: siteBlueprint
- }
- }])
- }
- )
- .then(res => res.json())
- .then(result => head(result.items))
- }
- // get the delivery address's coordinates from the Google Maps API Key
- const deliveryCoords = await googleMapsLookup(deliveryAddress, googleMapsAPIKey)
- // 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: deliveryAddress,
- coords: {
- lat: deliveryCoords.lat,
- lon: deliveryCoords.lon
- }
- }
- }
- ]
- }])
- }
- ).then(res => res.json())
- }
- process().then(createdJob => console.log(createdJob))
Add Comment
Please, Sign In to add comment