code refactor

This commit is contained in:
MaysWind
2024-09-10 00:32:36 +08:00
parent 70beb45c4e
commit 56ad572387
5 changed files with 195 additions and 152 deletions
+100 -100
View File
@@ -55,32 +55,32 @@ var (
// AddNewUser adds a new user according to specified info
func (l *UserDataCli) AddNewUser(c *core.CliContext, username string, email string, nickname string, password string, defaultCurrency string) (*models.User, error) {
if username == "" {
log.BootErrorf(c, "[user_data.AddNewUser] user name is empty")
log.CliErrorf(c, "[user_data.AddNewUser] user name is empty")
return nil, errs.ErrUsernameIsEmpty
}
if email == "" {
log.BootErrorf(c, "[user_data.AddNewUser] user email is empty")
log.CliErrorf(c, "[user_data.AddNewUser] user email is empty")
return nil, errs.ErrEmailIsEmpty
}
if nickname == "" {
log.BootErrorf(c, "[user_data.AddNewUser] user nickname is empty")
log.CliErrorf(c, "[user_data.AddNewUser] user nickname is empty")
return nil, errs.ErrNicknameIsEmpty
}
if password == "" {
log.BootErrorf(c, "[user_data.AddNewUser] user password is empty")
log.CliErrorf(c, "[user_data.AddNewUser] user password is empty")
return nil, errs.ErrPasswordIsEmpty
}
if defaultCurrency == "" {
log.BootErrorf(c, "[user_data.AddNewUser] user default currency is empty")
log.CliErrorf(c, "[user_data.AddNewUser] user default currency is empty")
return nil, errs.ErrUserDefaultCurrencyIsEmpty
}
if _, ok := validators.AllCurrencyNames[defaultCurrency]; !ok {
log.BootErrorf(c, "[user_data.AddNewUser] user default currency is invalid")
log.CliErrorf(c, "[user_data.AddNewUser] user default currency is invalid")
return nil, errs.ErrUserDefaultCurrencyIsInvalid
}
@@ -97,11 +97,11 @@ func (l *UserDataCli) AddNewUser(c *core.CliContext, username string, email stri
err := l.users.CreateUser(c, user)
if err != nil {
log.BootErrorf(c, "[user_data.AddNewUser] failed to create user \"%s\", because %s", user.Username, err.Error())
log.CliErrorf(c, "[user_data.AddNewUser] failed to create user \"%s\", because %s", user.Username, err.Error())
return nil, err
}
log.BootInfof(c, "[user_data.AddNewUser] user \"%s\" has add successfully, uid is %d", user.Username, user.Uid)
log.CliInfof(c, "[user_data.AddNewUser] user \"%s\" has add successfully, uid is %d", user.Username, user.Uid)
return user, nil
}
@@ -109,14 +109,14 @@ func (l *UserDataCli) AddNewUser(c *core.CliContext, username string, email stri
// GetUserByUsername returns user by user name
func (l *UserDataCli) GetUserByUsername(c *core.CliContext, username string) (*models.User, error) {
if username == "" {
log.BootErrorf(c, "[user_data.GetUserByUsername] user name is empty")
log.CliErrorf(c, "[user_data.GetUserByUsername] user name is empty")
return nil, errs.ErrUsernameIsEmpty
}
user, err := l.users.GetUserByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.GetUserByUsername] failed to get user by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.GetUserByUsername] failed to get user by user name \"%s\", because %s", username, err.Error())
return nil, err
}
@@ -126,19 +126,19 @@ func (l *UserDataCli) GetUserByUsername(c *core.CliContext, username string) (*m
// ModifyUserPassword modifies user password
func (l *UserDataCli) ModifyUserPassword(c *core.CliContext, username string, password string) error {
if username == "" {
log.BootErrorf(c, "[user_data.ModifyUserPassword] user name is empty")
log.CliErrorf(c, "[user_data.ModifyUserPassword] user name is empty")
return errs.ErrUsernameIsEmpty
}
if password == "" {
log.BootErrorf(c, "[user_data.ModifyUserPassword] user password is empty")
log.CliErrorf(c, "[user_data.ModifyUserPassword] user password is empty")
return errs.ErrPasswordIsEmpty
}
user, err := l.users.GetUserByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.ModifyUserPassword] failed to get user by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.ModifyUserPassword] failed to get user by user name \"%s\", because %s", username, err.Error())
return err
}
@@ -155,7 +155,7 @@ func (l *UserDataCli) ModifyUserPassword(c *core.CliContext, username string, pa
_, _, err = l.users.UpdateUser(c, userNew, false)
if err != nil {
log.BootErrorf(c, "[user_data.ModifyUserPassword] failed to update user \"%s\" password, because %s", user.Username, err.Error())
log.CliErrorf(c, "[user_data.ModifyUserPassword] failed to update user \"%s\" password, because %s", user.Username, err.Error())
return err
}
@@ -163,9 +163,9 @@ func (l *UserDataCli) ModifyUserPassword(c *core.CliContext, username string, pa
err = l.tokens.DeleteTokensBeforeTime(c, user.Uid, now)
if err == nil {
log.BootInfof(c, "[user_data.ModifyUserPassword] revoke old tokens before unix time \"%d\" for user \"%s\"", now, user.Username)
log.CliInfof(c, "[user_data.ModifyUserPassword] revoke old tokens before unix time \"%d\" for user \"%s\"", now, user.Username)
} else {
log.BootWarnf(c, "[user_data.ModifyUserPassword] failed to revoke old tokens for user \"%s\", because %s", user.Username, err.Error())
log.CliWarnf(c, "[user_data.ModifyUserPassword] failed to revoke old tokens for user \"%s\", because %s", user.Username, err.Error())
}
return nil
@@ -174,33 +174,33 @@ func (l *UserDataCli) ModifyUserPassword(c *core.CliContext, username string, pa
// SendPasswordResetMail sends an email with password reset link
func (l *UserDataCli) SendPasswordResetMail(c *core.CliContext, username string) error {
if username == "" {
log.BootErrorf(c, "[user_data.SendPasswordResetMail] user name is empty")
log.CliErrorf(c, "[user_data.SendPasswordResetMail] user name is empty")
return errs.ErrUsernameIsEmpty
}
user, err := l.users.GetUserByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.SendPasswordResetMail] failed to get user by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.SendPasswordResetMail] failed to get user by user name \"%s\", because %s", username, err.Error())
return err
}
if l.CurrentConfig().ForgetPasswordRequireVerifyEmail && !user.EmailVerified {
log.BootWarnf(c, "[user_data.SendPasswordResetMail] user \"uid:%d\" has not verified email", user.Uid)
log.CliWarnf(c, "[user_data.SendPasswordResetMail] user \"uid:%d\" has not verified email", user.Uid)
return errs.ErrEmailIsNotVerified
}
token, _, err := l.tokens.CreatePasswordResetTokenWithoutUserAgent(c, user)
if err != nil {
log.BootErrorf(c, "[user_data.SendPasswordResetMail] failed to create token for user \"uid:%d\", because %s", user.Uid, err.Error())
log.CliErrorf(c, "[user_data.SendPasswordResetMail] failed to create token for user \"uid:%d\", because %s", user.Uid, err.Error())
return err
}
err = l.forgetPasswords.SendPasswordResetEmail(c, user, token, "")
if err != nil {
log.BootWarnf(c, "[user_data.SendPasswordResetMail] cannot send email to \"%s\", because %s", user.Email, err.Error())
log.CliWarnf(c, "[user_data.SendPasswordResetMail] cannot send email to \"%s\", because %s", user.Email, err.Error())
return err
}
@@ -210,14 +210,14 @@ func (l *UserDataCli) SendPasswordResetMail(c *core.CliContext, username string)
// EnableUser sets user enabled according to the specified user name
func (l *UserDataCli) EnableUser(c *core.CliContext, username string) error {
if username == "" {
log.BootErrorf(c, "[user_data.EnableUser] user name is empty")
log.CliErrorf(c, "[user_data.EnableUser] user name is empty")
return errs.ErrUsernameIsEmpty
}
err := l.users.EnableUser(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.EnableUser] failed to set user enabled by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.EnableUser] failed to set user enabled by user name \"%s\", because %s", username, err.Error())
return err
}
@@ -227,14 +227,14 @@ func (l *UserDataCli) EnableUser(c *core.CliContext, username string) error {
// DisableUser sets user disabled according to the specified user name
func (l *UserDataCli) DisableUser(c *core.CliContext, username string) error {
if username == "" {
log.BootErrorf(c, "[user_data.DisableUser] user name is empty")
log.CliErrorf(c, "[user_data.DisableUser] user name is empty")
return errs.ErrUsernameIsEmpty
}
err := l.users.DisableUser(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.DisableUser] failed to set user disabled by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.DisableUser] failed to set user disabled by user name \"%s\", because %s", username, err.Error())
return err
}
@@ -248,33 +248,33 @@ func (l *UserDataCli) ResendVerifyEmail(c *core.CliContext, username string) err
}
if username == "" {
log.BootErrorf(c, "[user_data.ResendVerifyEmail] user name is empty")
log.CliErrorf(c, "[user_data.ResendVerifyEmail] user name is empty")
return errs.ErrUsernameIsEmpty
}
user, err := l.users.GetUserByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.ResendVerifyEmail] failed to get user by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.ResendVerifyEmail] failed to get user by user name \"%s\", because %s", username, err.Error())
return err
}
if user.EmailVerified {
log.BootWarnf(c, "[user_data.ResendVerifyEmail] user \"uid:%d\" email has been verified", user.Uid)
log.CliWarnf(c, "[user_data.ResendVerifyEmail] user \"uid:%d\" email has been verified", user.Uid)
return errs.ErrEmailIsVerified
}
token, _, err := l.tokens.CreateEmailVerifyTokenWithoutUserAgent(c, user)
if err != nil {
log.BootErrorf(c, "[user_data.ResendVerifyEmail] failed to create token for user \"uid:%d\", because %s", user.Uid, err.Error())
log.CliErrorf(c, "[user_data.ResendVerifyEmail] failed to create token for user \"uid:%d\", because %s", user.Uid, err.Error())
return errs.ErrTokenGenerating
}
err = l.users.SendVerifyEmail(user, token, "")
if err != nil {
log.BootErrorf(c, "[user_data.ResendVerifyEmail] cannot send email to \"%s\", because %s", user.Email, err.Error())
log.CliErrorf(c, "[user_data.ResendVerifyEmail] cannot send email to \"%s\", because %s", user.Email, err.Error())
return err
}
@@ -284,14 +284,14 @@ func (l *UserDataCli) ResendVerifyEmail(c *core.CliContext, username string) err
// SetUserEmailVerified sets user email address verified
func (l *UserDataCli) SetUserEmailVerified(c *core.CliContext, username string) error {
if username == "" {
log.BootErrorf(c, "[user_data.SetUserEmailVerified] user name is empty")
log.CliErrorf(c, "[user_data.SetUserEmailVerified] user name is empty")
return errs.ErrUsernameIsEmpty
}
err := l.users.SetUserEmailVerified(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.SetUserEmailVerified] failed to set user email address verified by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.SetUserEmailVerified] failed to set user email address verified by user name \"%s\", because %s", username, err.Error())
return err
}
@@ -301,14 +301,14 @@ func (l *UserDataCli) SetUserEmailVerified(c *core.CliContext, username string)
// SetUserEmailUnverified sets user email address unverified
func (l *UserDataCli) SetUserEmailUnverified(c *core.CliContext, username string) error {
if username == "" {
log.BootErrorf(c, "[user_data.SetUserEmailUnverified] user name is empty")
log.CliErrorf(c, "[user_data.SetUserEmailUnverified] user name is empty")
return errs.ErrUsernameIsEmpty
}
err := l.users.SetUserEmailUnverified(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.SetUserEmailUnverified] failed to set user email address unverified by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.SetUserEmailUnverified] failed to set user email address unverified by user name \"%s\", because %s", username, err.Error())
return err
}
@@ -318,14 +318,14 @@ func (l *UserDataCli) SetUserEmailUnverified(c *core.CliContext, username string
// DeleteUser deletes user according to the specified user name
func (l *UserDataCli) DeleteUser(c *core.CliContext, username string) error {
if username == "" {
log.BootErrorf(c, "[user_data.DeleteUser] user name is empty")
log.CliErrorf(c, "[user_data.DeleteUser] user name is empty")
return errs.ErrUsernameIsEmpty
}
err := l.users.DeleteUser(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.DeleteUser] failed to delete user by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.DeleteUser] failed to delete user by user name \"%s\", because %s", username, err.Error())
return err
}
@@ -335,21 +335,21 @@ func (l *UserDataCli) DeleteUser(c *core.CliContext, username string) error {
// ListUserTokens returns all tokens of the specified user
func (l *UserDataCli) ListUserTokens(c *core.CliContext, username string) ([]*models.TokenRecord, error) {
if username == "" {
log.BootErrorf(c, "[user_data.ListUserTokens] user name is empty")
log.CliErrorf(c, "[user_data.ListUserTokens] user name is empty")
return nil, errs.ErrUsernameIsEmpty
}
uid, err := l.getUserIdByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.ListUserTokens] error occurs when getting user id by user name")
log.CliErrorf(c, "[user_data.ListUserTokens] error occurs when getting user id by user name")
return nil, err
}
tokens, err := l.tokens.GetAllUnexpiredNormalTokensByUid(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.ListUserTokens] failed to get tokens of user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.ListUserTokens] failed to get tokens of user \"%s\", because %s", username, err.Error())
return nil, err
}
@@ -359,14 +359,14 @@ func (l *UserDataCli) ListUserTokens(c *core.CliContext, username string) ([]*mo
// ClearUserTokens clears all tokens of the specified user
func (l *UserDataCli) ClearUserTokens(c *core.CliContext, username string) error {
if username == "" {
log.BootErrorf(c, "[user_data.ClearUserTokens] user name is empty")
log.CliErrorf(c, "[user_data.ClearUserTokens] user name is empty")
return errs.ErrUsernameIsEmpty
}
uid, err := l.getUserIdByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.ClearUserTokens] error occurs when getting user id by user name")
log.CliErrorf(c, "[user_data.ClearUserTokens] error occurs when getting user id by user name")
return err
}
@@ -374,7 +374,7 @@ func (l *UserDataCli) ClearUserTokens(c *core.CliContext, username string) error
err = l.tokens.DeleteTokensBeforeTime(c, uid, now)
if err != nil {
log.BootErrorf(c, "[user_data.ClearUserTokens] failed to delete tokens of user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.ClearUserTokens] failed to delete tokens of user \"%s\", because %s", username, err.Error())
return err
}
@@ -384,21 +384,21 @@ func (l *UserDataCli) ClearUserTokens(c *core.CliContext, username string) error
// DisableUserTwoFactorAuthorization disables 2fa for the specified user
func (l *UserDataCli) DisableUserTwoFactorAuthorization(c *core.CliContext, username string) error {
if username == "" {
log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] user name is empty")
log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] user name is empty")
return errs.ErrUsernameIsEmpty
}
uid, err := l.getUserIdByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] error occurs when getting user id by user name")
log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] error occurs when getting user id by user name")
return err
}
enableTwoFactor, err := l.twoFactorAuthorizations.ExistsTwoFactorSetting(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to check two-factor setting, because %s", err.Error())
log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to check two-factor setting, because %s", err.Error())
return err
}
@@ -409,14 +409,14 @@ func (l *UserDataCli) DisableUserTwoFactorAuthorization(c *core.CliContext, user
err = l.twoFactorAuthorizations.DeleteTwoFactorRecoveryCodes(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to delete two-factor recovery codes for user \"%s\"", username)
log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to delete two-factor recovery codes for user \"%s\"", username)
return err
}
err = l.twoFactorAuthorizations.DeleteTwoFactorSetting(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to delete two-factor setting for user \"%s\"", username)
log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to delete two-factor setting for user \"%s\"", username)
return err
}
@@ -426,21 +426,21 @@ func (l *UserDataCli) DisableUserTwoFactorAuthorization(c *core.CliContext, user
// CheckTransactionAndAccount checks whether all user transactions and all user accounts are correct
func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username string) (bool, error) {
if username == "" {
log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] user name is empty")
log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] user name is empty")
return false, errs.ErrUsernameIsEmpty
}
uid, err := l.getUserIdByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] error occurs when getting user id by user name")
log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] error occurs when getting user id by user name")
return false, err
}
accountMap, categoryMap, tagMap, tagIndexes, tagIndexesMap, err := l.getUserEssentialData(c, uid, username)
if err != nil {
log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] failed to get essential data for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] failed to get essential data for user \"%s\", because %s", username, err.Error())
return false, err
}
@@ -455,7 +455,7 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st
allTransactions, err := l.transactions.GetAllTransactions(c, uid, pageCountForGettingTransactions, false)
if err != nil {
log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] failed to all transactions for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] failed to all transactions for user \"%s\", because %s", username, err.Error())
return false, err
}
@@ -506,7 +506,7 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st
} else if transaction.Type == models.TRANSACTION_DB_TYPE_TRANSFER_IN {
balance = balance + transaction.Amount
} else {
log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] transaction type of transaction \"id:%d\" is invalid", transaction.TransactionId)
log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] transaction type of transaction \"id:%d\" is invalid", transaction.TransactionId)
return false, errs.ErrOperationFailed
}
@@ -521,12 +521,12 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st
}
if !exists && account.Balance != 0 {
log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" balance is not correct, expected balance is %d, but there is no transaction actually", account.AccountId, account.Balance)
log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" balance is not correct, expected balance is %d, but there is no transaction actually", account.AccountId, account.Balance)
return false, errs.ErrOperationFailed
}
if account.Balance != actualBalance {
log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" balance is not correct, expected balance is %d, but actual balance is %d", account.AccountId, account.Balance, actualBalance)
log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" balance is not correct, expected balance is %d, but actual balance is %d", account.AccountId, account.Balance, actualBalance)
return false, errs.ErrOperationFailed
}
}
@@ -535,7 +535,7 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st
_, exists := accountMap[accountId]
if !exists {
log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" does not exist, but there are some transactions of this account actually, and actual balance is %d", accountId, actualBalance)
log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" does not exist, but there are some transactions of this account actually, and actual balance is %d", accountId, actualBalance)
return false, errs.ErrOperationFailed
}
}
@@ -544,7 +544,7 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st
tagIndex := tagIndexes[i]
if tagIndex.TransactionTime < 1 {
log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] transaction tag index \"id:%d\" does not have transaction time", tagIndex.TagIndexId)
log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] transaction tag index \"id:%d\" does not have transaction time", tagIndex.TagIndexId)
return false, errs.ErrOperationFailed
}
}
@@ -555,21 +555,21 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st
// FixTransactionTagIndexWithTransactionTime fixes user transaction tag index data with transaction time
func (l *UserDataCli) FixTransactionTagIndexWithTransactionTime(c *core.CliContext, username string) (bool, error) {
if username == "" {
log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] user name is empty")
log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] user name is empty")
return false, errs.ErrUsernameIsEmpty
}
uid, err := l.getUserIdByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] error occurs when getting user id by user name")
log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] error occurs when getting user id by user name")
return false, err
}
tagIndexes, err := l.tags.GetAllTagIdsOfAllTransactions(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to get tag index for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to get tag index for user \"%s\", because %s", username, err.Error())
return false, err
}
@@ -584,14 +584,14 @@ func (l *UserDataCli) FixTransactionTagIndexWithTransactionTime(c *core.CliConte
}
if len(invalidTagIndexes) < 1 {
log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] all user transaction tag index data has been checked, there is no problem with user data")
log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] all user transaction tag index data has been checked, there is no problem with user data")
return false, errs.ErrOperationFailed
}
allTransactions, err := l.transactions.GetAllTransactions(c, uid, pageCountForGettingTransactions, false)
if err != nil {
log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to all transactions for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to all transactions for user \"%s\", because %s", username, err.Error())
return false, err
}
@@ -611,7 +611,7 @@ func (l *UserDataCli) FixTransactionTagIndexWithTransactionTime(c *core.CliConte
err = l.tags.ModifyTagIndexTransactionTime(c, uid, invalidTagIndexes)
if err != nil {
log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to update transaction tag index for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to update transaction tag index for user \"%s\", because %s", username, err.Error())
return false, err
}
@@ -621,28 +621,28 @@ func (l *UserDataCli) FixTransactionTagIndexWithTransactionTime(c *core.CliConte
// ExportTransaction returns csv file content according user all transactions
func (l *UserDataCli) ExportTransaction(c *core.CliContext, username string, fileType string) ([]byte, error) {
if username == "" {
log.BootErrorf(c, "[user_data.ExportTransaction] user name is empty")
log.CliErrorf(c, "[user_data.ExportTransaction] user name is empty")
return nil, errs.ErrUsernameIsEmpty
}
uid, err := l.getUserIdByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.ExportTransaction] error occurs when getting user id by user name")
log.CliErrorf(c, "[user_data.ExportTransaction] error occurs when getting user id by user name")
return nil, err
}
accountMap, categoryMap, tagMap, _, tagIndexesMap, err := l.getUserEssentialData(c, uid, username)
if err != nil {
log.BootErrorf(c, "[user_data.ExportTransaction] failed to get essential data for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.ExportTransaction] failed to get essential data for user \"%s\", because %s", username, err.Error())
return nil, err
}
allTransactions, err := l.transactions.GetAllTransactions(c, uid, pageCountForDataExport, true)
if err != nil {
log.BootErrorf(c, "[user_data.ExportTransaction] failed to all transactions for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.ExportTransaction] failed to all transactions for user \"%s\", because %s", username, err.Error())
return nil, err
}
@@ -657,7 +657,7 @@ func (l *UserDataCli) ExportTransaction(c *core.CliContext, username string, fil
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())
log.CliErrorf(c, "[user_data.ExportTransaction] failed to get csv format exported data for \"%s\", because %s", username, err.Error())
return nil, err
}
@@ -666,7 +666,7 @@ func (l *UserDataCli) ExportTransaction(c *core.CliContext, username string, fil
func (l *UserDataCli) ImportTransaction(c *core.CliContext, username string, fileType string, data []byte) error {
if username == "" {
log.BootErrorf(c, "[user_data.ImportTransaction] user name is empty")
log.CliErrorf(c, "[user_data.ImportTransaction] user name is empty")
return errs.ErrUsernameIsEmpty
}
@@ -683,44 +683,44 @@ func (l *UserDataCli) ImportTransaction(c *core.CliContext, username string, fil
user, err := l.GetUserByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.ImportTransaction] failed to get user by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.ImportTransaction] failed to get user by user name \"%s\", because %s", username, err.Error())
return err
}
accountMap, categoryMap, tagMap, err := l.getUserEssentialDataForImport(c, user.Uid, username)
if err != nil {
log.BootErrorf(c, "[user_data.ImportTransaction] failed to get essential data for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.ImportTransaction] failed to get essential data for user \"%s\", because %s", username, err.Error())
return err
}
parsedTransactions, 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())
log.CliErrorf(c, "[user_data.ImportTransaction] failed to parse imported data for \"%s\", because %s", username, err.Error())
return err
}
if len(parsedTransactions) < 1 {
log.BootErrorf(c, "[user_data.ImportTransaction] there are no transactions in import file")
log.CliErrorf(c, "[user_data.ImportTransaction] there are no transactions in import file")
return errs.ErrOperationFailed
}
if len(newAccounts) > 0 {
accountNames := l.accounts.GetAccountNames(newAccounts)
log.BootErrorf(c, "[user_data.ImportTransaction] there are %d accounts (%s) need to be created, please create them manually", len(newAccounts), strings.Join(accountNames, ","))
log.CliErrorf(c, "[user_data.ImportTransaction] there are %d accounts (%s) need to be created, please create them manually", len(newAccounts), strings.Join(accountNames, ","))
return errs.ErrOperationFailed
}
if len(newCategories) > 0 {
categoryNames := l.categories.GetCategoryNames(newCategories)
log.BootErrorf(c, "[user_data.ImportTransaction] there are %d transaction categories (%s) need to be created, please create them manually", len(newCategories), strings.Join(categoryNames, ","))
log.CliErrorf(c, "[user_data.ImportTransaction] there are %d transaction categories (%s) need to be created, please create them manually", len(newCategories), strings.Join(categoryNames, ","))
return errs.ErrOperationFailed
}
if len(newTags) > 0 {
tagNames := l.tags.GetTagNames(newTags)
log.BootErrorf(c, "[user_data.ImportTransaction] there are %d transaction tags (%s) need to be created, please create them manually", len(newTags), strings.Join(tagNames, ","))
log.CliErrorf(c, "[user_data.ImportTransaction] there are %d transaction tags (%s) need to be created, please create them manually", len(newTags), strings.Join(tagNames, ","))
return errs.ErrOperationFailed
}
@@ -728,14 +728,14 @@ func (l *UserDataCli) ImportTransaction(c *core.CliContext, username string, fil
newTransactionTagIdsMap, err := parsedTransactions.ToTransactionTagIdsMap()
if err != nil {
log.BootErrorf(c, "[user_data.ImportTransaction] failed to get transaction tag ids map, because %s", err.Error())
log.CliErrorf(c, "[user_data.ImportTransaction] failed to get transaction tag ids map, because %s", err.Error())
return errs.ErrOperationFailed
}
err = l.transactions.BatchCreateTransactions(c, user.Uid, newTransactions, newTransactionTagIdsMap)
if err != nil {
log.BootErrorf(c, "[user_data.ImportTransaction] failed to create transaction, because %s", err.Error())
log.CliErrorf(c, "[user_data.ImportTransaction] failed to create transaction, because %s", err.Error())
return err
}
@@ -746,7 +746,7 @@ func (l *UserDataCli) getUserIdByUsername(c *core.CliContext, username string) (
user, err := l.GetUserByUsername(c, username)
if err != nil {
log.BootErrorf(c, "[user_data.getUserIdByUsername] failed to get user by user name \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.getUserIdByUsername] failed to get user by user name \"%s\", because %s", username, err.Error())
return 0, err
}
@@ -755,14 +755,14 @@ func (l *UserDataCli) getUserIdByUsername(c *core.CliContext, username string) (
func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, username string) (accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, tagIndexes []*models.TransactionTagIndex, tagIndexesMap map[int64][]int64, err error) {
if uid <= 0 {
log.BootErrorf(c, "[user_data.getUserEssentialData] user uid \"%d\" is invalid", uid)
log.CliErrorf(c, "[user_data.getUserEssentialData] user uid \"%d\" is invalid", uid)
return nil, nil, nil, nil, nil, errs.ErrUserIdInvalid
}
accounts, err := l.accounts.GetAllAccountsByUid(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.getUserEssentialData] failed to get accounts for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.getUserEssentialData] failed to get accounts for user \"%s\", because %s", username, err.Error())
return nil, nil, nil, nil, nil, err
}
@@ -771,7 +771,7 @@ func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, userna
categories, err := l.categories.GetAllCategoriesByUid(c, uid, 0, -1)
if err != nil {
log.BootErrorf(c, "[user_data.getUserEssentialData] failed to get categories for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.getUserEssentialData] failed to get categories for user \"%s\", because %s", username, err.Error())
return nil, nil, nil, nil, nil, err
}
@@ -780,7 +780,7 @@ func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, userna
tags, err := l.tags.GetAllTagsByUid(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.getUserEssentialData] failed to get tags for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.getUserEssentialData] failed to get tags for user \"%s\", because %s", username, err.Error())
return nil, nil, nil, nil, nil, err
}
@@ -789,7 +789,7 @@ func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, userna
tagIndexes, err = l.tags.GetAllTagIdsOfAllTransactions(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.getUserEssentialData] failed to get tag index for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.getUserEssentialData] failed to get tag index for user \"%s\", because %s", username, err.Error())
return nil, nil, nil, nil, nil, err
}
@@ -800,14 +800,14 @@ func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, userna
func (l *UserDataCli) getUserEssentialDataForImport(c *core.CliContext, uid int64, username string) (accountMap map[string]*models.Account, categoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag, err error) {
if uid <= 0 {
log.BootErrorf(c, "[user_data.getUserEssentialDataForImport] user uid \"%d\" is invalid", uid)
log.CliErrorf(c, "[user_data.getUserEssentialDataForImport] user uid \"%d\" is invalid", uid)
return nil, nil, nil, errs.ErrUserIdInvalid
}
accounts, err := l.accounts.GetAllAccountsByUid(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get accounts for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get accounts for user \"%s\", because %s", username, err.Error())
return nil, nil, nil, err
}
@@ -816,7 +816,7 @@ func (l *UserDataCli) getUserEssentialDataForImport(c *core.CliContext, uid int6
categories, err := l.categories.GetAllCategoriesByUid(c, uid, 0, -1)
if err != nil {
log.BootErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get categories for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get categories for user \"%s\", because %s", username, err.Error())
return nil, nil, nil, err
}
@@ -825,7 +825,7 @@ func (l *UserDataCli) getUserEssentialDataForImport(c *core.CliContext, uid int6
tags, err := l.tags.GetAllTagsByUid(c, uid)
if err != nil {
log.BootErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get tags for user \"%s\", because %s", username, err.Error())
log.CliErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get tags for user \"%s\", because %s", username, err.Error())
return nil, nil, nil, err
}
@@ -838,12 +838,12 @@ func (l *UserDataCli) checkTransactionAccount(c *core.CliContext, transaction *m
account, exists := accountMap[transaction.AccountId]
if !exists {
log.BootErrorf(c, "[user_data.checkTransactionAccount] the account \"id:%d\" of transaction \"id:%d\" does not exist", transaction.AccountId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionAccount] the account \"id:%d\" of transaction \"id:%d\" does not exist", transaction.AccountId, transaction.TransactionId)
return errs.ErrAccountNotFound
}
if account.ParentAccountId == models.LevelOneAccountParentId && accountHasChild[account.AccountId] {
log.BootErrorf(c, "[user_data.checkTransactionAccount] the account \"id:%d\" of transaction \"id:%d\" is not a sub-account", transaction.AccountId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionAccount] the account \"id:%d\" of transaction \"id:%d\" is not a sub-account", transaction.AccountId, transaction.TransactionId)
return errs.ErrOperationFailed
}
@@ -851,12 +851,12 @@ func (l *UserDataCli) checkTransactionAccount(c *core.CliContext, transaction *m
relatedAccount, exists := accountMap[transaction.RelatedAccountId]
if !exists {
log.BootErrorf(c, "[user_data.checkTransactionAccount] the related account \"id:%d\" of transaction \"id:%d\" does not exist", transaction.RelatedAccountId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionAccount] the related account \"id:%d\" of transaction \"id:%d\" does not exist", transaction.RelatedAccountId, transaction.TransactionId)
return errs.ErrAccountNotFound
}
if relatedAccount.ParentAccountId == models.LevelOneAccountParentId && accountHasChild[relatedAccount.AccountId] {
log.BootErrorf(c, "[user_data.checkTransactionAccount] the related account \"id:%d\" of transaction \"id:%d\" is not a sub-account", transaction.RelatedAccountId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionAccount] the related account \"id:%d\" of transaction \"id:%d\" is not a sub-account", transaction.RelatedAccountId, transaction.TransactionId)
return errs.ErrOperationFailed
}
}
@@ -867,7 +867,7 @@ func (l *UserDataCli) checkTransactionAccount(c *core.CliContext, transaction *m
func (l *UserDataCli) checkTransactionCategory(c *core.CliContext, transaction *models.Transaction, categoryMap map[int64]*models.TransactionCategory) error {
if transaction.Type == models.TRANSACTION_DB_TYPE_MODIFY_BALANCE {
if transaction.CategoryId > 0 {
log.BootErrorf(c, "[user_data.checkTransactionCategory] transaction \"id:%d\" is balance modification transaction, but has category \"id:%d\"", transaction.TransactionId, transaction.CategoryId)
log.CliErrorf(c, "[user_data.checkTransactionCategory] transaction \"id:%d\" is balance modification transaction, but has category \"id:%d\"", transaction.TransactionId, transaction.CategoryId)
return errs.ErrBalanceModificationTransactionCannotSetCategory
} else {
return nil
@@ -877,12 +877,12 @@ func (l *UserDataCli) checkTransactionCategory(c *core.CliContext, transaction *
category, exists := categoryMap[transaction.CategoryId]
if !exists {
log.BootErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" does not exist", transaction.CategoryId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" does not exist", transaction.CategoryId, transaction.TransactionId)
return errs.ErrTransactionCategoryNotFound
}
if category.ParentCategoryId == models.LevelOneTransactionParentId {
log.BootErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" is not a sub category", transaction.CategoryId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" is not a sub category", transaction.CategoryId, transaction.TransactionId)
return errs.ErrOperationFailed
}
@@ -901,7 +901,7 @@ func (l *UserDataCli) checkTransactionTag(c *core.CliContext, transactionId int6
tag, exists := tagMap[tagIndex]
if !exists {
log.BootErrorf(c, "[user_data.checkTransactionTag] the transaction tag \"id:%d\" of transaction \"id:%d\" does not exist", tag.TagId, transactionId)
log.CliErrorf(c, "[user_data.checkTransactionTag] the transaction tag \"id:%d\" of transaction \"id:%d\" does not exist", tag.TagId, transactionId)
return errs.ErrTransactionTagNotFound
}
}
@@ -917,22 +917,22 @@ func (l *UserDataCli) checkTransactionRelatedTransaction(c *core.CliContext, tra
relatedTransaction, exists := transactionMap[transaction.RelatedId]
if !exists {
log.BootErrorf(c, "[user_data.checkTransactionRelatedTransaction] the related transaction \"id:%d\" of transaction \"id:%d\" does not exist", transaction.RelatedId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionRelatedTransaction] the related transaction \"id:%d\" of transaction \"id:%d\" does not exist", transaction.RelatedId, transaction.TransactionId)
return errs.ErrTransactionNotFound
}
if transaction.RelatedId != relatedTransaction.TransactionId || transaction.TransactionId != relatedTransaction.RelatedId {
log.BootErrorf(c, "[user_data.checkTransactionRelatedTransaction] related ids of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionRelatedTransaction] related ids of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId)
return errs.ErrOperationFailed
}
if transaction.RelatedAccountId != relatedTransaction.AccountId || transaction.AccountId != relatedTransaction.RelatedAccountId {
log.BootErrorf(c, "[user_data.checkTransactionRelatedTransaction] related account ids of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionRelatedTransaction] related account ids of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId)
return errs.ErrOperationFailed
}
if transaction.RelatedAccountAmount != relatedTransaction.Amount || transaction.Amount != relatedTransaction.RelatedAccountAmount {
log.BootErrorf(c, "[user_data.checkTransactionRelatedTransaction] related amounts of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId)
log.CliErrorf(c, "[user_data.checkTransactionRelatedTransaction] related amounts of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId)
return errs.ErrOperationFailed
}
@@ -940,7 +940,7 @@ func (l *UserDataCli) checkTransactionRelatedTransaction(c *core.CliContext, tra
relatedAccount := accountMap[transaction.RelatedAccountId]
if account.Currency == relatedAccount.Currency && transaction.Amount != transaction.RelatedAccountAmount {
log.BootWarnf(c, "[user_data.checkTransactionRelatedTransaction] transfer-in amount and transfer-out amount of transaction \"id:%d\" are not equal", transaction.TransactionId)
log.CliWarnf(c, "[user_data.checkTransactionRelatedTransaction] transfer-in amount and transfer-out amount of transaction \"id:%d\" are not equal", transaction.TransactionId)
}
return nil