From cc8646cf1b00fefd9e2dc03a12e39ba51b3c1089 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 7 Sep 2024 22:10:38 +0800 Subject: [PATCH] add log --- pkg/api/data_managements.go | 2 +- pkg/cli/user_data.go | 4 +- .../data_table_transaction_data_converter.go | 26 +++++- ...g_transaction_data_plain_text_converter.go | 9 +- ...nsaction_data_plain_text_converter_test.go | 82 +++++++++++-------- pkg/converters/transaction_data_converter.go | 5 +- 6 files changed, 85 insertions(+), 43 deletions(-) diff --git a/pkg/api/data_managements.go b/pkg/api/data_managements.go index 1688de09..846c1d6e 100644 --- a/pkg/api/data_managements.go +++ b/pkg/api/data_managements.go @@ -255,7 +255,7 @@ func (a *DataManagementsApi) getExportedFileContent(c *core.WebContext, fileType dataExporter = a.ezBookKeepingCsvConverter } - result, err := dataExporter.ToExportedContent(uid, allTransactions, accountMap, categoryMap, tagMap, tagIndexes) + result, err := dataExporter.ToExportedContent(c, uid, allTransactions, accountMap, categoryMap, tagMap, tagIndexes) if err != nil { log.Errorf(c, "[data_managements.ExportDataHandler] failed to get csv format exported data for \"uid:%d\", because %s", uid, err.Error()) diff --git a/pkg/cli/user_data.go b/pkg/cli/user_data.go index b39754b3..51ae148a 100644 --- a/pkg/cli/user_data.go +++ b/pkg/cli/user_data.go @@ -653,7 +653,7 @@ func (l *UserDataCli) ExportTransaction(c *core.CliContext, username string, fil dataExporter = l.ezBookKeepingCsvConverter } - result, err := dataExporter.ToExportedContent(uid, allTransactions, accountMap, categoryMap, tagMap, tagIndexesMap) + result, err := dataExporter.ToExportedContent(c, uid, allTransactions, accountMap, categoryMap, tagMap, tagIndexesMap) if err != nil { log.BootErrorf(c, "[user_data.ExportTransaction] failed to get csv format exported data for \"%s\", because %s", username, err.Error()) @@ -693,7 +693,7 @@ func (l *UserDataCli) ImportTransaction(c *core.CliContext, username string, fil return err } - newTransactions, newAccounts, newCategories, newTags, err := dataImporter.ParseImportedData(user, data, utils.GetTimezoneOffsetMinutes(time.Local), accountMap, categoryMap, tagMap) + newTransactions, newAccounts, newCategories, newTags, err := dataImporter.ParseImportedData(c, user, data, utils.GetTimezoneOffsetMinutes(time.Local), accountMap, categoryMap, tagMap) if err != nil { log.BootErrorf(c, "[user_data.ImportTransaction] failed to parse imported data for \"%s\", because %s", username, err.Error()) diff --git a/pkg/converters/data_table_transaction_data_converter.go b/pkg/converters/data_table_transaction_data_converter.go index b7087a1c..6242d3c1 100644 --- a/pkg/converters/data_table_transaction_data_converter.go +++ b/pkg/converters/data_table_transaction_data_converter.go @@ -6,7 +6,9 @@ import ( "strings" "time" + "github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/errs" + "github.com/mayswind/ezbookkeeping/pkg/log" "github.com/mayswind/ezbookkeeping/pkg/models" "github.com/mayswind/ezbookkeeping/pkg/utils" "github.com/mayswind/ezbookkeeping/pkg/validators" @@ -44,7 +46,7 @@ type DataTableTransactionDataConverter struct { transactionTagSeparator string } -func (c *DataTableTransactionDataConverter) buildExportedContent(dataTableBuilder DataTableBuilder, uid int64, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexes map[int64][]int64) error { +func (c *DataTableTransactionDataConverter) buildExportedContent(ctx core.Context, dataTableBuilder DataTableBuilder, uid int64, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexes map[int64][]int64) error { for i := 0; i < len(transactions); i++ { transaction := transactions[i] @@ -80,8 +82,9 @@ func (c *DataTableTransactionDataConverter) buildExportedContent(dataTableBuilde return nil } -func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, dataTable ImportedDataTable, defaultTimezoneOffset int16, accountMap map[string]*models.Account, categoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) ([]*models.Transaction, []*models.Account, []*models.TransactionCategory, []*models.TransactionTag, error) { +func (c *DataTableTransactionDataConverter) parseImportedData(ctx core.Context, user *models.User, dataTable ImportedDataTable, defaultTimezoneOffset int16, accountMap map[string]*models.Account, categoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) ([]*models.Transaction, []*models.Account, []*models.TransactionCategory, []*models.TransactionTag, error) { if dataTable.DataRowCount() < 1 { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse import data for user \"uid:%d\", because data table row count is less 1", user.Uid) return nil, nil, nil, nil, errs.ErrOperationFailed } @@ -108,6 +111,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, if !timeColumnExists || !typeColumnExists || !subCategoryColumnExists || !accountColumnExists || !amountColumnExists || !account2ColumnExists || !amount2ColumnExists { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse import data for user \"uid:%d\", because missing essential columns in header row", user.Uid) return nil, nil, nil, nil, errs.ErrFormatInvalid } @@ -129,8 +133,10 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, allNewTags := make([]*models.TransactionTag, 0) dataRowIterator := dataTable.DataRowIterator() + dataRowIndex := 0 for dataRowIterator.HasNext() { + dataRowIndex++ dataRow := dataRowIterator.Next() columnCount := dataRow.ColumnCount() @@ -139,6 +145,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, } if columnCount < len(headerLineItems) { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse data row \"index:%d\" for user \"uid:%d\", because may missing some columns (column count %d in data row is less than header column count %d)", dataRowIndex, user.Uid, columnCount, len(headerLineItems)) return nil, nil, nil, nil, errs.ErrFormatInvalid } @@ -148,6 +155,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, transactionTimezone, err := utils.ParseFromTimezoneOffset(dataRow.GetData(timezoneColumnIdx)) if err != nil { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse time zone \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(timezoneColumnIdx), dataRowIndex, user.Uid, err.Error()) return nil, nil, nil, nil, err } @@ -157,12 +165,14 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, transactionTime, err := utils.ParseFromLongDateTime(dataRow.GetData(timeColumnIdx), timezoneOffset) if err != nil { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse time \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(timeColumnIdx), dataRowIndex, user.Uid, err.Error()) return nil, nil, nil, nil, err } transactionDbType, err := c.getTransactionDbType(dataRow.GetData(typeColumnIdx)) if err != nil { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse transaction type \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(typeColumnIdx), dataRowIndex, user.Uid, err.Error()) return nil, nil, nil, nil, err } @@ -172,12 +182,14 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, transactionCategoryType, err := c.getTransactionCategoryType(transactionDbType) if err != nil { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse transaction category type in data row \"index:%d\" for user \"uid:%d\", because %s", dataRowIndex, user.Uid, err.Error()) return nil, nil, nil, nil, err } subCategoryName := dataRow.GetData(subCategoryColumnIdx) if subCategoryName == "" { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] sub category type is empty in data row \"index:%d\" for user \"uid:%d\"", dataRowIndex, user.Uid) return nil, nil, nil, nil, errs.ErrFormatInvalid } @@ -195,6 +207,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, accountName := dataRow.GetData(accountColumnIdx) if accountName == "" { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] account name is empty in data row \"index:%d\" for user \"uid:%d\"", dataRowIndex, user.Uid) return nil, nil, nil, nil, errs.ErrFormatInvalid } @@ -207,6 +220,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, currency = dataRow.GetData(accountCurrencyColumnIdx) if _, ok := validators.AllCurrencyNames[currency]; !ok { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] account currency \"%s\" is not supported in data row \"index:%d\" for user \"uid:%d\"", currency, dataRowIndex, user.Uid) return nil, nil, nil, nil, errs.ErrAccountCurrencyInvalid } } @@ -218,6 +232,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, if accountCurrencyColumnExists { if account.Currency != dataRow.GetData(accountCurrencyColumnIdx) { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] currency \"%s\" in data row \"index:%d\" not equals currency \"%s\" of the account for user \"uid:%d\"", dataRow.GetData(accountCurrencyColumnIdx), dataRowIndex, account.Currency, user.Uid) return nil, nil, nil, nil, errs.ErrAccountCurrencyInvalid } } @@ -225,6 +240,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, amount, err := utils.ParseAmount(dataRow.GetData(amountColumnIdx)) if err != nil { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse acmount \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(amountColumnIdx), dataRowIndex, user.Uid, err.Error()) return nil, nil, nil, nil, err } @@ -235,6 +251,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, account2Name := dataRow.GetData(account2ColumnIdx) if account2Name == "" { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] account2 name is empty in data row \"index:%d\" for user \"uid:%d\"", dataRowIndex, user.Uid) return nil, nil, nil, nil, errs.ErrFormatInvalid } @@ -247,6 +264,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, currency = dataRow.GetData(account2CurrencyColumnIdx) if _, ok := validators.AllCurrencyNames[currency]; !ok { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] account2 currency \"%s\" is not supported in data row \"index:%d\" for user \"uid:%d\"", currency, dataRowIndex, user.Uid) return nil, nil, nil, nil, errs.ErrAccountCurrencyInvalid } } @@ -258,6 +276,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, if account2CurrencyColumnExists { if account2.Currency != dataRow.GetData(account2CurrencyColumnIdx) { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] currency \"%s\" in data row \"index:%d\" not equals currency \"%s\" of the account2 for user \"uid:%d\"", dataRow.GetData(account2CurrencyColumnIdx), dataRowIndex, account2.Currency, user.Uid) return nil, nil, nil, nil, errs.ErrAccountCurrencyInvalid } } @@ -266,6 +285,7 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, relatedAccountAmount, err = utils.ParseAmount(dataRow.GetData(amount2ColumnIdx)) if err != nil { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse acmount2 \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(amount2ColumnIdx), dataRowIndex, user.Uid, err.Error()) return nil, nil, nil, nil, err } } @@ -280,12 +300,14 @@ func (c *DataTableTransactionDataConverter) parseImportedData(user *models.User, geoLongitude, err = utils.StringToFloat64(geoLocationItems[0]) if err != nil { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse geographic location \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(geoLocationIdx), dataRowIndex, user.Uid, err.Error()) return nil, nil, nil, nil, err } geoLatitude, err = utils.StringToFloat64(geoLocationItems[1]) if err != nil { + log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse geographic location \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(geoLocationIdx), dataRowIndex, user.Uid, err.Error()) return nil, nil, nil, nil, err } } diff --git a/pkg/converters/ezbookkeeping_transaction_data_plain_text_converter.go b/pkg/converters/ezbookkeeping_transaction_data_plain_text_converter.go index 7c0d74b0..a9db7c60 100644 --- a/pkg/converters/ezbookkeeping_transaction_data_plain_text_converter.go +++ b/pkg/converters/ezbookkeeping_transaction_data_plain_text_converter.go @@ -1,6 +1,7 @@ package converters import ( + "github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/models" ) @@ -60,9 +61,9 @@ var ezbookkeepingDataColumns = []DataTableColumn{ } // ToExportedContent returns the exported transaction plain text data -func (c *ezBookKeepingTransactionDataPlainTextConverter) ToExportedContent(uid int64, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexes map[int64][]int64) ([]byte, error) { +func (c *ezBookKeepingTransactionDataPlainTextConverter) ToExportedContent(ctx core.Context, uid int64, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexes map[int64][]int64) ([]byte, error) { dataTableBuilder := createNewezbookkeepingTransactionPlainTextDataTableBuilder(len(transactions), c.columns, c.dataColumnMapping, c.columnSeparator, c.lineSeparator) - err := c.buildExportedContent(dataTableBuilder, uid, transactions, accountMap, categoryMap, tagMap, allTagIndexes) + err := c.buildExportedContent(ctx, dataTableBuilder, uid, transactions, accountMap, categoryMap, tagMap, allTagIndexes) if err != nil { return nil, err @@ -72,12 +73,12 @@ func (c *ezBookKeepingTransactionDataPlainTextConverter) ToExportedContent(uid i } // ParseImportedData returns the imported data by parsing the transaction plain text data -func (c *ezBookKeepingTransactionDataPlainTextConverter) ParseImportedData(user *models.User, data []byte, defaultTimezoneOffset int16, accountMap map[string]*models.Account, categoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) ([]*models.Transaction, []*models.Account, []*models.TransactionCategory, []*models.TransactionTag, error) { +func (c *ezBookKeepingTransactionDataPlainTextConverter) ParseImportedData(ctx core.Context, user *models.User, data []byte, defaultTimezoneOffset int16, accountMap map[string]*models.Account, categoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) ([]*models.Transaction, []*models.Account, []*models.TransactionCategory, []*models.TransactionTag, error) { dataTable, err := createNewezbookkeepingTransactionPlainTextDataTable(string(data), c.columnSeparator, c.lineSeparator) if err != nil { return nil, nil, nil, nil, err } - return c.parseImportedData(user, dataTable, defaultTimezoneOffset, accountMap, categoryMap, tagMap) + return c.parseImportedData(ctx, user, dataTable, defaultTimezoneOffset, accountMap, categoryMap, tagMap) } diff --git a/pkg/converters/ezbookkeeping_transaction_data_plain_text_converter_test.go b/pkg/converters/ezbookkeeping_transaction_data_plain_text_converter_test.go index d958e12f..f33d33e9 100644 --- a/pkg/converters/ezbookkeeping_transaction_data_plain_text_converter_test.go +++ b/pkg/converters/ezbookkeeping_transaction_data_plain_text_converter_test.go @@ -5,12 +5,14 @@ import ( "github.com/stretchr/testify/assert" + "github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/models" "github.com/mayswind/ezbookkeeping/pkg/utils" ) func TestEzBookKeepingPlainFileConverterToExportedContent(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() transactions := make([]*models.Transaction, 3) transactions[0] = &models.Transaction{ @@ -116,7 +118,7 @@ func TestEzBookKeepingPlainFileConverterToExportedContent(t *testing.T) { "2024-09-01 12:34:56,+08:00,Income,Test Category,Test Sub Category,Test Account,CNY,123.45,,,,123.450000 45.670000,Test Tag;Test Tag2,Hello World\n" + "2024-09-01 12:34:56,+00:00,Expense,Test Category2,Test Sub Category2,Test Account,CNY,-0.10,,,,,Test Tag,Foo#Bar\n" + "2024-09-01 12:34:56,-05:00,Transfer,Test Category3,Test Sub Category3,Test Account,CNY,123.45,Test Account2,USD,17.35,,Test Tag2,T\te s t test\n" - actualContent, err := converter.ToExportedContent(123, transactions, accountMap, categoryMap, tagMap, allTagIndexes) + actualContent, err := converter.ToExportedContent(context, 123, transactions, accountMap, categoryMap, tagMap, allTagIndexes) assert.Nil(t, err) assert.Equal(t, expectedContent, string(actualContent)) @@ -124,13 +126,14 @@ func TestEzBookKeepingPlainFileConverterToExportedContent(t *testing.T) { func TestEzBookKeepingPlainFileConverterParseImportedData_MinimumValidData(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - allNewTransactions, allNewAccounts, allNewSubCategories, allNewTags, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + allNewTransactions, allNewAccounts, allNewSubCategories, allNewTags, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01 00:00:00,Balance Modification,,Test Account,123.45,,\n"+ "2024-09-01 01:23:45,Income,Test Category,Test Account,0.12,,\n"+ "2024-09-01 12:34:56,Expense,Test Category2,Test Account,1.00,,\n"+ @@ -183,43 +186,46 @@ func TestEzBookKeepingPlainFileConverterParseImportedData_MinimumValidData(t *te func TestEzBookKeepingPlainFileConverterParseImportedData_ParseInvalidTime(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - _, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + _, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01T12:34:56,Expense,Test Category,Test Account,123.45,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "09/01/2024 12:34:56,Expense,Test Category,Test Account,123.45,,"), 0, nil, nil, nil) assert.NotNil(t, err) } func TestEzBookKeepingPlainFileConverterParseImportedData_ParseInvalidType(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - _, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + _, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01 12:34:56,Type,Test Category,Test Account,123.45,,"), 0, nil, nil, nil) assert.NotNil(t, err) } func TestEzBookKeepingPlainFileConverterParseImportedData_ParseValidTimezone(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - allNewTransactions, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Timezone,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + allNewTransactions, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Timezone,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01 12:34:56,+08:00,Expense,Test Category,Test Account,123.45,,"), 0, nil, nil, nil) assert.Nil(t, err) assert.Equal(t, 1, len(allNewTransactions)) @@ -228,56 +234,60 @@ func TestEzBookKeepingPlainFileConverterParseImportedData_ParseValidTimezone(t * func TestEzBookKeepingPlainFileConverterParseImportedData_ParseInvalidTimezone(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - _, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Timezone,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + _, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Timezone,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01 12:34:56,Asia/Shanghai,Expense,Test Category,Test Account,123.45,,"), 0, nil, nil, nil) assert.NotNil(t, err) } func TestEzBookKeepingPlainFileConverterParseImportedData_ParseInvalidSubCategoryName(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - _, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + _, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01 12:34:56,Expense,,Test Account,123.45,,"), 0, nil, nil, nil) assert.NotNil(t, err) } func TestEzBookKeepingPlainFileConverterParseImportedData_ParseInvalidAccountName(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - _, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + _, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01 12:34:56,Expense,Test Category,,123.45,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01 12:34:56,Transfer,Test Category,Test Account,123.45,,123.45"), 0, nil, nil, nil) assert.NotNil(t, err) } func TestEzBookKeepingPlainFileConverterParseImportedData_ParseValidAccountCurrency(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - allNewTransactions, allNewAccounts, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ + allNewTransactions, allNewAccounts, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ "2024-09-01 01:23:45,Balance Modification,Test Category,Test Account,USD,123.45,,,\n"+ "2024-09-01 12:34:56,Transfer,Test Category2,Test Account,USD,1.23,Test Account2,EUR,1.10"), 0, nil, nil, nil) @@ -297,18 +307,19 @@ func TestEzBookKeepingPlainFileConverterParseImportedData_ParseValidAccountCurre func TestEzBookKeepingPlainFileConverterParseImportedData_ParseInvalidAccountCurrency(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - _, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ + _, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ "2024-09-01 01:23:45,Balance Modification,,Test Account,USD,123.45,,,\n"+ "2024-09-01 12:34:56,Transfer,Test Category3,Test Account,CNY,1.23,Test Account2,EUR,1.10"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ "2024-09-01 01:23:45,Balance Modification,,Test Account,USD,123.45,,,\n"+ "2024-09-01 12:34:56,Transfer,Test Category3,Test Account2,CNY,1.23,Test Account,EUR,1.10"), 0, nil, nil, nil) assert.NotNil(t, err) @@ -316,47 +327,50 @@ func TestEzBookKeepingPlainFileConverterParseImportedData_ParseInvalidAccountCur func TestEzBookKeepingPlainFileConverterParseImportedData_ParseNotSupportedCurrency(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - _, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ + _, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ "2024-09-01 01:23:45,Balance Modification,Test Category,Test Account,XXX,123.45,,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount\n"+ "2024-09-01 01:23:45,Transfer,Test Category,Test Account,USD,123.45,Test Account2,XXX,123.45"), 0, nil, nil, nil) assert.NotNil(t, err) } func TestEzBookKeepingPlainFileConverterParseImportedData_ParseInvalidAmount(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - _, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + _, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01 12:34:56,Expense,Test Category,Test Account,123 45,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount\n"+ "2024-09-01 12:34:56,Transfer,Test Category,Test Account,123.45,Test Account2,123 45"), 0, nil, nil, nil) assert.NotNil(t, err) } func TestEzBookKeepingPlainFileConverterParseImportedData_ParseValidGeographicLocation(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - allNewTransactions, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Geographic Location\n"+ + allNewTransactions, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Geographic Location\n"+ "2024-09-01 12:34:56,Expense,Test Category,Test Account,123.45,,,123.45 45.56"), 0, nil, nil, nil) assert.Nil(t, err) @@ -367,37 +381,39 @@ func TestEzBookKeepingPlainFileConverterParseImportedData_ParseValidGeographicLo func TestEzBookKeepingPlainFileConverterParseImportedData_ParseInvalidGeographicLocation(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - allNewTransactions, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Geographic Location\n"+ + allNewTransactions, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Geographic Location\n"+ "2024-09-01 12:34:56,Expense,Test Category,Test Account,123.45,,,1"), 0, nil, nil, nil) assert.Nil(t, err) assert.Equal(t, 1, len(allNewTransactions)) assert.Equal(t, float64(0), allNewTransactions[0].GeoLongitude) assert.Equal(t, float64(0), allNewTransactions[0].GeoLatitude) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Geographic Location\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Geographic Location\n"+ "2024-09-01 12:34:56,Expense,Test Category,Test Account,123.45,,,a b"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Geographic Location\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Geographic Location\n"+ "2024-09-01 12:34:56,Expense,Test Category,Test Account,123.45,,,1 "), 0, nil, nil, nil) assert.NotNil(t, err) } func TestEzBookKeepingPlainFileConverterParseImportedData_ParseTag(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - _, _, _, allNewTags, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Tags\n"+ + _, _, _, allNewTags, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Tags\n"+ "2024-09-01 00:00:00,Balance Modification,,Test Account,123.45,,,foo;;bar.;#test;hello\tworld;;"), 0, nil, nil, nil) assert.Nil(t, err) @@ -419,13 +435,14 @@ func TestEzBookKeepingPlainFileConverterParseImportedData_ParseTag(t *testing.T) func TestEzBookKeepingPlainFileConverterParseImportedData_ParseDescription(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1234567890, DefaultCurrency: "CNY", } - allNewTransactions, _, _, _, err := converter.ParseImportedData(user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Description\n"+ + allNewTransactions, _, _, _, err := converter.ParseImportedData(context, user, []byte("Time,Type,Sub Category,Account,Amount,Account2,Account2 Amount,Description\n"+ "2024-09-01 12:34:56,Expense,Test Category,Test Account,123.45,,,foo bar\t#test"), 0, nil, nil, nil) assert.Nil(t, err) @@ -435,44 +452,45 @@ func TestEzBookKeepingPlainFileConverterParseImportedData_ParseDescription(t *te func TestEzBookKeepingPlainFileConverterParseImportedData_MissingRequiredColumn(t *testing.T) { converter := EzBookKeepingTransactionDataCSVFileConverter + context := core.NewNullContext() user := &models.User{ Uid: 1, DefaultCurrency: "CNY", } - _, _, _, _, err := converter.ParseImportedData(user, []byte(""), 0, nil, nil, nil) + _, _, _, _, err := converter.ParseImportedData(context, user, []byte(""), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ "+08:00,Balance Modification,Test Category,Test Sub Category,Test Account,CNY,123.45,,,,,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ "2024-09-01 00:00:00,+08:00,Test Category,Test Sub Category,Test Account,CNY,123.45,,,,,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Type,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Type,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ "2024-09-01 00:00:00,+08:00,Balance Modification,Test Account,CNY,123.45,,,,,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Timezone,Type,Category,Sub Category,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Timezone,Type,Category,Sub Category,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ "2024-09-01 00:00:00,+08:00,Balance Modification,Test Category,Test Sub Category,CNY,123.45,,,,,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ "2024-09-01 00:00:00,+08:00,Balance Modification,Test Category,Test Sub Category,Test Account,CNY,,,,,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ "2024-09-01 00:00:00,+08:00,Balance Modification,Test Category,Test Sub Category,Test Account,CNY,123.45,,,,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Geographic Location,Tags,Description\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Geographic Location,Tags,Description\n"+ "2024-09-01 00:00:00,+08:00,Balance Modification,Test Category,Test Sub Category,Test Account,CNY,123.45,,,,,"), 0, nil, nil, nil) assert.NotNil(t, err) - _, _, _, _, err = converter.ParseImportedData(user, []byte("Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ + _, _, _, _, err = converter.ParseImportedData(context, user, []byte("Time,Timezone,Type,Category,Sub Category,Account,Account Currency,Amount,Account2,Account2 Currency,Account2 Amount,Geographic Location,Tags,Description\n"+ "2024-09-01 00:00:00,+08:00,Balance Modification,Test Category,Test Sub Category,Test Account,CNY,123.45,,,,,"), 0, nil, nil, nil) assert.NotNil(t, err) } diff --git a/pkg/converters/transaction_data_converter.go b/pkg/converters/transaction_data_converter.go index f3b86038..3d65f05a 100644 --- a/pkg/converters/transaction_data_converter.go +++ b/pkg/converters/transaction_data_converter.go @@ -1,19 +1,20 @@ package converters import ( + "github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/models" ) // TransactionDataExporter defines the structure of transaction data exporter type TransactionDataExporter interface { // ToExportedContent returns the exported data - ToExportedContent(uid int64, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexes map[int64][]int64) ([]byte, error) + ToExportedContent(ctx core.Context, uid int64, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexes map[int64][]int64) ([]byte, error) } // TransactionDataImporter defines the structure of transaction data importer type TransactionDataImporter interface { // ParseImportedData returns the imported data - ParseImportedData(user *models.User, data []byte, defaultTimezoneOffset int16, accountMap map[string]*models.Account, categoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) ([]*models.Transaction, []*models.Account, []*models.TransactionCategory, []*models.TransactionTag, error) + ParseImportedData(ctx core.Context, user *models.User, data []byte, defaultTimezoneOffset int16, accountMap map[string]*models.Account, categoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) ([]*models.Transaction, []*models.Account, []*models.TransactionCategory, []*models.TransactionTag, error) } // TransactionDataConverter defines the structure of transaction data converter