Skip to content

Commit 8bac16a

Browse files
authored
fix(importer/ynab-import): fix initial bugs (#682)
- return a list of TransactionCreate resources, not Transaction resources - name the TransactionCreate resource "transaction" in the list - only use positive amounts
1 parent 6763b97 commit 8bac16a

File tree

8 files changed

+28
-26
lines changed

8 files changed

+28
-26
lines changed

api/docs.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,13 +3213,13 @@
32133213
"type": "string"
32143214
}
32153215
},
3216-
"model": {
3217-
"$ref": "#/definitions/models.Transaction"
3218-
},
32193216
"sourceAccountName": {
32203217
"description": "Name of the source account if the ID is not known",
32213218
"type": "string",
32223219
"example": "Employer"
3220+
},
3221+
"transaction": {
3222+
"$ref": "#/definitions/models.TransactionCreate"
32233223
}
32243224
}
32253225
},

api/swagger.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ definitions:
258258
items:
259259
type: string
260260
type: array
261-
model:
262-
$ref: '#/definitions/models.Transaction'
263261
sourceAccountName:
264262
description: Name of the source account if the ID is not known
265263
example: Employer
266264
type: string
265+
transaction:
266+
$ref: '#/definitions/models.TransactionCreate'
267267
type: object
268268
models.AccountCreate:
269269
properties:

pkg/controllers/import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func duplicateTransactions(co Controller, transactions []importer.TransactionPre
248248
var duplicates []models.Transaction
249249
co.DB.Find(&duplicates, models.Transaction{
250250
TransactionCreate: models.TransactionCreate{
251-
ImportHash: transaction.Model.ImportHash,
251+
ImportHash: transaction.Transaction.ImportHash,
252252
},
253253
})
254254

pkg/controllers/import_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (suite *TestSuiteStandard) TestYnabImportPreviewDuplicateDetection() {
159159

160160
transaction := suite.createTestTransaction(models.TransactionCreate{
161161
SourceAccountID: account.Data.ID,
162-
ImportHash: preview.Data[0].Model.ImportHash,
162+
ImportHash: preview.Data[0].Transaction.ImportHash,
163163
Amount: decimal.NewFromFloat(1.13),
164164
})
165165

pkg/importer/parser/ynab-import/parse.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ func Parse(f io.Reader, account models.Account) ([]importer.TransactionPreview,
4444
}
4545

4646
t := importer.TransactionPreview{
47-
Model: models.Transaction{
48-
TransactionCreate: models.TransactionCreate{
49-
Date: date,
50-
ImportHash: helpers.Sha256String(strings.Join(record, ",")),
51-
Note: record[Memo],
52-
BudgetID: account.BudgetID,
53-
},
47+
Transaction: models.TransactionCreate{
48+
Date: date,
49+
ImportHash: helpers.Sha256String(strings.Join(record, ",")),
50+
Note: record[Memo],
51+
BudgetID: account.BudgetID,
5452
},
5553
}
5654

@@ -60,28 +58,28 @@ func Parse(f io.Reader, account models.Account) ([]importer.TransactionPreview,
6058
} else if record[Outflow] == "" && record[Inflow] == "" {
6159
return csvReadError(reader, errors.New("no amount is set for the transaction"))
6260
} else if record[Outflow] != "" {
63-
t.Model.TransactionCreate.SourceAccountID = account.DefaultModel.ID
61+
t.Transaction.SourceAccountID = account.DefaultModel.ID
6462
t.DestinationAccountName = record[Payee]
6563

6664
amount, err := decimal.NewFromString(record[Outflow])
6765
if err != nil {
6866
return csvReadError(reader, errors.New("outflow could not be parsed to a decimal"))
6967
}
7068

71-
t.Model.TransactionCreate.Amount = amount.Neg()
69+
t.Transaction.Amount = amount
7270
} else {
73-
t.Model.TransactionCreate.DestinationAccountID = account.DefaultModel.ID
71+
t.Transaction.DestinationAccountID = account.DefaultModel.ID
7472
t.SourceAccountName = record[Payee]
7573

7674
amount, err := decimal.NewFromString(record[Inflow])
7775
if err != nil {
7876
return csvReadError(reader, errors.New("inflow could not be parsed to a decimal"))
7977
}
8078

81-
t.Model.TransactionCreate.Amount = amount
79+
t.Transaction.Amount = amount
8280
}
8381

84-
if t.Model.TransactionCreate.Amount.IsZero() {
82+
if t.Transaction.Amount.IsZero() {
8583
return csvReadError(reader, errors.New("the amount for a transaction must not be 0"))
8684
}
8785

pkg/importer/parser/ynab-import/parse_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func TestParse(t *testing.T) {
3232
transactions, err := Parse(f, models.Account{})
3333
assert.Nil(t, err, "Parsing failed")
3434
assert.Len(t, transactions, tt.length, "Wrong number of transactions has been parsed")
35+
36+
for _, transaction := range transactions {
37+
assert.True(t, transaction.Transaction.Amount.IsPositive(), "Transaction amount is not positive: %s", transaction.Transaction.Amount)
38+
}
3539
})
3640
}
3741
}

pkg/importer/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ type Transaction struct {
4848

4949
// TransactionPreview is used to preview transactions that will be imported to allow for editing.
5050
type TransactionPreview struct {
51-
Model models.Transaction
52-
SourceAccountName string `json:"sourceAccountName" example:"Employer"` // Name of the source account if the ID is not known
53-
DestinationAccountName string `json:"destinationAccountName" example:"Deutsche Bahn"` // Name of the destination account if the ID is not known
54-
DuplicateTransactionIDs []uuid.UUID `json:"duplicateTransactionIds"` // IDs of transactions that this transaction duplicates
51+
Transaction models.TransactionCreate `json:"transaction"`
52+
SourceAccountName string `json:"sourceAccountName" example:"Employer"` // Name of the source account if the ID is not known
53+
DestinationAccountName string `json:"destinationAccountName" example:"Deutsche Bahn"` // Name of the destination account if the ID is not known
54+
DuplicateTransactionIDs []uuid.UUID `json:"duplicateTransactionIds"` // IDs of transactions that this transaction duplicates
5555
}

0 commit comments

Comments
 (0)