Last active
September 4, 2019 20:58
-
-
Save igorblumberg/b29697f7d32a8f64c26c8d6893ef04fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License (MIT) | |
Copyright (c) 2019 Camisa Dimona e Malhas LTDA | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
// | |
// This is a Google Script: https://script.google.com/ | |
// | |
//Change one cell content to display the error message | |
//Change the background color of another cell to show the run status (green = success ; yellow = running ; red = error) | |
function show_status(status,message) | |
{ | |
var spr = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spr.getSheetByName('orders') | |
var range = sheet.getRange('H2'); | |
var range2 = sheet.getRange('H3'); | |
var color = "#ffffff" | |
if(status == "running") | |
color = "#fcdf03" | |
else if(status == "success") | |
color = "#00bd19" | |
else if(status == "error") | |
color = "#ff0000" | |
range.setBackgroundColor(color); | |
if(message) | |
range2.setValue(message); | |
else | |
range2.setValue(""); | |
} | |
//Count how many orders we have | |
// | |
function countActiveRows() { | |
var spr = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spr.getSheetByName('orders') | |
var column = sheet.getRange('A:A'); | |
var values = column.getValues(); // get all data in one call | |
var ct = 0; | |
while ( values[ct][0] != "" ) { | |
ct++; | |
} | |
return ct -1; | |
} | |
//fetch settings from the "settings" tab | |
function fetchSettings() | |
{ | |
var spr = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spr.getSheetByName('settings'); | |
var settings = sheet.getRange('B1:B6').getValues(); | |
return settings; | |
} | |
//call Loggi API | |
function callLoggiQL(packages) | |
{ | |
var settings = fetchSettings(); | |
var loggi_url = "https://staging.loggi.com/graphql" | |
var gqlMutation = 'mutation createOrder($packages: [Packages]){ createOrder(input: { shopId: '+settings[5]+', pickups: [{address: {address: "'+settings[2]+'", complement: "'+settings[3]+'"}, instructions: "'+settings[4]+'"}], packages: $packages}) { success shop { pk name } orders { pk } errors { field message } }}' | |
var variables = {'packages': packages} | |
var data = { | |
'query':gqlMutation, | |
'variables': variables | |
} | |
var options = { | |
'method' : 'post', | |
'contentType': 'application/json', | |
'payload' : JSON.stringify(data), | |
'headers': {'Authorization': "ApiKey "+settings[1]+":"+settings[0]} | |
}; | |
var result = UrlFetchApp.fetch(loggi_url, options); | |
return result; | |
} | |
//entry point | |
function runLoggi() { | |
show_status("running");//set status as "running" | |
var number_rows = countActiveRows(); | |
var data = SpreadsheetApp.getSheetByName('orders').getRange(2,1,number_rows,6).getValues(); | |
var packages = []; | |
for each (var delivery in data) | |
{ | |
var temp = { | |
pickupIndex: 0, | |
recipient: {name: delivery[0], phone: delivery[1]}, | |
address: {address: delivery[2], complement: delivery[3]}, | |
dimensions: {width: 10, height: 10, length: 10, weight: parseInt(delivery[5]) * 250}, //todo: calculate the correct size based on how many t-shirts we are sending | |
instructions: delivery[4] | |
}; | |
packages.push(temp);//mount the packages payload | |
} | |
var result = JSON.parse(callLoggiQL(packages)); //make LOGGI API REQUEST | |
if(result.data.createOrder.success) | |
{ | |
show_status("success"); | |
SpreadsheetApp.getActiveSheet().getRange(2,1,number_rows,6).clear() | |
} | |
else | |
{ | |
var message = result.data.createOrder.errors[0].message | |
show_status("error",message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment