This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
106 lines (87 loc) · 2.94 KB
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"strings"
"time"
"github.com/Seklfreak/nordigen-lunchmoney-sync/lunchmoney"
"github.com/Seklfreak/nordigen-lunchmoney-sync/nordigen"
)
func createLunchmoneyTrx(
trx nordigen.Transaction,
account *nordigen.Account,
lunchmoneyAssetID int,
) (*lunchmoney.Transaction, error) {
payee := trx.CreditorName
if payee == "" {
payee = trx.DebtorName
}
date := trx.ValueDate
if time.Time(date).IsZero() {
date = trx.BookingDate
}
// for transfers from/to wallets using the personal account use OwnerName as payee (e.g. PayPal)
if (trx.AdditionalInformation == "MONEY_TRANSFER" ||
trx.ProprietaryBankTransactionCode == "TOPUP") &&
payee == "" && account.OwnerName != "" {
payee = account.OwnerName
}
// for exchange or transfer use the transaction code as payee
if (trx.ProprietaryBankTransactionCode == "EXCHANGE" ||
trx.ProprietaryBankTransactionCode == "TRANSFER") &&
payee == "" {
payee = strings.Title(strings.ToLower(trx.ProprietaryBankTransactionCode))
}
note := trx.RemittanceInformationUnstructured
if note == "" {
note = strings.Join(trx.RemittanceInformationUnstructuredArray, "; ")
}
transactionID := trx.TransactionID
if transactionID == "" {
// if API returns no external Transaction ID build new one out of hash of all information
transactionID = fmt.Sprintf(
"%s|%.2f%s|%s|%s|%s",
time.Time(trx.ValueDate),
trx.TransactionAmount.Amount,
trx.TransactionAmount.Currency,
trx.CreditorName,
trx.DebtorName,
note,
)
hasher := sha256.New()
hasher.Write([]byte(transactionID))
transactionID = base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}
lmTrx := &lunchmoney.Transaction{
AssetID: lunchmoneyAssetID,
Amount: float64(trx.TransactionAmount.Amount),
Currency: strings.ToLower(trx.TransactionAmount.Currency),
Date: lunchmoney.TransactionDate(date),
Payee: payee,
Notes: note,
Status: lunchmoney.TransactionStatusUncleared,
ExternalID: transactionID,
Tags: []string{"nordigen-lunchmoney-sync"},
}
if lmTrx.AssetID <= 0 {
return nil, fmt.Errorf("converting trx %s: lunchmoney transaction asset id cannot be empty", trx.TransactionID)
}
if lmTrx.Amount == 0 {
// TODO: return custom error type that can be ignored in the caller instead
return nil, nil // ignore transactions with 0 amount
}
if lmTrx.Currency == "" {
return nil, fmt.Errorf("converting trx %s: lunchmoney transaction currency cannot be empty", trx.TransactionID)
}
if time.Time(lmTrx.Date).IsZero() {
return nil, fmt.Errorf("converting trx %s: lunchmoney transaction date cannot be empty", trx.TransactionID)
}
if lmTrx.Payee == "" {
return nil, fmt.Errorf("converting trx %s: lunchmoney transaction payee cannot be empty", trx.TransactionID)
}
if lmTrx.ExternalID == "" {
return nil, fmt.Errorf("converting trx %s: lunchmoney transaction external ID cannot be empty", trx.TransactionID)
}
return lmTrx, nil
}