Skip to content

Commit e7e0e30

Browse files
authored
fix(import): negative amounts for YNAB4 import when importing split transactions (#464)
1 parent aec98f3 commit e7e0e30

File tree

4 files changed

+799
-713
lines changed

4 files changed

+799
-713
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ issues:
88
- gocyclo
99
- errcheck
1010
- dupl
11+
1112
# Parsing logic has high cyclomatic complexity. This is okay.
1213
- path: pkg/importer/parser/ynab4/parse.go
1314
linters:
1415
- gocyclo
1516
text: "func `parseTransactions`"
1617

18+
# Parsing logic has high cyclomatic complexity. This is okay.
19+
- path: pkg/importer/creator.go
20+
linters:
21+
- gocyclo
22+
text: "func `Create`"
23+
1724
linters:
1825
enable:
1926
- gocyclo

pkg/importer/creator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package importer
22

33
import (
4+
"errors"
5+
46
"github.com/envelope-zero/backend/pkg/importer/types"
57
"github.com/google/uuid"
68
"gorm.io/gorm"
@@ -61,6 +63,10 @@ func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) err
6163

6264
// Create transactions
6365
for _, r := range resources.Transactions {
66+
if r.Model.Amount.IsNegative() {
67+
return errors.New("a transaction to be imported has a negative amount, this is invalid")
68+
}
69+
6470
transaction := r.Model
6571
transaction.BudgetID = budget.ID
6672
transaction.SourceAccountID = resources.Accounts[r.SourceAccount].Model.ID

pkg/importer/parser/ynab4/parse.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,12 @@ func parseTransactions(resources *types.ParsedResources, transactions []Transact
319319
newTransaction.Envelope = mapping.Envelope
320320
newTransaction.Category = mapping.Category
321321
}
322-
newTransaction.Model.Amount = sub.Amount
322+
323+
if sub.Amount.IsPositive() {
324+
newTransaction.Model.Amount = sub.Amount
325+
} else {
326+
newTransaction.Model.Amount = sub.Amount.Neg()
327+
}
323328

324329
resources.Transactions = append(resources.Transactions, newTransaction)
325330
}

0 commit comments

Comments
 (0)