From a691eb0e12c4c7e5abf76fdd3c84e72e1ac85f58 Mon Sep 17 00:00:00 2001 From: Geoffrey Ragot Date: Wed, 25 Sep 2024 23:47:50 +0200 Subject: [PATCH] feat: define insertion date at store level --- internal/storage/ledger/transactions.go | 17 +++++++++++------ internal/transaction.go | 7 ++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/internal/storage/ledger/transactions.go b/internal/storage/ledger/transactions.go index e99a1fbf4..84fad8b39 100644 --- a/internal/storage/ledger/transactions.go +++ b/internal/storage/ledger/transactions.go @@ -286,6 +286,11 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e return postgres.ResolveError(err) } + insertionDate := tx.InsertedAt + if insertionDate.IsZero() { + insertionDate = time.Now() + } + accounts := map[string]Account{} for _, address := range tx.InvolvedAccounts() { account := Account{ @@ -293,8 +298,8 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e AddressArray: strings.Split(address, ":"), Address: address, FirstUsage: tx.Timestamp, - InsertionDate: tx.InsertedAt, - UpdatedAt: tx.InsertedAt, + InsertionDate: insertionDate, + UpdatedAt: insertionDate, Metadata: make(metadata.Metadata), } _, err := s.upsertAccount(ctx, &account) @@ -318,7 +323,7 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e Metadata: tx.Metadata, Timestamp: tx.Timestamp, Reference: tx.Reference, - InsertedAt: tx.InsertedAt, + InsertedAt: insertionDate, Sources: sources, Destinations: destinations, SourcesArray: Map(sources, convertAddrToIndexedJSONB), @@ -334,7 +339,7 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e tx.ID = mappedTx.ID tx.PostCommitVolumes = postCommitVolumes.Copy() tx.Timestamp = mappedTx.Timestamp - tx.InsertedAt = mappedTx.InsertedAt + tx.InsertedAt = insertionDate if s.ledger.HasFeature(ledger.FeatureMovesHistory, "ON") { moves := Moves{} @@ -348,7 +353,7 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e AccountAddressArray: strings.Split(posting.Destination, ":"), Amount: (*bunpaginate.BigInt)(posting.Amount), Asset: posting.Asset, - InsertionDate: tx.InsertedAt, + InsertionDate: insertionDate, EffectiveDate: tx.Timestamp, TransactionSeq: mappedTx.Seq, AccountSeq: accounts[posting.Destination].Seq, @@ -363,7 +368,7 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e AccountAddressArray: strings.Split(posting.Source, ":"), Amount: (*bunpaginate.BigInt)(posting.Amount), Asset: posting.Asset, - InsertionDate: tx.InsertedAt, + InsertionDate: insertionDate, EffectiveDate: tx.Timestamp, TransactionSeq: mappedTx.Seq, AccountSeq: accounts[posting.Source].Seq, diff --git a/internal/transaction.go b/internal/transaction.go index 932f01306..a7735a804 100644 --- a/internal/transaction.go +++ b/internal/transaction.go @@ -31,12 +31,9 @@ func (data TransactionData) WithPostings(postings ...Posting) TransactionData { } func NewTransactionData() TransactionData { - now := time.Now() return TransactionData{ - Metadata: metadata.Metadata{}, - // todo: should be defined by the database ? - InsertedAt: now, - Timestamp: now, + Metadata: metadata.Metadata{}, + Timestamp: time.Now(), } }