· 6 years ago · Feb 19, 2020, 03:42 PM
1// Provide your VROMO access_token and Google Maps Geocode API Key
2const access_token = "{{access_token}}"
3const googleMapsAPIKey = "{{google_maps_api_key}}"
4
5// assumes an object called "input" exists with properties "siteName", "siteAddress", "deliveryAddress", "siteBlueprint"
6const siteName = input.siteName
7const siteAddress = input.siteAddress
8const deliveryAddress = input.deliveryAddress
9const siteBlueprint = input.siteBlueprint
10
11// get the first element of an array
12const head = ([x]) => x
13
14// google maps geocode api call that return the coords "0, 0" on failure
15const 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
24const 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 )
112 .then(res => res.json())
113 .catch(() => fetch(
114 `https://api.vromo.io/v2/graph/role/sites/${site.id}/jobs?access_token=${access_token}`, {
115 method: "POST",
116 headers: {
117 "Content-Type": "application/json"
118 },
119 body: JSON.stringify([{
120 name: "Order Number #483920",
121 contact: {
122 name: "Mike",
123 phone: "+353832224444",
124 email: "mike@gmail.com"
125 },
126 attr: [{
127 key: "Chef Instructions",
128 value: "No onions on burger please"
129 },
130 {
131 key: "Driver Instructions",
132 value: "Please do not ring the doorbell"
133 }
134 ],
135 tasks: [{
136 timeframe: {
137 by: 1578585415881
138 }
139 },
140 {
141 timeframe: {
142 by: 1578589038037
143 },
144 zone: {
145 name: "Customer",
146 address: deliveryAddress,
147 coords: {
148 lat: site.coords.lat,
149 lon: site.coords.lon
150 }
151 }
152 }
153 ]
154 }])
155 }
156 )
157 .then(res => res.json())
158 )
159}
160
161process().then(createdJob => console.log(createdJob))