WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 245fdd7

Browse files
committed
clear all transactions of specified account (#228)
1 parent cbe7841 commit 245fdd7

File tree

25 files changed

+397
-5
lines changed

25 files changed

+397
-5
lines changed

cmd/webserver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ func startWebServer(c *core.CliContext) error {
325325
apiV1Route.GET("/data/statistics.json", bindApi(api.DataManagements.DataStatisticsHandler))
326326
apiV1Route.POST("/data/clear/all.json", bindApi(api.DataManagements.ClearAllDataHandler))
327327
apiV1Route.POST("/data/clear/transactions.json", bindApi(api.DataManagements.ClearAllTransactionsHandler))
328+
apiV1Route.POST("/data/clear/transactions/by_account.json", bindApi(api.DataManagements.ClearAllTransactionsByAccountHandler))
328329

329330
if config.EnableDataExport {
330331
apiV1Route.GET("/data/export.csv", bindCsv(api.DataManagements.ExportDataToEzbookkeepingCSVHandler))

pkg/api/data_managements.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/mayswind/ezbookkeeping/pkg/utils"
1616
)
1717

18+
const pageCountForClearTransactions = 1000
1819
const pageCountForDataExport = 1000
1920

2021
// DataManagementsApi represents data management api
@@ -232,6 +233,61 @@ func (a *DataManagementsApi) ClearAllTransactionsHandler(c *core.WebContext) (an
232233
return true, nil
233234
}
234235

236+
// ClearAllTransactionsByAccountHandler deletes all transactions of specified account
237+
func (a *DataManagementsApi) ClearAllTransactionsByAccountHandler(c *core.WebContext) (any, *errs.Error) {
238+
var clearDataReq models.ClearAccountTransactionsRequest
239+
err := c.ShouldBindJSON(&clearDataReq)
240+
241+
if err != nil {
242+
log.Warnf(c, "[data_managements.ClearAllTransactionsByAccountHandler] parse request failed, because %s", err.Error())
243+
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
244+
}
245+
246+
uid := c.GetCurrentUid()
247+
user, err := a.users.GetUserById(c, uid)
248+
249+
if err != nil {
250+
if !errs.IsCustomError(err) {
251+
log.Warnf(c, "[data_managements.ClearAllTransactionsByAccountHandler] failed to get user for user \"uid:%d\", because %s", uid, err.Error())
252+
}
253+
254+
return nil, errs.ErrUserNotFound
255+
}
256+
257+
if !a.users.IsPasswordEqualsUserPassword(clearDataReq.Password, user) {
258+
return nil, errs.ErrUserPasswordWrong
259+
}
260+
261+
if user.FeatureRestriction.Contains(core.USER_FEATURE_RESTRICTION_TYPE_CLEAR_ALL_DATA) {
262+
return nil, errs.ErrNotPermittedToPerformThisAction
263+
}
264+
265+
account, err := a.accounts.GetAccountByAccountId(c, uid, clearDataReq.AccountId)
266+
267+
if err != nil {
268+
log.Errorf(c, "[data_managements.ClearAllTransactionsByAccountHandler] failed to get account \"id:%d\" for user \"uid:%d\", because %s", uid, clearDataReq.AccountId, err.Error())
269+
return nil, errs.Or(err, errs.ErrOperationFailed)
270+
}
271+
272+
if account.Hidden {
273+
return nil, errs.ErrCannotDeleteTransactionInHiddenAccount
274+
}
275+
276+
if account.Type == models.ACCOUNT_TYPE_MULTI_SUB_ACCOUNTS {
277+
return nil, errs.ErrCannotDeleteTransactionInParentAccount
278+
}
279+
280+
err = a.transactions.DeleteAllTransactionsOfAccount(c, uid, account.AccountId, pageCountForClearTransactions)
281+
282+
if err != nil {
283+
log.Errorf(c, "[data_managements.ClearAllTransactionsByAccountHandler] failed to delete all transactions in account \"id:%d\", because %s", account.AccountId, err.Error())
284+
return nil, errs.Or(err, errs.ErrOperationFailed)
285+
}
286+
287+
log.Infof(c, "[data_managements.ClearAllTransactionsByAccountHandler] user \"uid:%d\" has cleared all transactions in account \"id:%d\"", uid, account.AccountId)
288+
return true, nil
289+
}
290+
235291
func (a *DataManagementsApi) getExportedFileContent(c *core.WebContext, fileType string) ([]byte, string, *errs.Error) {
236292
if !a.CurrentConfig().EnableDataExport {
237293
return nil, "", errs.ErrDataExportNotAllowed

pkg/models/data_management.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ type ClearDataRequest struct {
55
Password string `json:"password" binding:"omitempty,min=6,max=128"`
66
}
77

8+
// ClearAccountTransactionsRequest represents all parameters of clear transaction data of a specific account request
9+
type ClearAccountTransactionsRequest struct {
10+
AccountId int64 `json:"accountId,string" binding:"required,min=1"`
11+
Password string `json:"password" binding:"omitempty,min=6,max=128"`
12+
}
13+
814
// DataStatisticsResponse represents a view-object of user data statistic
915
type DataStatisticsResponse struct {
1016
TotalAccountCount int64 `json:"totalAccountCount,string"`

pkg/services/transactions.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,43 @@ func (s *TransactionService) DeleteAllTransactions(c core.Context, uid int64, de
13811381
})
13821382
}
13831383

1384+
// DeleteAllTransactionsOfAccount deletes all existed transactions of specific account from database
1385+
func (s *TransactionService) DeleteAllTransactionsOfAccount(c core.Context, uid int64, accountId int64, pageCount int32) error {
1386+
if uid <= 0 {
1387+
return errs.ErrUserIdInvalid
1388+
}
1389+
1390+
if accountId <= 0 {
1391+
return errs.ErrAccountIdInvalid
1392+
}
1393+
1394+
transactions, err := s.GetAllSpecifiedTransactions(c, uid, 0, 0, 0, nil, []int64{accountId}, nil, false, models.TRANSACTION_TAG_FILTER_HAS_ANY, "", "", pageCount, true)
1395+
1396+
if err != nil {
1397+
return err
1398+
}
1399+
1400+
if len(transactions) < 1 {
1401+
return nil
1402+
}
1403+
1404+
for i := 0; i < len(transactions); i++ {
1405+
transaction := transactions[i]
1406+
1407+
if transaction.Type == models.TRANSACTION_DB_TYPE_TRANSFER_IN {
1408+
err = s.DeleteTransaction(c, uid, transaction.RelatedId)
1409+
} else {
1410+
err = s.DeleteTransaction(c, uid, transaction.TransactionId)
1411+
}
1412+
1413+
if err != nil {
1414+
return err
1415+
}
1416+
}
1417+
1418+
return nil
1419+
}
1420+
13841421
// GetRelatedTransferTransaction returns the related transaction for transfer transaction
13851422
func (s *TransactionService) GetRelatedTransferTransaction(originalTransaction *models.Transaction) *models.Transaction {
13861423
var relatedType models.TransactionDbType

src/components/mobile/PasswordInputSheet.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<f7-button large fill
2727
:class="{ 'disabled': !currentPassword || confirmDisabled }"
2828
:color="color || 'primary'"
29-
:text="tt('Continue')"
29+
:text="tt('Confirm')"
3030
@click="confirm">
3131
</f7-button>
3232
<div class="margin-top text-align-center">

src/consts/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const DEFAULT_API_TIMEOUT: number = 10000; // 10s
77
export const DEFAULT_UPLOAD_API_TIMEOUT: number = 30000; // 30s
88
export const DEFAULT_EXPORT_API_TIMEOUT: number = 180000; // 180s
99
export const DEFAULT_IMPORT_API_TIMEOUT: number = 1800000; // 1800s
10+
export const DEFAULT_CLEAR_ALL_TRANSACTIONS_API_TIMEOUT: number = 1800000; // 1800s
1011
export const DEFAULT_LLM_API_TIMEOUT: number = 600000; // 600s
1112

1213
export const GOOGLE_MAP_JAVASCRIPT_URL: string = 'https://maps.googleapis.com/maps/api/js';

src/lib/services.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
DEFAULT_UPLOAD_API_TIMEOUT,
2222
DEFAULT_EXPORT_API_TIMEOUT,
2323
DEFAULT_IMPORT_API_TIMEOUT,
24+
DEFAULT_CLEAR_ALL_TRANSACTIONS_API_TIMEOUT,
2425
DEFAULT_LLM_API_TIMEOUT,
2526
GOOGLE_MAP_JAVASCRIPT_URL,
2627
BAIDU_MAP_JAVASCRIPT_URL,
@@ -42,6 +43,7 @@ import type {
4243
import type {
4344
ExportTransactionDataRequest,
4445
ClearDataRequest,
46+
ClearAccountTransactionsRequest,
4547
DataStatisticsResponse
4648
} from '@/models/data_management.ts';
4749
import type {
@@ -383,10 +385,19 @@ export default {
383385
}
384386
},
385387
clearAllData: (req: ClearDataRequest): ApiResponsePromise<boolean> => {
386-
return axios.post<ApiResponse<boolean>>('v1/data/clear/all.json', req);
388+
return axios.post<ApiResponse<boolean>>('v1/data/clear/all.json', req, {
389+
timeout: DEFAULT_CLEAR_ALL_TRANSACTIONS_API_TIMEOUT
390+
} as ApiRequestConfig);
387391
},
388392
clearAllTransactions: (req: ClearDataRequest): ApiResponsePromise<boolean> => {
389-
return axios.post<ApiResponse<boolean>>('v1/data/clear/transactions.json', req);
393+
return axios.post<ApiResponse<boolean>>('v1/data/clear/transactions.json', req, {
394+
timeout: DEFAULT_CLEAR_ALL_TRANSACTIONS_API_TIMEOUT
395+
} as ApiRequestConfig);
396+
},
397+
clearAllTransactionsOfAccount: (req: ClearAccountTransactionsRequest): ApiResponsePromise<boolean> => {
398+
return axios.post<ApiResponse<boolean>>('v1/data/clear/transactions/by_account.json', req, {
399+
timeout: DEFAULT_CLEAR_ALL_TRANSACTIONS_API_TIMEOUT
400+
} as ApiRequestConfig);
390401
},
391402
getAllAccounts: ({ visibleOnly }: { visibleOnly: boolean }): ApiResponsePromise<AccountInfoResponse[]> => {
392403
return axios.get<ApiResponse<AccountInfoResponse[]>>('v1/accounts/list.json?visible_only=' + visibleOnly);

src/locales/de.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"confirmImportTransactions": "Sind Sie sicher, dass Sie {count} Transaktionen importieren möchten?",
125125
"importingTransactions": "Importing ({process}%)",
126126
"importTransactionResult": "Sie haben {count} Transaktionen erfolgreich importiert.",
127+
"clearTransactionsInAccountTip": "You CANNOT undo this action. This will clear your transactions data in {account}. Please enter your current password to confirm.",
127128
"accountActivationAndResendValidationEmailTip": "Ein Aktivierungslink wurde an Ihre E-Mail-Adresse gesendet: {email}. Wenn Sie die E-Mail nicht erhalten haben, geben Sie bitte das Passwort erneut ein und klicken Sie auf die Schaltfläche unten, um die Bestätigungs-E-Mail erneut zu senden.",
128129
"resendValidationEmailTip": "Wenn Sie die E-Mail nicht erhalten haben, geben Sie bitte das Passwort erneut ein und klicken Sie auf die Schaltfläche unten, um die Bestätigungs-E-Mail an: {email} erneut zu senden."
129130
}
@@ -1409,6 +1410,7 @@
14091410
"Continue": "Weiter",
14101411
"Previous": "Zurück",
14111412
"Next": "Weiter",
1413+
"Confirm": "Confirm",
14121414
"Status": "Status",
14131415
"Enable": "Aktivieren",
14141416
"Enabled": "Aktiviert",
@@ -2098,6 +2100,7 @@
20982100
"You CANNOT undo this action. This will clear your transactions data. Please enter your current password to confirm.": "You CANNOT undo this action. This will clear your transactions data. Please enter your current password to confirm.",
20992101
"You CANNOT undo this action. This will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.": "Diese Aktion kann NICHT rückgängig gemacht werden. Dies wird Ihre Konten, Kategorien, Tags und Transaktionsdaten löschen. Bitte geben Sie Ihr aktuelles Passwort ein, um zu bestätigen.",
21002102
"You CANNOT undo this action. \"Clear All Transactions\" will clear all your transactions data, and \"Clear All Data\" will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.": "You CANNOT undo this action. \"Clear All Transactions\" will clear all your transactions data, and \"Clear All Data\" will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.",
2103+
"All transactions in this account has been cleared": "All transactions in this account has been cleared",
21012104
"All transactions has been cleared": "All transactions has been cleared",
21022105
"All user data has been cleared": "Alle Benutzerdaten wurden gelöscht",
21032106
"Unable to clear user data": "Benutzerdaten können nicht gelöscht werden",

src/locales/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"confirmImportTransactions": "Are you sure you want to import {count} transactions?",
125125
"importingTransactions": "Importing ({process}%)",
126126
"importTransactionResult": "You have imported {count} transactions successfully.",
127+
"clearTransactionsInAccountTip": "You CANNOT undo this action. This will clear your transactions data in {account}. Please enter your current password to confirm.",
127128
"accountActivationAndResendValidationEmailTip": "Account activation link has been sent to your email address: {email}, If you don't receive the mail, please fill password again and click the button below to resend the validation mail.",
128129
"resendValidationEmailTip": "If you don't receive the mail, please fill password again and click the button below to resend the validation mail to: {email}"
129130
}
@@ -1409,6 +1410,7 @@
14091410
"Continue": "Continue",
14101411
"Previous": "Previous",
14111412
"Next": "Next",
1413+
"Confirm": "Confirm",
14121414
"Status": "Status",
14131415
"Enable": "Enable",
14141416
"Enabled": "Enabled",
@@ -2098,6 +2100,7 @@
20982100
"You CANNOT undo this action. This will clear your transactions data. Please enter your current password to confirm.": "You CANNOT undo this action. This will clear your transactions data. Please enter your current password to confirm.",
20992101
"You CANNOT undo this action. This will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.": "You CANNOT undo this action. This will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.",
21002102
"You CANNOT undo this action. \"Clear All Transactions\" will clear all your transactions data, and \"Clear All Data\" will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.": "You CANNOT undo this action. \"Clear All Transactions\" will clear all your transactions data, and \"Clear All Data\" will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.",
2103+
"All transactions in this account has been cleared": "All transactions in this account has been cleared",
21012104
"All transactions has been cleared": "All transactions has been cleared",
21022105
"All user data has been cleared": "All user data has been cleared",
21032106
"Unable to clear user data": "Unable to clear user data",

src/locales/es.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"confirmImportTransactions": "¿Está seguro de que desea importar {count} transacciones?",
125125
"importingTransactions": "Importing ({process}%)",
126126
"importTransactionResult": "Ha importado {count} transacciones correctamente.",
127+
"clearTransactionsInAccountTip": "You CANNOT undo this action. This will clear your transactions data in {account}. Please enter your current password to confirm.",
127128
"accountActivationAndResendValidationEmailTip": "El enlace de activación de la cuenta se envió a su dirección de correo electrónico: {email}. Si no recibe el correo, ingrese la contraseña nuevamente y haga clic en el botón a continuación para reenviar el correo de validación.",
128129
"resendValidationEmailTip": "Si no recibe el correo, complete nuevamente la contraseña y haga clic en el botón a continuación para reenviar el correo de validación a: {email}"
129130
}
@@ -1409,6 +1410,7 @@
14091410
"Continue": "Continuar",
14101411
"Previous": "Anterior",
14111412
"Next": "Siguiente",
1413+
"Confirm": "Confirm",
14121414
"Status": "Estado",
14131415
"Enable": "Activar",
14141416
"Enabled": "Activado",
@@ -2098,6 +2100,7 @@
20982100
"You CANNOT undo this action. This will clear your transactions data. Please enter your current password to confirm.": "You CANNOT undo this action. This will clear your transactions data. Please enter your current password to confirm.",
20992101
"You CANNOT undo this action. This will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.": "NO PUEDE deshacer esta acción. Esto borrará sus cuentas, categorías, etiquetas y datos de transacciones. Por favor ingrese su contraseña actual para confirmar.",
21002102
"You CANNOT undo this action. \"Clear All Transactions\" will clear all your transactions data, and \"Clear All Data\" will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.": "You CANNOT undo this action. \"Clear All Transactions\" will clear all your transactions data, and \"Clear All Data\" will clear your accounts, categories, tags and transactions data. Please enter your current password to confirm.",
2103+
"All transactions in this account has been cleared": "All transactions in this account has been cleared",
21012104
"All transactions has been cleared": "All transactions has been cleared",
21022105
"All user data has been cleared": "Todos los datos del usuario han sido borrados.",
21032106
"Unable to clear user data": "No se pueden borrar los datos del usuario",

0 commit comments

Comments
 (0)