Skip to content

Commit

Permalink
feat(payments): use time lib for storage package (#1725)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Sep 26, 2024
1 parent c0d20f5 commit 615ddd8
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 53 deletions.
2 changes: 1 addition & 1 deletion components/payments/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.7
require (
github.com/ThreeDotsLabs/watermill v1.3.7
github.com/bombsimon/logrusr/v3 v3.1.0
github.com/formancehq/go-libs v1.7.2-0.20240925132527-7627842ea9b5
github.com/formancehq/go-libs v1.7.2-0.20240926150224-306278b90e0c
github.com/formancehq/payments/genericclient v0.0.0-00010101000000-000000000000
github.com/gibson042/canonicaljson-go v1.0.3
github.com/go-chi/chi/v5 v5.1.0
Expand Down
4 changes: 2 additions & 2 deletions components/payments/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/formancehq/go-libs v1.7.2-0.20240925132527-7627842ea9b5 h1:6UcoXXm5hzFk7c2aOonPUPO3bNrU7CvTiE/nZQWMvY4=
github.com/formancehq/go-libs v1.7.2-0.20240925132527-7627842ea9b5/go.mod h1:ynmWBbsdhVyjE+MxneMErtgd/RnNAk892VuIhZE2fps=
github.com/formancehq/go-libs v1.7.2-0.20240926150224-306278b90e0c h1:fSPJ3x4ofe+9bnrS1pr1+9r0SAaxhJ5Jw4k4wKEUTwk=
github.com/formancehq/go-libs v1.7.2-0.20240926150224-306278b90e0c/go.mod h1:ynmWBbsdhVyjE+MxneMErtgd/RnNAk892VuIhZE2fps=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/gibson042/canonicaljson-go v1.0.3 h1:EAyF8L74AWabkyUmrvEFHEt/AGFQeD6RfwbAuf0j1bI=
Expand Down
6 changes: 3 additions & 3 deletions components/payments/internal/storage/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/pkg/errors"
"github.com/uptrace/bun"
Expand Down Expand Up @@ -161,7 +161,7 @@ func fromAccountModels(from models.Account) account {
return account{
ID: from.ID,
ConnectorID: from.ConnectorID,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: time.New(from.CreatedAt),
Reference: from.Reference,
Type: string(from.Type),
DefaultAsset: from.DefaultAsset,
Expand All @@ -176,7 +176,7 @@ func toAccountModels(from account) models.Account {
ID: from.ID,
ConnectorID: from.ConnectorID,
Reference: from.Reference,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: from.CreatedAt.Time,
Type: models.AccountType(from.Type),
Name: from.Name,
DefaultAsset: from.DefaultAsset,
Expand Down
17 changes: 9 additions & 8 deletions components/payments/internal/storage/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/pointer"
internalTime "github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/pkg/errors"
"github.com/uptrace/bun"
Expand All @@ -17,13 +18,13 @@ type balance struct {
bun.BaseModel `bun:"table:balances"`

// Mandatory fields
AccountID models.AccountID `bun:"account_id,pk,type:character varying,notnull"`
CreatedAt time.Time `bun:"created_at,pk,type:timestamp without time zone,notnull"`
Asset string `bun:"asset,pk,type:text,notnull"`
AccountID models.AccountID `bun:"account_id,pk,type:character varying,notnull"`
CreatedAt internalTime.Time `bun:"created_at,pk,type:timestamp without time zone,notnull"`
Asset string `bun:"asset,pk,type:text,notnull"`

ConnectorID models.ConnectorID `bun:"connector_id,type:character varying,notnull"`
Balance *big.Int `bun:"balance,type:numeric,notnull"`
LastUpdatedAt time.Time `bun:"last_updated_at,type:timestamp without time zone,notnull"`
LastUpdatedAt internalTime.Time `bun:"last_updated_at,type:timestamp without time zone,notnull"`
}

func (s *store) BalancesUpsert(ctx context.Context, balances []models.Balance) error {
Expand Down Expand Up @@ -245,11 +246,11 @@ func fromBalancesModels(from []models.Balance) []balance {
func fromBalanceModels(from models.Balance) balance {
return balance{
AccountID: from.AccountID,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: internalTime.New(from.CreatedAt),
Asset: from.Asset,
ConnectorID: from.AccountID.ConnectorID,
Balance: from.Balance,
LastUpdatedAt: from.LastUpdatedAt.UTC(),
LastUpdatedAt: internalTime.New(from.LastUpdatedAt),
}
}

Expand All @@ -264,9 +265,9 @@ func toBalancesModels(from []balance) []models.Balance {
func toBalanceModels(from balance) models.Balance {
return models.Balance{
AccountID: from.AccountID,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: from.CreatedAt.Time,
Asset: from.Asset,
Balance: from.Balance,
LastUpdatedAt: from.LastUpdatedAt.UTC(),
LastUpdatedAt: from.LastUpdatedAt.Time,
}
}
10 changes: 5 additions & 5 deletions components/payments/internal/storage/bank_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package storage
import (
"context"
"fmt"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
"github.com/pkg/errors"
Expand Down Expand Up @@ -264,7 +264,7 @@ func (s *store) BankAccountsDeleteRelatedAccountFromConnectorID(ctx context.Cont
func fromBankAccountModels(from models.BankAccount) bankAccount {
ba := bankAccount{
ID: from.ID,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: time.New(from.CreatedAt),
Name: from.Name,
Country: from.Country,
Metadata: from.Metadata,
Expand Down Expand Up @@ -294,7 +294,7 @@ func fromBankAccountModels(from models.BankAccount) bankAccount {
func toBankAccountModels(from bankAccount) models.BankAccount {
ba := models.BankAccount{
ID: from.ID,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: from.CreatedAt.Time,
Name: from.Name,
Country: from.Country,
Metadata: from.Metadata,
Expand Down Expand Up @@ -326,7 +326,7 @@ func fromBankAccountRelatedAccountModels(from models.BankAccountRelatedAccount)
BankAccountID: from.BankAccountID,
AccountID: from.AccountID,
ConnectorID: from.ConnectorID,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: time.New(from.CreatedAt),
}
}

Expand All @@ -335,6 +335,6 @@ func toBankAccountRelatedAccountModels(from bankAccountRelatedAccount) models.Ba
BankAccountID: from.BankAccountID,
AccountID: from.AccountID,
ConnectorID: from.ConnectorID,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: from.CreatedAt.Time,
}
}
2 changes: 1 addition & 1 deletion components/payments/internal/storage/bank_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package storage
import (
"context"
"testing"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/logging"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
Expand Down
8 changes: 4 additions & 4 deletions components/payments/internal/storage/connectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"database/sql"
"encoding/json"
"fmt"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/pkg/errors"
"github.com/uptrace/bun"
Expand Down Expand Up @@ -40,7 +40,7 @@ func (s *store) ConnectorsInstall(ctx context.Context, c models.Connector) error
toInsert := connector{
ID: c.ID,
Name: c.Name,
CreatedAt: c.CreatedAt.UTC(),
CreatedAt: time.New(c.CreatedAt),
Provider: c.Provider,
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func (s *store) ConnectorsGet(ctx context.Context, id models.ConnectorID) (*mode
return &models.Connector{
ID: connector.ID,
Name: connector.Name,
CreatedAt: connector.CreatedAt.UTC(),
CreatedAt: connector.CreatedAt.Time,
Provider: connector.Provider,
Config: connector.DecryptedConfig,
}, nil
Expand Down Expand Up @@ -155,7 +155,7 @@ func (s *store) ConnectorsList(ctx context.Context, q ListConnectorsQuery) (*bun
connectors = append(connectors, models.Connector{
ID: c.ID,
Name: c.Name,
CreatedAt: c.CreatedAt.UTC(),
CreatedAt: c.CreatedAt.Time,
Provider: c.Provider,
Config: c.DecryptedConfig,
})
Expand Down
10 changes: 5 additions & 5 deletions components/payments/internal/storage/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"encoding/json"
"fmt"
"math/big"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/pkg/errors"
"github.com/uptrace/bun"
Expand Down Expand Up @@ -310,7 +310,7 @@ func fromPaymentModels(from models.Payment) payment {
ID: from.ID,
ConnectorID: from.ConnectorID,
Reference: from.Reference,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: time.New(from.CreatedAt),
Type: from.Type,
InitialAmount: from.InitialAmount,
Amount: from.Amount,
Expand All @@ -328,7 +328,7 @@ func toPaymentModels(payment payment, status models.PaymentStatus) models.Paymen
ConnectorID: payment.ConnectorID,
InitialAmount: payment.InitialAmount,
Reference: payment.Reference,
CreatedAt: payment.CreatedAt.UTC(),
CreatedAt: payment.CreatedAt.Time,
Type: payment.Type,
Amount: payment.Amount,
Asset: payment.Asset,
Expand All @@ -345,7 +345,7 @@ func fromPaymentAdjustmentModels(from models.PaymentAdjustment) paymentAdjustmen
ID: from.ID,
PaymentID: from.PaymentID,
Reference: from.Reference,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: time.New(from.CreatedAt),
Status: from.Status,
Amount: from.Amount,
Asset: from.Asset,
Expand All @@ -359,7 +359,7 @@ func toPaymentAdjustmentModels(from paymentAdjustment) models.PaymentAdjustment
ID: from.ID,
PaymentID: from.PaymentID,
Reference: from.Reference,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: from.CreatedAt.Time,
Status: from.Status,
Amount: from.Amount,
Asset: from.Asset,
Expand Down
2 changes: 1 addition & 1 deletion components/payments/internal/storage/payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"math/big"
"testing"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/logging"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
Expand Down
6 changes: 3 additions & 3 deletions components/payments/internal/storage/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package storage
import (
"context"
"fmt"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
"github.com/pkg/errors"
Expand Down Expand Up @@ -203,7 +203,7 @@ func fromPoolModel(from models.Pool) (pool, []poolAccounts) {
p := pool{
ID: from.ID,
Name: from.Name,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: time.New(from.CreatedAt),
}

var accounts []poolAccounts
Expand All @@ -229,7 +229,7 @@ func toPoolModel(from pool) models.Pool {
return models.Pool{
ID: from.ID,
Name: from.Name,
CreatedAt: from.CreatedAt.UTC(),
CreatedAt: from.CreatedAt.Time,
PoolAccounts: accounts,
}
}
2 changes: 1 addition & 1 deletion components/payments/internal/storage/pools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package storage
import (
"context"
"testing"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/logging"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
Expand Down
6 changes: 3 additions & 3 deletions components/payments/internal/storage/schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package storage
import (
"context"
"fmt"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/pkg/errors"
"github.com/uptrace/bun"
Expand Down Expand Up @@ -128,14 +128,14 @@ func fromScheduleModel(s models.Schedule) schedule {
return schedule{
ID: s.ID,
ConnectorID: s.ConnectorID,
CreatedAt: s.CreatedAt.UTC(),
CreatedAt: time.New(s.CreatedAt),
}
}

func toScheduleModel(s schedule) models.Schedule {
return models.Schedule{
ID: s.ID,
ConnectorID: s.ConnectorID,
CreatedAt: s.CreatedAt.UTC(),
CreatedAt: s.CreatedAt.Time,
}
}
2 changes: 1 addition & 1 deletion components/payments/internal/storage/schedules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package storage
import (
"context"
"testing"
"time"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/logging"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
Expand Down
Loading

0 comments on commit 615ddd8

Please sign in to comment.