Skip to content

Commit

Permalink
Merge pull request #16 from starkbank/feature/approval-improvements
Browse files Browse the repository at this point in the history
Improve transfer approval experience and add clear approval orders bu…
  • Loading branch information
daltonfm-stark authored Jan 28, 2021
2 parents 7088606 + 2476ec4 commit 9b6b079
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 49 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 9 additions & 4 deletions src.vba/SendOrderForm.frm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Private Sub UserForm_Initialize()
End Sub

Private Sub SendButton_Click()
On Error Resume Next
Dim teamId As String
Dim orders As Collection
Dim respMessage As String
Expand All @@ -34,11 +33,18 @@ Private Sub SendButton_Click()
ActiveSheet.Cells(TableFormat.HeaderRow(), 1).Value = "Nome"
ActiveSheet.Cells(TableFormat.HeaderRow(), 2).Value = "CPF/CNPJ"
ActiveSheet.Cells(TableFormat.HeaderRow(), 3).Value = "Valor"
ActiveSheet.Cells(TableFormat.HeaderRow(), 4).Value = "Código do Banco"
ActiveSheet.Cells(TableFormat.HeaderRow(), 4).Value = "Código do Banco/ISPB"
ActiveSheet.Cells(TableFormat.HeaderRow(), 5).Value = "Agência"
ActiveSheet.Cells(TableFormat.HeaderRow(), 6).Value = "Conta"
ActiveSheet.Cells(TableFormat.HeaderRow(), 7).Value = "Tags"
ActiveSheet.Cells(TableFormat.HeaderRow(), 8).Value = "Descrição"
ActiveSheet.Cells(TableFormat.HeaderRow(), 9).Value = "externalId"

If Sheets("Credentials").Cells(7, 2) <> Date Then
ActiveSheet.Range("I10:I" & Rows.Count).ClearContents
End If

Sheets("Credentials").Cells(7, 2) = Date

Call FreezeHeader

Expand All @@ -50,8 +56,7 @@ Private Sub SendButton_Click()
Next
End With

Set orders = TeamGateway.getOrders()
respMessage = TeamGateway.createOrders(teamId, orders)
respMessage = TeamGateway.createOrders(teamId)

Unload Me

Expand Down
1 change: 1 addition & 0 deletions src.vba/SessionGateway.bas
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Public Sub saveSession(workspace As String, email As String, envString As String
Sheets("Credentials").Cells(5, 2) = memberName
Sheets("Credentials").Cells(6, 1) = "Workspace ID"
Sheets("Credentials").Cells(6, 2) = workspaceId
Sheets("Credentials").Cells(7, 1) = "Approval Date"

End Sub

Expand Down
134 changes: 95 additions & 39 deletions src.vba/TeamGateway.bas
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,33 @@ Public Function getTeams(cursor As String, optionalParam As Dictionary)

End Function

Public Function createOrders(teamId As String, orders As Collection)
Public Sub clearOrders()
Sheets("Transferências Com Aprovação").Range("A10:Z" & Rows.Count).ClearContents
End Sub

Public Function createOrders(teamId As String)
Dim orders As New Collection
Dim orderNumbers As New Collection
Dim externalIds As New Collection
Dim iteration As Integer
Dim startRow As Integer
Dim currentRow As Integer
Dim resp As response
Dim payload As String
Dim dict As New Dictionary
Dim returnMessage As String
Dim warningMessage As String
Dim errorMessage As String
Dim anySent As Boolean

dict.Add "teamId", teamId
dict.Add "orders", orders

payload = JsonConverter.ConvertToJson(dict)

Set resp = StarkBankApi.postRequest("/v1/team/order", payload, New Dictionary)

If resp.Status = 200 Then
createOrders = resp.json()("message")
MsgBox resp.json()("message"), , "Sucesso"

ElseIf resp.error().Exists("errors") Then
Dim errors As Collection: Set errors = resp.error()("errors")
Dim error As Dictionary
Dim errorList As String
Dim errorDescription As String

For Each error In errors
errorDescription = Utils.correctErrorLine(error("message"), TableFormat.HeaderRow() + 1)
errorList = errorList & errorDescription & Chr(10)
Next

Dim messageBox As String
messageBox = resp.error()("message") & Chr(10) & Chr(10) & errorList
MsgBox messageBox, , "Erro"

Else
MsgBox resp.error()("message"), , "Erro"

End If
anySent = False
returnMessage = ""
warningMessage = ""
errorMessage = ""

End Function

Public Function getOrders() As Collection
Dim orders As New Collection
dict.Add "teamId", teamId
iteration = 0
startRow = TableFormat.HeaderRow() + 1

For Each obj In SheetParser.dict
Dim amount As Long
Expand All @@ -79,6 +66,9 @@ Public Function getOrders() As Collection
Dim description As String
Dim order As Dictionary

iteration = iteration + 1
currentRow = TableFormat.HeaderRow() + iteration

If obj("Valor") = "" Then
MsgBox "Por favor, não deixe linhas em branco entre as ordens de transferência", , "Erro"
Unload SendOrderForm
Expand All @@ -87,17 +77,75 @@ Public Function getOrders() As Collection
amount = getAmountLong((obj("Valor")))
taxId = Trim(obj("CPF/CNPJ"))
name = Trim(obj("Nome"))
bankCode = Trim(obj("Código do Banco"))
bankCode = Trim(obj("Código do Banco/ISPB"))
branchCode = Trim(obj("Agência"))
accountNumber = Trim(obj("Conta"))
tags = Split(obj("Tags"), ",")
description = obj("Descrição")
externalId = obj("externalId")

Set order = TeamGateway.order(amount, taxId, name, bankCode, branchCode, accountNumber, tags, description)
orders.Add order
calculatedExternalId = calculateExternalId(amount, name, taxId, bankCode, branchCode, accountNumber)

If calculatedExternalId = externalId Then
warningMessage = "Aviso: Pedidos já enviados hoje não foram reenviados!" + Chr(10) + Chr(10)
Else
Set order = TeamGateway.order(amount, taxId, name, bankCode, branchCode, accountNumber, tags, description)
orders.Add order
orderNumbers.Add iteration
externalIds.Add calculatedExternalId
End If

If (iteration Mod 100) = 0 Or (currentRow >= ActiveSheet.Cells(Rows.Count, "A").End(xlUp).row) Then
If orderNumbers.Count = 0 Then
Set orders = Nothing
Set orderNumbers = Nothing
Set externalIds = Nothing
GoTo nextIteration
End If
dict.Add "orders", orders

payload = JsonConverter.ConvertToJson(dict)
Set resp = StarkBankApi.postRequest("/v1/team/order", payload, New Dictionary)
anySent = True

dict.Remove "orders"

If resp.Status = 200 Then
createOrders = resp.json()("message")
returnMessage = returnMessage + rowsMessage(startRow, currentRow) + resp.json()("message") + Chr(10)
Dim j As Integer
For j = 1 To externalIds.Count
ActiveSheet.Cells(TableFormat.HeaderRow() + orderNumbers.Item(j), 9).Value = externalIds.Item(j)
Next j

ElseIf resp.error().Exists("errors") Then
Dim errors As Collection: Set errors = resp.error()("errors")
Dim error As Dictionary
Dim errorDescription As String

For Each error In errors
errorDescription = Utils.correctErrorLine(error("message"), startRow)
errorMessage = errorMessage + errorDescription + Chr(10)
Next

Else
errorDescription = Utils.correctErrorLine(resp.error()("message"), startRow)
errorMessage = errorMessage + errorDescription + Chr(10)

End If
nextIteration:
startRow = currentRow + 1
Set orders = Nothing
Set orderNumbers = Nothing
Set externalIds = Nothing
End If

Next
Set getOrders = orders
If anySent Then
MsgBox warningMessage + Chr(10) + returnMessage + Chr(10) + errorMessage
Else
MsgBox "Todos os pedidos listados já foram enviados"
End If
End Function

Public Function order(amount As Long, taxId As String, name As String, bankCode As String, branchCode As String, accountNumber As String, tags() As String, description As String) As Dictionary
Expand Down Expand Up @@ -142,4 +190,12 @@ Public Function getOrdersByTransfer(cursor As String, optionalParam As Dictionar
MsgBox resp.error()("message"), , "Erro"
End If

End Function

Public Function calculateExternalId(amount As Long, name As String, taxId As String, bankCode As String, branchCode As String, accountNumber As String)
calculateExternalId = bankCode + branchCode + accountNumber + name + taxId + CStr(amount)
End Function

Public Function rowsMessage(startRow As Integer, currentRow As Integer)
rowsMessage = "Linhas " + CStr(startRow) + " a " + CStr(currentRow) + ": "
End Function
3 changes: 1 addition & 2 deletions src.vba/Utils.bas
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Public Function clearNonNumeric(text)
Set Reg = Nothing
End Function

Function correctErrorLine(errorMessage As String, offset As Integer) As String
Function correctErrorLine(ByVal errorMessage As String, offset As Integer) As String
Dim lineNumber As Integer
Dim message As String

Expand All @@ -124,7 +124,6 @@ Function correctErrorLine(errorMessage As String, offset As Integer) As String
message = M.submatches(2)
Next
End With

correctErrorLine = "Linha " & CStr(lineNumber + offset) & ": " & message

End Function
Expand Down
4 changes: 0 additions & 4 deletions src.vba/ViewHelpForm.frm
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ Private Sub HelpSandboxWebButton_Click()
ActiveWorkbook.FollowHyperlink address:="https://starkbank.com/sandbox"
End Sub

Private Sub Label2_Click()

End Sub

Private Sub SendPublicKeyButton_Click()
SendKeyForm.Show
End Sub
6 changes: 6 additions & 0 deletions src.vba/ViewStatementForm.frm
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,14 @@ Private Function getTransactionType(list() As String, ByRef teams As Collection,
transactionType = "Transferência interna"
Case "charge"
transactionType = "Recebimento de boleto pago"
Case "invoice"
transactionType = "Recebimento de cobrança Pix"
Case "deposit"
transactionType = "Recebimento de depósito Pix"
Case "charge-payment"
transactionType = "Pag. de boleto"
Case "brcode-payment"
transactionType = "Pag. de QR Code"
Case "bar-code-payment"
transactionType = "Pag. de imposto/concessionária"
Case "utility-payment"
Expand Down
Binary file modified starkbank-sdk.xlsm
Binary file not shown.

0 comments on commit 9b6b079

Please sign in to comment.