From 792af75e9901e474a6f89bdbd057b5246260e10d Mon Sep 17 00:00:00 2001 From: DylanMazurek Date: Mon, 15 Jan 2024 11:02:12 +1100 Subject: [PATCH] more logging --- handlers/assets.go | 27 +++++++++++++++++++++------ handlers/transactions.go | 33 +++++++++++++++++++++++++-------- shared/transaction.go | 21 ++++++++++++--------- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/handlers/assets.go b/handlers/assets.go index 7e0270e..3939b37 100644 --- a/handlers/assets.go +++ b/handlers/assets.go @@ -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") } } diff --git a/handlers/transactions.go b/handlers/transactions.go index 6d74414..d1dd449 100644 --- a/handlers/transactions.go +++ b/handlers/transactions.go @@ -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] @@ -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") } } diff --git a/shared/transaction.go b/shared/transaction.go index 303c12e..b697de7 100644 --- a/shared/transaction.go +++ b/shared/transaction.go @@ -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"` }