Skip to content

Commit

Permalink
feat: define insertion date at store level
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent ccc85bf commit a691eb0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
17 changes: 11 additions & 6 deletions internal/storage/ledger/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,20 @@ 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{
Ledger: s.ledger.Name,
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)
Expand All @@ -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),
Expand All @@ -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{}
Expand All @@ -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,
Expand All @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions internal/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down

0 comments on commit a691eb0

Please sign in to comment.