Skip to content
This repository was archived by the owner on Oct 31, 2021. It is now read-only.

Commit 14538e2

Browse files
committed
Merge #342
342: (#341) Fixing transaction timezone issue. r=elliotcourant a=elliotcourant Co-authored-by: Elliot Courant <me@elliotcourant.dev>
2 parents 7b5a486 + e9e82be commit 14538e2

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

pkg/internal/myownsanity/strings.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,16 @@ func SliceContains(slice []string, item string) bool {
2121

2222
return false
2323
}
24+
25+
// StringPEqual will compare whether or not two string pointers are equal. If one or the other is nil then it will
26+
// return false. Otherwise it will compare their values as strings not as pointers.
27+
func StringPEqual(a, b *string) bool {
28+
if a == nil && b != nil {
29+
return false
30+
}
31+
if a != nil && b == nil {
32+
return false
33+
}
34+
35+
return *a == *b
36+
}

pkg/internal/myownsanity/strings_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,25 @@ func TestSliceContains(t *testing.T) {
3838
assert.True(t, SliceContains(data, "Item #1"), "should contain item #1")
3939
assert.False(t, SliceContains(data, "Item #3"), "should contain item #3")
4040
}
41+
42+
func TestStringPEqual(t *testing.T) {
43+
{
44+
var a, b string = "a", "b"
45+
assert.False(t, StringPEqual(&a, &b), "should not be equal")
46+
}
47+
48+
{
49+
var a, b string = "a", "a"
50+
assert.True(t, StringPEqual(&a, &b), "should be equal")
51+
}
52+
53+
{
54+
a := "a"
55+
assert.False(t, StringPEqual(&a, nil), "should not be equal")
56+
}
57+
58+
{
59+
b := "b"
60+
assert.False(t, StringPEqual(nil, &b), "should not be equal")
61+
}
62+
}

pkg/internal/platypus/transaction.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package platypus
22

33
import (
4+
"time"
5+
6+
"github.com/monetr/rest-api/pkg/util"
47
"github.com/pkg/errors"
58
"github.com/plaid/plaid-go/plaid"
6-
"time"
79
)
810

911
type Transaction interface {
@@ -83,7 +85,7 @@ func (p PlaidTransaction) GetDate() time.Time {
8385
}
8486

8587
func (p PlaidTransaction) GetDateLocal(timezone *time.Location) time.Time {
86-
return p.Date.In(timezone)
88+
return util.InLocal(p.Date, timezone)
8789
}
8890

8991
func (p PlaidTransaction) GetISOCurrencyCode() string {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package platypus
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/brianvoe/gofakeit/v6"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestPlaidTransaction_GetDates(t *testing.T) {
12+
transaction := PlaidTransaction{
13+
Amount: 128,
14+
BankAccountId: gofakeit.Generate("?????"),
15+
Category: []string{
16+
"Bank Fee",
17+
},
18+
Date: time.Date(2021, 9, 16, 0, 0, 0, 0, time.UTC),
19+
ISOCurrencyCode: "USD",
20+
UnofficialCurrencyCode: "USD",
21+
IsPending: false,
22+
MerchantName: "Arby's",
23+
Name: "Arby's",
24+
OriginalDescription: "ARBYS",
25+
PendingTransactionId: nil,
26+
TransactionId: gofakeit.Generate("??????????"),
27+
}
28+
29+
timezone, err := time.LoadLocation("America/Chicago")
30+
assert.NoError(t, err, "must retrieve timezone")
31+
assert.NotNil(t, timezone, "timezone cannot be nil")
32+
33+
t.Run("GetDate", func(t *testing.T) {
34+
assert.Equal(t,
35+
"2021-09-16T00:00:00Z", transaction.GetDate().Format(time.RFC3339Nano),
36+
"should match value without transforming timezone",
37+
)
38+
})
39+
40+
t.Run("GetDateLocal", func(t *testing.T) {
41+
assert.Equal(t,
42+
"2021-09-16T00:00:00-05:00", transaction.GetDateLocal(timezone).Format(time.RFC3339Nano),
43+
"should match value when transforming timezone",
44+
)
45+
})
46+
}

pkg/jobs/transactions.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package jobs
22

33
import (
44
"context"
5+
"time"
6+
57
"github.com/getsentry/sentry-go"
8+
"github.com/monetr/rest-api/pkg/internal/myownsanity"
69
"github.com/monetr/rest-api/pkg/internal/platypus"
710
"github.com/monetr/rest-api/pkg/models"
811
"github.com/monetr/rest-api/pkg/repository"
912
"github.com/sirupsen/logrus"
10-
"time"
1113
)
1214

1315
func (j *jobManagerBase) upsertTransactions(
@@ -93,8 +95,7 @@ func (j *jobManagerBase) upsertTransactions(
9395
shouldUpdate = true
9496
}
9597

96-
// This won't work quite right, I need to compare the values, not the pointers.
97-
if existingTransaction.PendingPlaidTransactionId != plaidTransaction.GetPendingTransactionId() {
98+
if !myownsanity.StringPEqual(existingTransaction.PendingPlaidTransactionId, plaidTransaction.GetPendingTransactionId()) {
9899
shouldUpdate = true
99100
}
100101

0 commit comments

Comments
 (0)