· 5 years ago · Jul 17, 2020, 01:38 AM
1/**
2 * @sendle
3 * Uses sendle API to create orders and get quotes.
4 * [credentials] = { api_key: "2i213n12ina", sendle_id: "test"}
5 * [command] = get_quote, create_order, track_order
6 */
7
8const axios = require('axios');
9
10console.log(process.argv.length);
11
12if (process.argv.length == 2) {
13 console.log("Usage: node sendle.js [command] [API Key] [JSONofData]");
14 process.exit(1);
15} else if (process.argv.length < 5) {
16 console.log("Too few arguments. Usage: node sendle.js [command] [credentials] [JSONofData]");
17 process.exit(1);
18} else if (process.argv.length > 5) {
19 console.log("Too many arguments. Usage: node sendle.js [command] [credentials] [JSONofData]");
20 process.exit(1);
21}
22
23const command = process.argv[2];
24const apiKey = process.argv[3];
25const credentials = JSON.parse(process.argv[3]);
26let data = process.argv[4];
27
28try {
29 if(JSON.parse(data)) data = JSON.parse(data);
30} catch (error) {
31 console.log(error);
32 console.log("Error reading the JSON input. Input JSON data must be in valid serialized JSON format");
33 process.exit(1);
34}
35
36// define base url
37const url = "https://sandbox.sendle.com/api";
38
39const choice = {
40
41 // Input { "pickup_suburb": "", "pickup_postcode": ""}
42 // '{"postcode":"2000", "suburb":"", "state":"", "weight":"", "qty":"1"}'
43 // Output
44 // {[
45 // {type: "standard", price: "20.85", : days: "", eta: "12/08/2019", window: ""}
46 // {type: "express", price: "24.85", days: "4", eta: "", window: ""}
47 // {type: "express", price: "62.85", days: "", eta: "", window: "7pm to 10pm"}
48 // ]}
49 "get_quote": async data => {
50
51 console.log('Getting quotes');
52
53 const quoteData = {
54 "pickup_suburb": data.pickup_suburb,
55 "pickup_postcode": data.pickup_postcode,
56 "delivery_suburb": data.delivery_suburb,
57 "delivery_postcode": data.delivery_postcode,
58 "weight_value": data.weight_value,
59 "weight_units": data.weight_units
60 }
61
62 // Define variable for returning JSON
63 let quoteReturn;
64 let response = [];
65
66 // Define error handler with messages
67 let errorHandler = {
68 pickup_postcode: "",
69 pickup_suburb: "",
70 delivery_suburb: "",
71 delivery_postcode: "",
72 weight_value: "",
73 weight_units: "",
74 }
75
76 try {
77 quoteReturn = await axios.get(`${url}/quote`, {
78 headers: {
79 "Authorization": `${credentials.sendle_id}:${credentials.api_key}`,
80 "Content-Type": "application/json"
81 },
82 data: JSON.stringify(quoteData)
83 })
84
85 // Restructure object and return
86
87 quoteReturn.data.forEach(r => {
88 response.push({ type: r.plan_name, price: r.quote.gross.amount, days: r.eta.days_range, eta: r.eta.for_pickup_date })
89 })
90
91 console.log(response);
92
93 return;
94
95 } catch (error) {
96 const errorData = error.response.data.messages
97 console.log("Error: ", error.response.data.messages);
98
99 if(error.response.status === 422) {
100 errorHandler.pickup_suburb = errorData.pickup_suburb ? errorData.pickup_suburb : null;
101 errorHandler.pickup_postcode = errorData.pickup_postcode ? errorData.pickup_postcode : null;
102 errorHandler.delivery_suburb = errorData.delivery_suburb ? errorData.delivery_suburb : null;
103 errorHandler.delivery_postcode = errorData.delivery_postcode ? errorData.delivery_postcode : null;
104 errorHandler.weight_units = errorData.weight_units ? errorData.delivery_postcode : null;
105 }
106
107 errorHandler = Object.entries(errorHandler).filter(error => {
108 return error[1] !== null
109 })
110
111 return errorHandler;
112 }
113
114 },
115 "create_order": async data => {
116 // Accepts JSON as follows:
117 // '{ "pickup_date": "2020/07/30", "description": "test", "description": "76 Myrtle Creek Avenue",
118 // "first_mile_option": "drop_off" || "pickup"
119 // "weight": "1", "volume"; "0.01"
120 // "sender": { "name": "", "phone": "", "company": "", "address": "", "suburb": "", "state": "", "postcode": "", "country": "", "instructions": ""}
121 // "receiver": { "name": "", "phone": "", "company": "", "address": "", "suburb": "", "state": "", "postcode": "", "country": "", "instructions": ""}
122
123 console.log("Creating order...");
124
125 const orderData = {
126 "pickup_date": data.pickup_date ? data.pickup_date : "",
127 "first_mile_option": "pickup",
128 "description": data.description ? data.description : "",
129 "weight": {"value": data.weight ? data.weight : "", "units": "kg"},
130 "volume": {"value": data.volume ? data.volume : "", "units": "m3"},
131 "sender": {
132 "contact": {
133 "name": data.sender?.name ? data.sender?.name : "",
134 "phone": data.sender?.phone ? data.sender?.phone : "",
135 "company": data.sender?.company ? data.sender?.company : ""
136 },
137 "address": {
138 "address_line1": data.sender?.address ? data.sender?.address : "",
139 "suburb": data.sender?.suburb ? data.sender?.suburb : "",
140 "state_name": data.sender?.state ? data.sender?.state : "",
141 "postcode": data.sender?.postcode ? data.sender?.postcode : "",
142 "country": data.sender?.country ? data.sender.country : "Australia"
143 },
144 "instructions": data.sender?.instructions ? data.sender.instructions : ""
145 },
146 "receiver": {
147 "contact": {
148 "name": data.receiver?.name ? data.receiver.name : "",
149 "email": data.receiver?.email ? data.receiver.email : "",
150 "company": data.receiver?.company ? data.receiver.company : ""
151 },
152 "address": {
153 "address_line1": data.receiver?.address_line ? data.receiver.address_line : "",
154 "suburb": data.receiver?.suburb ? data.receiver.suburb : "",
155 "state_name": data.receiver?.state ? data.receiver.state : "",
156 "postcode": data.receiver?.postcode ? data.receiver.postcode : "",
157 "country": data.receiver?.country ? data.receiver.country : "Australia"
158 },
159 "instructions": data.receiver?.instructions ? data.receiver.instructions : ""
160 }
161 }
162
163 // Define variable for returning JSON
164 let orderReturn;
165
166 try {
167 orderReturn = await axios.post(`${url}/orders`, {
168 headers: {
169 "Content-Type": "application/json",
170 // "Authorization": Buffer.from(`${credentials.sendle_id}:${credentials.api_key}`, 'utf8').toString('base64')
171 },
172 data: JSON.stringify(orderData)
173 }, {
174 auth: {
175 username: credentials.sendle_id,
176 password: credentials.api_key
177 }
178 })
179
180 console.log(orderReturn.data);
181
182 return orderReturn.data;
183
184 } catch (error) {
185 console.log(JSON.stringify(error.response.data));
186
187 return JSON.stringify(error.response.data);
188 }
189
190 },
191 "track_order": async data => {
192
193 // Input
194 // '{ order_id: "S3ND73" }'
195 // Output
196 // { state: "Delivered", "tracking_events:
197 // [ { "event_type": "Pickup", "description": "Parcel picked up"}, {
198 // { "event_type": "Pickup", "description": "Parcel picked up"}
199 // } ]
200 // "}
201
202 console.log('Getting track order...');
203
204 const order = data.order_id;
205
206 try {
207 const trackingData = await axios.get(`${url}/tracking/${order}`, {
208 headers: {
209 "Authorization": `${credentials.sendle_id}:${credentials.api_key}`,
210 "Content-Type": "application/json"
211 }
212 })
213
214 if(trackingData) {
215 console.log(JSON.stringify(trackingData));
216 }
217 } catch (error) {
218 console.log(JSON.stringify(error));
219
220 }
221
222 },
223 "get_label": async data => {
224 // Input '{ tracking_number: "f5233746-71d4-4b05-bf63-56f4abaed5f6" }'
225 let labelReturn;
226
227 try {
228 if(data.tracking_number) {
229 labelReturn = await axios.get(`${url}/orders/${data.tracking_number}/labels/a4.pdf`, {
230 headers: {
231 "Content-Type": "application/json",
232 }
233 }, {
234 auth: {
235 username: credentials.sendle_id,
236 password: credentials.api_key
237 }
238 })
239
240 console.log(labelReturn.data);
241
242 return labelReturn.data;
243 }
244
245 } catch (error) {
246 console.log(JSON.stringify(error.response.data));
247
248 return JSON.stringify(error.response.data);
249 }
250
251 },
252 "view_order": async data => {
253 // Input '{ tracking_number: "f5233746-71d4-4b05-bf63-56f4abaed5f6" }'
254 let labelReturn;
255
256 try {
257 if(data.tracking_number) {
258 labelReturn = await axios.get(`${url}/orders/${data.tracking_number}`, {
259 headers: {
260 "Content-Type": "application/json",
261 }
262 }, {
263 auth: {
264 username: credentials.sendle_id,
265 password: credentials.api_key
266 }
267 })
268
269 console.log(labelReturn.data);
270
271 return labelReturn.data;
272 }
273
274 } catch (error) {
275 console.log(JSON.stringify(error.response.data));
276
277 return JSON.stringify(error.response.data);
278 }
279 }
280}
281
282if (command in choice) {
283 choice[command](data);
284}
285else {
286 choice['default']();
287}