From d44798bf0fa6ad860d11f5fc5ec206cc93b1bc9e Mon Sep 17 00:00:00 2001 From: MaysWind Date: Thu, 9 Apr 2026 23:44:53 +0800 Subject: [PATCH] use base context to handle the cases where the user IP address is unavailable --- .../converter/data_table_transaction_data_importer.go | 2 +- pkg/core/context.go | 1 + pkg/core/context_cli.go | 5 +++++ pkg/core/context_cron.go | 5 +++++ pkg/core/context_null.go | 5 +++++ pkg/services/transactions.go | 2 +- 6 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/converters/converter/data_table_transaction_data_importer.go b/pkg/converters/converter/data_table_transaction_data_importer.go index fde1ffa4..5b7423d0 100644 --- a/pkg/converters/converter/data_table_transaction_data_importer.go +++ b/pkg/converters/converter/data_table_transaction_data_importer.go @@ -383,7 +383,7 @@ func (c *DataTableTransactionDataImporter) ParseImportedData(ctx core.Context, u Comment: description, GeoLongitude: geoLongitude, GeoLatitude: geoLatitude, - CreatedIp: "127.0.0.1", + CreatedIp: ctx.ClientIP(), }, TagIds: tagIds, OriginalCategoryName: subCategoryName, diff --git a/pkg/core/context.go b/pkg/core/context.go index aa327146..d9486176 100644 --- a/pkg/core/context.go +++ b/pkg/core/context.go @@ -5,6 +5,7 @@ import "context" // Context is the base context of ezBookkeeping type Context interface { context.Context + ClientIP() string GetContextId() string GetClientLocale() string } diff --git a/pkg/core/context_cli.go b/pkg/core/context_cli.go index 14b355b9..65004b6c 100644 --- a/pkg/core/context_cli.go +++ b/pkg/core/context_cli.go @@ -12,6 +12,11 @@ type CliContext struct { command *cli.Command } +// ClientIP returns the client IP address, for CLI context, it always returns the loopback address +func (c *CliContext) ClientIP() string { + return "127.0.0.1" +} + // GetContextId returns the current context id func (c *CliContext) GetContextId() string { return "" diff --git a/pkg/core/context_cron.go b/pkg/core/context_cron.go index 8dc5eba8..3a92103c 100644 --- a/pkg/core/context_cron.go +++ b/pkg/core/context_cron.go @@ -14,6 +14,11 @@ type CronContext struct { cronJobInterval time.Duration } +// ClientIP returns the client IP address, for cron job context, it always returns the loopback address +func (c *CronContext) ClientIP() string { + return "127.0.0.1" +} + // GetContextId returns the current context id func (c *CronContext) GetContextId() string { return c.contextId diff --git a/pkg/core/context_null.go b/pkg/core/context_null.go index b749ef79..f4967b2d 100644 --- a/pkg/core/context_null.go +++ b/pkg/core/context_null.go @@ -9,6 +9,11 @@ type NullContext struct { context.Context } +// ClientIP returns the client IP address, for null context, it always returns the loopback address +func (c *NullContext) ClientIP() string { + return "127.0.0.1" +} + // GetContextId returns the current context id func (c *NullContext) GetContextId() string { return nullContextId diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 25875b0b..1daf6cb0 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -778,7 +778,7 @@ func (s *TransactionService) CreateScheduledTransactions(c core.Context, current Amount: template.Amount, HideAmount: template.HideAmount, Comment: template.Comment, - CreatedIp: "127.0.0.1", + CreatedIp: c.ClientIP(), ScheduledCreated: true, }