Skip to content

Commit

Permalink
more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanmazurek committed Jan 15, 2024
1 parent 598e3a7 commit 792af75
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
27 changes: 21 additions & 6 deletions handlers/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,42 @@ import (
)

func AssetHandler(lma *lunchmoney.Client, asset *shared.Asset) {
balanceFloat, _ := asset.Balance.Float64()
balanceFloat, err := asset.Balance.Float64()
if err != nil {
log.Error().
Err(err).
Str("ext-asset-id", asset.ExternalAssetID).
Msg("unable to update asset")
}

if asset.AssetID == nil {
log.Error().
Str("ext-asset-id", asset.ExternalAssetID).
Msg("unable to update asset, asset id not set")
}

assetId := *asset.AssetID

currency := strings.ToUpper(asset.Currency)

balance := money.NewFromFloat(math.Abs(balanceFloat), currency)

lmAsset := &models.Asset{
AssetID: asset.AssetID,
AssetID: &assetId,
Balance: *balance,
BalanceAsOf: asset.BalanceAsOf,
}

log.Info().
Str("externalId", asset.ExternalAssetID).
Int64("assetId", *asset.AssetID).
Str("ext-asset-id", asset.ExternalAssetID).
Int64("int-asset-id", assetId).
Msg("updated asset")

updatedAsset, err := lma.UpdateAsset(*asset.AssetID, lmAsset)
if err != nil || updatedAsset.Error != nil {
log.Error().
Str("externalId", asset.ExternalAssetID).
Int64("assetId", *asset.AssetID).
Str("ext-asset-id", asset.ExternalAssetID).
Int64("int-asset-id", assetId).
Err(err).Msg("unable to update asset")
}
}
33 changes: 25 additions & 8 deletions handlers/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,23 @@ var TransactionStatus = map[string]string{
}

func TransactionHandler(lma *lunchmoney.Client, transaction *shared.Transaction) {
amount := money.NewFromFloat(transaction.Amount, transaction.Currency)
amountFloat, err := transaction.Amount.Float64()
if err != nil {
log.Error().
Err(err).
Str("ext-asset-id", transaction.ExternalAssetID).
Str("ext-transaction-id", transaction.ExternalTransactionID).
Msg("unable to upsert asset")
}

if transaction.AssetID == nil {
log.Error().
Str("ext-asset-id", transaction.ExternalAssetID).
Str("ext-transaction-id", transaction.ExternalTransactionID).
Msg("unable to upsert transaction, asset id not set")
}

amount := money.NewFromFloat(amountFloat, transaction.Currency)

assetId := json.Number(fmt.Sprintf("%d", transaction.AssetID))
status := TransactionStatus[transaction.Status]
Expand All @@ -36,16 +52,17 @@ func TransactionHandler(lma *lunchmoney.Client, transaction *shared.Transaction)

insertedTransactions, err := lma.InsertTransactions([]models.Transaction{lmTransaction}, true)
if err != nil {
log.Error().Err(err).
Str("externalId", *lmTransaction.ExternalID).
Int64("assetId", transaction.AssetID).
Msg("unable to insert transaction")
log.Error().
Err(err).
Str("ext-asset-id", transaction.ExternalAssetID).
Str("ext-transaction-id", transaction.ExternalTransactionID).
Msg("unable to upsert transaction")
}

if insertedTransactions != nil {
log.Info().
Str("externalId", *lmTransaction.ExternalID).
Int64("assetId", transaction.AssetID).
Msgf("inserted %d transactions", len(*insertedTransactions))
Str("ext-asset-id", transaction.ExternalAssetID).
Str("ext-transaction-id", transaction.ExternalTransactionID).
Msg("upserted transaction")
}
}
21 changes: 12 additions & 9 deletions shared/transaction.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package shared

import "time"
import (
"encoding/json"
"time"
)

type Transaction struct {
ExternalTransactionID string `json:"externalTransactionId" bson:"externalTransactionId"`
ExternalAssetID string `json:"externalAssetId" bson:"externalAssetId"`
Datetime time.Time `json:"datetime"`
Description string `json:"description"`
Status string `json:"status"`
Amount float64 `json:"amount"`
Currency string `json:"currency"`
ExternalTransactionID string `json:"externalTransactionId" bson:"externalTransactionId"`
ExternalAssetID string `json:"externalAssetId" bson:"externalAssetId"`
Datetime time.Time `json:"datetime"`
Description string `json:"description"`
Status string `json:"status"`
Amount json.Number `json:"amount"`
Currency string `json:"currency"`

AssetID int64 `json:"assetId"`
AssetID *int64 `json:"assetId"`
}

0 comments on commit 792af75

Please sign in to comment.