diff options
author | Martin Polden <mpolden@mpolden.no> | 2019-11-02 10:15:40 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2019-11-02 10:53:11 +0100 |
commit | 628a36233696f832b724eaf44a1f2aef16f68fa6 (patch) | |
tree | a1099df20eff5d29c467d6ac5ffdc3b8360481d7 | |
parent | 85d044f43c812c947eb9289bef39632dd793b388 (diff) |
Handle new Komplett transaction format
-rw-r--r-- | record/komplett/komplett.go | 22 | ||||
-rw-r--r-- | record/komplett/komplett_test.go | 38 | ||||
-rw-r--r-- | record/komplett/testdata/legacy.json | 122 | ||||
-rw-r--r-- | record/komplett/testdata/test.json | 138 |
4 files changed, 199 insertions, 121 deletions
diff --git a/record/komplett/komplett.go b/record/komplett/komplett.go index 5cb1d62..b12b151 100644 --- a/record/komplett/komplett.go +++ b/record/komplett/komplett.go @@ -14,9 +14,10 @@ const ( decimalSeparator = "." thousandSeparator = " " timeLayout = "02.01.2006" + withdrawalPrefix = "kr -" ) -// Reader implements a reader for Komplett-encoded (HTML or JSON) records. +// Reader implements a reader for Komplett-encoded (JSON) records. type Reader struct { rd io.Reader replacer *strings.Replacer @@ -27,9 +28,11 @@ type jsonTime time.Time type jsonAmount int64 type jsonRecord struct { - Time jsonTime `json:"FormattedPostingDate"` - Amount jsonAmount `json:"BillingAmount"` - Text string `json:"DisplayDescription"` + Time jsonTime `json:"FormattedPostingDate"` + BillingAmount jsonAmount `json:"BillingAmount"` + Amount jsonAmount `json:"Amount"` + FormattedAmount string `json:"FormattedAmount"` + Text string `json:"DisplayDescription"` } func (t *jsonTime) UnmarshalJSON(data []byte) error { @@ -79,10 +82,19 @@ func (r *Reader) Read() ([]record.Record, error) { } var rs []record.Record for _, jr := range jrs { + amount := jr.BillingAmount + if amount == 0 { // New format + amount = jr.Amount + // New format does not indicate whether transaction amount is positive or negative, so we guess + // based on the formatted field. + if strings.HasPrefix(jr.FormattedAmount, withdrawalPrefix) { + amount = -amount + } + } rs = append(rs, record.Record{ Time: time.Time(jr.Time), Text: jr.Text, - Amount: int64(jr.Amount), + Amount: int64(amount), }) } return rs, nil diff --git a/record/komplett/komplett_test.go b/record/komplett/komplett_test.go index d5abe3a..6141b8b 100644 --- a/record/komplett/komplett_test.go +++ b/record/komplett/komplett_test.go @@ -25,8 +25,8 @@ func testFile(t *testing.T, name string) *os.File { return f } -func TestRead(t *testing.T) { - f := testFile(t, "test.json") +func TestLegacyRead(t *testing.T) { + f := testFile(t, "legacy.json") defer f.Close() r := NewReader(f) @@ -60,3 +60,37 @@ func TestRead(t *testing.T) { } } } + +func TestRead(t *testing.T) { + f := testFile(t, "test.json") + defer f.Close() + + r := NewReader(f) + rs, err := r.Read() + if err != nil { + t.Fatal(err) + } + + var tests = []struct { + t time.Time + text string + amount int64 + }{ + {date(2019, 10, 30), "Varekjøp", -299000}, + {date(2019, 10, 30), "Uttak av bonus", 6000}, + } + if len(rs) != len(tests) { + t.Fatalf("want %d records, got %d", len(tests), len(rs)) + } + for i, tt := range tests { + if !rs[i].Time.Equal(tt.t) { + t.Errorf("#%d: want Time = %s, got %s", i, tt.t, rs[i].Time) + } + if rs[i].Text != tt.text { + t.Errorf("#%d: want Text = %s, got %s", i, tt.text, rs[i].Text) + } + if rs[i].Amount != tt.amount { + t.Errorf("#%d: want Amount = %d, got %d", i, tt.amount, rs[i].Amount) + } + } +} diff --git a/record/komplett/testdata/legacy.json b/record/komplett/testdata/legacy.json new file mode 100644 index 0000000..7d84ea9 --- /dev/null +++ b/record/komplett/testdata/legacy.json @@ -0,0 +1,122 @@ +[ + { + "MerchantID": null, + "MerchantAddress": null, + "MerchantCountry": null, + "PointCode": null, + "CanDefer": false, + "IsBonus": false, + "DisplayDescription": "Innskudd / Ekstra avdrag", + "TransactionDateTime": "/Date(1504224000000)/", + "BillingAmount": 42.42, + "BillingCurrencyCode": "NOK", + "MerchantCategoryCode": null, + "MerchantName": null, + "TerminalID": null, + "CardNumber": null, + "ApprovalCode": null, + "RetrievalReference": "1", + "TransactionID": "2", + "CorrelationID": "3", + "TransactionFile": null, + "TransactionRef": "4", + "PostingDate": "/Date(1504224000000)/", + "FormattedPostingDate": "01.09.2017", + "EventNarrative": "KID-innbetaling", + "Amount": 42.42, + "FormattedAmount": "42,42", + "CurrencyCode": "NOK", + "CreditOrDebit": 0, + "TransactionTypeCode": "42" + }, + { + "MerchantID": null, + "MerchantAddress": null, + "MerchantCountry": null, + "PointCode": null, + "CanDefer": false, + "IsBonus": false, + "DisplayDescription": "Innskudd / Ekstra avdrag", + "TransactionDateTime": "/Date(1501545600000)/", + "BillingAmount": 1337, + "BillingCurrencyCode": "NOK", + "MerchantCategoryCode": null, + "MerchantName": null, + "TerminalID": null, + "CardNumber": null, + "ApprovalCode": null, + "RetrievalReference": "1", + "TransactionID": "2", + "CorrelationID": "3", + "TransactionFile": null, + "TransactionRef": "4", + "PostingDate": "/Date(1501545600000)/", + "FormattedPostingDate": "01.08.2017", + "EventNarrative": "KID-innbetaling", + "Amount": 1337, + "FormattedAmount": "1337,00", + "CurrencyCode": "NOK", + "CreditOrDebit": 0, + "TransactionTypeCode": "42" + }, + { + "MerchantID": null, + "MerchantAddress": null, + "MerchantCountry": null, + "PointCode": null, + "CanDefer": false, + "IsBonus": false, + "DisplayDescription": "Varekjøp", + "TransactionDateTime": "/Date(1535760000000)/", + "BillingAmount": -500, + "BillingCurrencyCode": "NOK", + "MerchantCategoryCode": null, + "MerchantName": null, + "TerminalID": null, + "CardNumber": null, + "ApprovalCode": null, + "RetrievalReference": "1", + "TransactionID": "2", + "CorrelationID": "3", + "TransactionFile": null, + "TransactionRef": "4", + "PostingDate": "/Date(1535760000000)/", + "FormattedPostingDate": "01.09.2018", + "EventNarrative": "KID-innbetaling", + "Amount": 500, + "FormattedAmount": "-500,00", + "CurrencyCode": "NOK", + "CreditOrDebit": 0, + "TransactionTypeCode": "42" + }, + { + "MerchantID": null, + "MerchantAddress": null, + "MerchantCountry": null, + "PointCode": null, + "CanDefer": false, + "IsBonus": false, + "DisplayDescription": "Varekjøp", + "TransactionDateTime": "/Date(1543622400000)/", + "BillingAmount": -997.4, + "BillingCurrencyCode": "NOK", + "MerchantCategoryCode": null, + "MerchantName": null, + "TerminalID": null, + "CardNumber": null, + "ApprovalCode": null, + "RetrievalReference": "1", + "TransactionID": "2", + "CorrelationID": "3", + "TransactionFile": null, + "TransactionRef": "4", + "PostingDate": "/Date(1543622400000)/", + "FormattedPostingDate": "01.12.2018", + "EventNarrative": "KID-innbetaling", + "Amount": 997.4, + "FormattedAmount": "-997,40", + "CurrencyCode": "NOK", + "CreditOrDebit": 0, + "TransactionTypeCode": "42" + } +] diff --git a/record/komplett/testdata/test.json b/record/komplett/testdata/test.json index 7d84ea9..9f9e577 100644 --- a/record/komplett/testdata/test.json +++ b/record/komplett/testdata/test.json @@ -1,122 +1,32 @@ [ { - "MerchantID": null, - "MerchantAddress": null, - "MerchantCountry": null, - "PointCode": null, - "CanDefer": false, - "IsBonus": false, - "DisplayDescription": "Innskudd / Ekstra avdrag", - "TransactionDateTime": "/Date(1504224000000)/", - "BillingAmount": 42.42, - "BillingCurrencyCode": "NOK", - "MerchantCategoryCode": null, - "MerchantName": null, - "TerminalID": null, - "CardNumber": null, - "ApprovalCode": null, - "RetrievalReference": "1", - "TransactionID": "2", - "CorrelationID": "3", - "TransactionFile": null, - "TransactionRef": "4", - "PostingDate": "/Date(1504224000000)/", - "FormattedPostingDate": "01.09.2017", - "EventNarrative": "KID-innbetaling", - "Amount": 42.42, - "FormattedAmount": "42,42", - "CurrencyCode": "NOK", - "CreditOrDebit": 0, - "TransactionTypeCode": "42" - }, - { - "MerchantID": null, - "MerchantAddress": null, - "MerchantCountry": null, - "PointCode": null, - "CanDefer": false, - "IsBonus": false, - "DisplayDescription": "Innskudd / Ekstra avdrag", - "TransactionDateTime": "/Date(1501545600000)/", - "BillingAmount": 1337, - "BillingCurrencyCode": "NOK", - "MerchantCategoryCode": null, - "MerchantName": null, - "TerminalID": null, - "CardNumber": null, - "ApprovalCode": null, - "RetrievalReference": "1", - "TransactionID": "2", - "CorrelationID": "3", - "TransactionFile": null, - "TransactionRef": "4", - "PostingDate": "/Date(1501545600000)/", - "FormattedPostingDate": "01.08.2017", - "EventNarrative": "KID-innbetaling", - "Amount": 1337, - "FormattedAmount": "1337,00", - "CurrencyCode": "NOK", - "CreditOrDebit": 0, - "TransactionTypeCode": "42" - }, - { - "MerchantID": null, - "MerchantAddress": null, - "MerchantCountry": null, - "PointCode": null, - "CanDefer": false, - "IsBonus": false, + "TransactionId": null, + "TransactionRef": null, "DisplayDescription": "Varekjøp", - "TransactionDateTime": "/Date(1535760000000)/", - "BillingAmount": -500, - "BillingCurrencyCode": "NOK", - "MerchantCategoryCode": null, - "MerchantName": null, - "TerminalID": null, - "CardNumber": null, - "ApprovalCode": null, - "RetrievalReference": "1", - "TransactionID": "2", - "CorrelationID": "3", - "TransactionFile": null, - "TransactionRef": "4", - "PostingDate": "/Date(1535760000000)/", - "FormattedPostingDate": "01.09.2018", - "EventNarrative": "KID-innbetaling", - "Amount": 500, - "FormattedAmount": "-500,00", - "CurrencyCode": "NOK", - "CreditOrDebit": 0, - "TransactionTypeCode": "42" + "EventNarrative": " ", + "DestinationAccountNumber": null, + "Amount": 2990.0, + "PostingDate": "/Date(1572390000000)/", + "CanDefer": false, + "IsDeferred": false, + "DefermentPeriod": null, + "FormattedAmount": "kr -2 990,00", + "FormattedPostingDate": "30.10.2019", + "FormattedDefermentExpiryDate": null }, { - "MerchantID": null, - "MerchantAddress": null, - "MerchantCountry": null, - "PointCode": null, + "TransactionId": null, + "TransactionRef": null, + "DisplayDescription": "Uttak av bonus", + "EventNarrative": " ", + "DestinationAccountNumber": null, + "Amount": 60.0, + "PostingDate": "/Date(1572390000000)/", "CanDefer": false, - "IsBonus": false, - "DisplayDescription": "Varekjøp", - "TransactionDateTime": "/Date(1543622400000)/", - "BillingAmount": -997.4, - "BillingCurrencyCode": "NOK", - "MerchantCategoryCode": null, - "MerchantName": null, - "TerminalID": null, - "CardNumber": null, - "ApprovalCode": null, - "RetrievalReference": "1", - "TransactionID": "2", - "CorrelationID": "3", - "TransactionFile": null, - "TransactionRef": "4", - "PostingDate": "/Date(1543622400000)/", - "FormattedPostingDate": "01.12.2018", - "EventNarrative": "KID-innbetaling", - "Amount": 997.4, - "FormattedAmount": "-997,40", - "CurrencyCode": "NOK", - "CreditOrDebit": 0, - "TransactionTypeCode": "42" + "IsDeferred": false, + "DefermentPeriod": null, + "FormattedAmount": "kr 60,00", + "FormattedPostingDate": "30.10.2019", + "FormattedDefermentExpiryDate": null } ] |