Skip to content

Commit caefd31

Browse files
committed
Fix download transfer
1 parent 414a274 commit caefd31

5 files changed

+91
-49
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Given a version number MAJOR.MINOR.PATCH, increment:
1515
## [Unreleased]
1616
### Fixed
1717
- paymentRequest duplicated payment
18+
- download transfer locally
1819

1920
## [0.6.5] - 2024-06-28
2021
### Added

src/.clasp.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"scriptId":"1JAupf57mwkjPaC3FFC8Ydx-yo6LkoIjnIrpQx2_95JenFfQ0G2ov4rbO","rootDir":"/Users/luis.almeida/Documents/starkbank/google-sheets/src"}
1+
{"scriptId":"199zM22Q3FVHJK5EOZtrz7EQ7bH5wm_vnQCu-YVI7kOz6N455TDq-XmMq"}

src/FormDownloadTransfers.html

+27-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
1010
</head>
1111
<body>
12-
12+
<div id="alert" class="alert alert-dismissible fade show alert-danger front" role="alert" style="padding-bottom: 10px;">
13+
<a id="alert-message"></a>
14+
<button type="button" class="close" onclick="hideAlert()" aria-label="Close">
15+
<span aria-hidden="true">&times;</span>
16+
</button>
17+
</div>
1318
<div class="d-flex justify-content-center">
1419
<div class="spinner-border text-primary" role="status">
1520
<span class="sr-only">Aguarde enquanto os arquivos são baixados...</span>
@@ -20,15 +25,18 @@
2025

2126
<script>
2227
var downloaded = 0;
28+
var items = 0;
2329

2430
function onLoad(){
31+
$("#alert").hide();
2532
google.script.run.withSuccessHandler(downloadAll)
2633
.withFailureHandler(onFailure)
2734
.getTransferDownloadList();
2835
}
2936

3037
function downloadAll(result){
3138
for (let id of result){
39+
items = result.length
3240
google.script.run.withSuccessHandler(pdfSave)
3341
.withFailureHandler(onFailure)
3442
.TransferDownloadBase64Encoded(id);
@@ -51,13 +59,18 @@
5159

5260
document.body.removeChild(element);
5361
downloaded += 1;
54-
// if (downloaded >= 30) {
55-
// setTimeout(google.script.host.close, 20000);
56-
// }
62+
63+
if (downloaded >= items) {
64+
setTimeout(google.script.host.close, 10000);
65+
}
5766
}
5867

5968
function onFailure(error) {
6069
console.log(error)
70+
$("#alert").show();
71+
var alertMessage = document.getElementById("alert-message");
72+
alertMessage.innerHTML = "Não foi possivel realizar o download dos itens"
73+
stopLoading()
6174
}
6275

6376
function save() {
@@ -66,6 +79,16 @@
6679
.withFailureHandler(onFailure)
6780
.SendOrder(teamSelected);
6881
}
82+
83+
function startLoading() {
84+
$('.btn').hide();
85+
$('.spinner-border').show();
86+
}
87+
88+
function stopLoading() {
89+
$('.btn').show();
90+
$('.spinner-border').hide();
91+
}
6992

7093
window.onload = onLoad;
7194
</script>

src/sdkViewTransfer.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,13 @@ function getTransferDownloadList(){
6666
break;
6767
}
6868
}
69-
if (idList.length == 0) {
70-
Browser.msgBox("Nenhuma transferência válida (sucesso) para download listada.")
71-
}
7269
return idList;
7370
}
7471

7572

7673
function TransferDownload(id) {
7774
let path = "/transfer/" + id + "/pdf";
78-
let pdfContent = fetchBuffer(path)[0];
75+
let pdfContent = fetchBuffer(path);
7976
return pdfContent;
8077
}
8178

@@ -122,6 +119,12 @@ function TransferDownloadAllLocal(){
122119

123120
function TransferDownloadBase64Encoded(id){
124121
let blob = TransferDownload(id);
122+
blob = blob[0]
123+
let status = blob[1]
124+
125+
if (status != 200) {
126+
throw new Error(blob[0])
127+
}
125128
return {
126129
id: id,
127130
content: Utilities.base64Encode(blob.getBytes())

src/utilsRequest.js

+55-40
Original file line numberDiff line numberDiff line change
@@ -102,65 +102,80 @@ function parseResponse(responseApi) {
102102
}
103103

104104

105-
function fetchBuffer(path, method = 'GET', payload = null, query = null, version = 'v2') {
105+
function fetchBuffer(path, method='GET', payload=null, query=null, version="v2", environment=null, privateKeyPem=null, challengeId=null) {
106106
let user = new getDefaultUser();
107-
let hostname = getHostname(user.environment.toLowerCase(), version);
108-
let options = {method: method};
107+
if (!user.privateKey) {
108+
throw JSON.stringify({"message": "Erro de autenticação! Por favor, faça login novamente."});
109+
}
110+
if (!environment) {
111+
environment = environment || user.environment.toLowerCase();
112+
}
113+
let hostname = getHostname(environment, version);
114+
let options = {
115+
method: method,
116+
muteHttpExceptions: true,
117+
};
109118
let url = hostname + path;
110119

111120
if (query) {
112-
let queryString = '';
113-
let separator = '?';
114-
for (let key in query) {
115-
if (query[key]) {
116-
queryString += separator + key + '=' + query[key];
117-
separator = '&';
121+
let queryString = '';
122+
let separator = '?';
123+
for (let key in query) {
124+
if (query[key]) {
125+
queryString += separator + key + '=' + query[key];
126+
separator = '&';
127+
}
118128
}
119-
}
120-
url += queryString;
129+
url += queryString;
121130
}
122131

123-
paths = ["/session", "/boleto-payment"]
124-
125-
if (paths.includes(path) && method != "GET") {
126-
var accessId = KeyGen.generateMemberAccessId(user.workspaceId, user.email)
127-
} else {
128-
var accessId = user.accessId;
129-
}
130-
131-
let accessTime = Math.round((new Date()).getTime() / 1000).toString();
132-
options['headers'] = {
133-
'Access-Id': accessId,
134-
'User-Agent': 'GoogleSheets-SDK-0.4.3',
135-
'Accept-Language': 'pt-BR',
136-
'Content-Type': 'application/json',
137-
'Access-Time': accessTime
138-
};
139-
140-
let body = JSON.stringify(payload);
141-
if (!payload) {
142-
body = "";
143-
}
132+
if (privateKeyPem) {
133+
var accessId = KeyGen.generateMemberAccessId(user.workspaceId, user.email)
134+
} else {
135+
var accessId = user.accessId;
136+
}
144137

145-
options['payload'] = body;
146-
147138
if (!privateKeyPem) {
148-
var privateKeyPem = user.privateKey;
139+
var privateKeyPem = user.privateKey;
149140
}
150-
151-
let message = accessId + ':' + accessTime + ':' + body;
141+
142+
let accessTime = Math.round((new Date()).getTime() / 1000).toString();
143+
options['headers'] = {
144+
'Access-Id': accessId,
145+
'User-Agent': 'App-StarkBank-GSheets-v0.6.5b',
146+
'User-Agent-Override': 'App-StarkBank-GSheets-v0.6.5b',
147+
'PlatFormId' : 'gsheets',
148+
'PlatFormVersion' : '0.6.5',
149+
'Accept-Language': 'pt-BR',
150+
'Content-Type': 'application/pdf',
151+
'Access-Time': accessTime
152+
};
153+
154+
let body = ""
155+
if (payload) {
156+
body = payload;
157+
}
158+
159+
options['payload'] = body;
160+
161+
let message = accessId + ':' + accessTime + ':' + body
162+
163+
if (challengeId) {
164+
message += ":" + challengeId
165+
options['headers']['Access-Challenge-Ids'] = challengeId
166+
}
167+
152168
let signature = easySign(message, privateKeyPem);
153169
options['headers']['Access-Signature'] = signature;
154170

155171
response = UrlFetchApp.fetch(url, options);
156172

157-
let content = response.getAs("application/pdf");
158173
let status = response.getResponseCode();
159174

160175
if (status != 200) {
161-
Browser.msgBox(parseResponse(response)[0]["errors"][0]["message"])
162-
return new Error()
176+
return [parseResponse(response)[0]["errors"][0]["code"], status]
163177
} else {
178+
let content = response.getAs("application/pdf");
164179
return [content, status]
165180
}
166181
}

0 commit comments

Comments
 (0)