Simplified delete transaction
MODIFY
src/server/rest/service/TransactionService.ts
src/server/rest/service/TransactionService.ts
@@ -255,8 +255,7 @@ export default class TransactionService {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.DELETE,
- entity: Entity.TRANSACTION,
+ action: Action.DELETE, entity: Entity.TRANSACTION,
module: MODULE_NAME, method: 'handleDeleteTransaction',
value: transactionId.toString()
});
@@ -669,37 +668,49 @@ export default class TransactionService {
inSuccess: 0,
inError: 0
};
- const specificError: { refunded: number; notFound: number; billed: number, refundedIDs: number[]; notFoundIDs: number[], billedIDs: number[] } = {
- refunded: 0,
- notFound: 0,
- billed: 0,
- refundedIDs: [],
- notFoundIDs: [],
- billedIDs: []
- };
// Check if transaction has been refunded
const refundConnector = await RefundFactory.getRefundImpl(loggedUser.tenantID);
const billingImpl = await BillingFactory.getBillingImpl(loggedUser.tenantID);
- for (const transactionId of transactionsIDs) {
+ for (const transactionID of transactionsIDs) {
// Get
- const transaction = await TransactionStorage.getTransaction(loggedUser.tenantID, transactionId);
+ const transaction = await TransactionStorage.getTransaction(loggedUser.tenantID, transactionID);
// Not Found
if (!transaction) {
result.inError++;
- specificError.notFound++;
- specificError.notFoundIDs.push(transactionId);
+ Logging.logError({
+ tenantID: loggedUser.tenantID,
+ user: loggedUser,
+ module: MODULE_NAME, method: 'handleDeleteTransactions',
+ message: `Transaction ID '${transactionID}' does not exist`,
+ action: action,
+ detailedMessages: { transaction }
+ });
// Already Refunded
} else if (refundConnector && !refundConnector.canBeDeleted(transaction)) {
result.inError++;
- specificError.refunded++;
- specificError.refundedIDs.push(transactionId);
- } else if (billingImpl && transaction.billingData.invoiceID && await BillingStorage.getInvoice(loggedUser.tenantID, transaction.billingData.invoiceID)) {
+ Logging.logError({
+ tenantID: loggedUser.tenantID,
+ user: loggedUser,
+ module: MODULE_NAME, method: 'handleDeleteTransactions',
+ message: `Transaction ID '${transactionID}' has been refunded and cannot be deleted`,
+ action: action,
+ detailedMessages: { transaction }
+ });
+ // Billed
+ } else if (billingImpl && transaction.billingData && transaction.billingData.invoiceID) {
result.inError++;
- specificError.billed++;
- specificError.billedIDs.push(transactionId);
+ Logging.logError({
+ tenantID: loggedUser.tenantID,
+ user: loggedUser,
+ module: MODULE_NAME, method: 'handleDeleteTransactions',
+ message: `Transaction ID '${transactionID}' has been billed and cannot be deleted`,
+ action: action,
+ detailedMessages: { transaction }
+ });
+ // Transaction in progress
} else if (!transaction.stop) {
if (!transaction.chargeBox) {
- transactionsIDsToDelete.push(transactionId);
+ transactionsIDsToDelete.push(transactionID);
} else {
// Check connector
const foundConnector = Utils.getConnectorFromID(transaction.chargeBox, transaction.connectorId);
@@ -709,46 +720,25 @@ export default class TransactionService {
await ChargingStationStorage.saveChargingStation(loggedUser.tenantID, transaction.chargeBox);
}
// To Delete
- transactionsIDsToDelete.push(transactionId);
+ transactionsIDsToDelete.push(transactionID);
}
+ // Ok
} else {
- // To Delete
- transactionsIDsToDelete.push(transactionId);
+ transactionsIDsToDelete.push(transactionID);
}
}
// Delete All Transactions
result.inSuccess = await TransactionStorage.deleteTransactions(loggedUser.tenantID, transactionsIDsToDelete);
- // Adjust
- result.inError += transactionsIDsToDelete.length - result.inSuccess;
// Log
- if (result.inError > 0) {
- const errorDetails = [];
- if (specificError.notFound) {
- errorDetails.push(`${specificError.notFound} session IDs have not been found: ${specificError.notFoundIDs.join(', ')}`);
- }
- if (specificError.refunded) {
- errorDetails.push(`${specificError.refunded} session IDs has been refunded and cannot be deleted: ${specificError.refundedIDs.join(', ')}`);
- }
- if (specificError.billed) {
- errorDetails.push(`${specificError.billed} session IDs has been billed and cannot be deleted: ${specificError.billedIDs.join(', ')}`);
- }
- Logging.logError({
- tenantID: loggedUser.tenantID,
- user: loggedUser,
- module: MODULE_NAME, method: 'handleDeleteTransactions',
- message: `${result.inSuccess} transaction(s) have been deleted successfully and ${result.inError} encountered an error or cannot be deleted`,
- action: action,
- detailedMessages: { errorDetails }
- });
- } else {
- Logging.logInfo({
- tenantID: loggedUser.tenantID,
- user: loggedUser,
- module: MODULE_NAME, method: 'handleDeleteTransactions',
- message: `${result.inSuccess} transaction(s) have been deleted successfully`,
- action: action
- });
- }
+ // Log
+ Utils.logActionsResponse(loggedUser.tenantID,
+ ServerAction.TRANSACTIONS_DELETE,
+ MODULE_NAME, 'synchronizeCarCatalogs', result,
+ '{{inSuccess}} transaction(s) were successfully deleted',
+ '{{inError}} transaction(s) failed to be deleted',
+ '{{inSuccess}} transaction(s) were successfully deleted and {{inError}} failed to be deleted',
+ 'No transactions have been deleted'
+ );
return result;
}

Simplified delete transaction