-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdelete-row-from-table.use.case.ts
More file actions
176 lines (159 loc) · 6.58 KB
/
delete-row-from-table.use.case.ts
File metadata and controls
176 lines (159 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { getDataAccessObject } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/create-data-access-object.js';
import { buildDAOsTableSettingsDs } from '@rocketadmin/shared-code/dist/src/helpers/data-structures-builders/table-settings.ds.builder.js';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { AmplitudeEventTypeEnum } from '../../../enums/amplitude-event-type.enum.js';
import { LogOperationTypeEnum } from '../../../enums/log-operation-type.enum.js';
import { OperationResultStatusEnum } from '../../../enums/operation-result-status.enum.js';
import { TableActionEventEnum } from '../../../enums/table-action-event-enum.js';
import { DeleteRowException } from '../../../exceptions/custom-exceptions/delete-row-exception.js';
import { ExceptionOperations } from '../../../exceptions/custom-exceptions/exception-operation.js';
import { UnknownSQLException } from '../../../exceptions/custom-exceptions/unknown-sql-exception.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { compareArrayElements } from '../../../helpers/compare-array-elements.js';
import { AmplitudeService } from '../../amplitude/amplitude.service.js';
import { isTestConnectionUtil } from '../../connection/utils/is-test-connection-util.js';
import { TableActionActivationService } from '../../table-actions/table-actions-module/table-action-activation.service.js';
import { TableLogsService } from '../../table-logs/table-logs.service.js';
import { DeleteRowFromTableDs } from '../application/data-structures/delete-row-from-table.ds.js';
import { DeletedRowFromTableDs } from '../application/data-structures/deleted-row-from-table.ds.js';
import { convertHexDataInPrimaryKeyUtil } from '../utils/convert-hex-data-in-primary-key.util.js';
import { getUserEmailForAgent, validateConnection } from '../utils/validate-connection.util.js';
import { IDeleteRowFromTable } from './table-use-cases.interface.js';
@Injectable()
export class DeleteRowFromTableUseCase
extends AbstractUseCase<DeleteRowFromTableDs, DeletedRowFromTableDs>
implements IDeleteRowFromTable
{
constructor(
@Inject(BaseType.GLOBAL_DB_CONTEXT)
protected _dbContext: IGlobalDatabaseContext,
private amplitudeService: AmplitudeService,
private tableLogsService: TableLogsService,
private tableActionActivationService: TableActionActivationService,
) {
super();
}
protected async implementation(inputData: DeleteRowFromTableDs): Promise<DeletedRowFromTableDs> {
// eslint-disable-next-line prefer-const
let { connectionId, masterPwd, primaryKey, tableName, userId } = inputData;
const { uncached } = inputData;
let operationResult = OperationResultStatusEnum.unknown;
if (!primaryKey) {
throw new HttpException(
{
message: Messages.PRIMARY_KEY_MISSING,
},
HttpStatus.BAD_REQUEST,
);
}
const connection = await this._dbContext.connectionRepository.findAndDecryptConnection(connectionId, masterPwd);
validateConnection(connection);
const dao = getDataAccessObject(connection);
if (uncached) {
dao.invalidateMetadataCache();
}
const userEmail = await getUserEmailForAgent(connection, userId, this._dbContext.userRepository);
const isView = await dao.isView(tableName, userEmail);
if (isView) {
throw new HttpException(
{
message: Messages.CANT_UPDATE_TABLE_VIEW,
},
HttpStatus.BAD_REQUEST,
);
}
const [tableStructure, primaryColumns] = await Promise.all([
dao.getTableStructure(tableName, userEmail),
dao.getTablePrimaryColumns(tableName, userEmail),
]);
primaryKey = convertHexDataInPrimaryKeyUtil(primaryKey, tableStructure);
const availablePrimaryColumns: Array<string> = primaryColumns.map((column) => column.column_name);
Object.keys(primaryKey).forEach((key) => {
// eslint-disable-next-line security/detect-object-injection
if (!primaryKey[key] && primaryKey[key] !== '') {
// eslint-disable-next-line security/detect-object-injection
delete primaryKey[key];
}
});
const receivedPrimaryColumns = Object.keys(primaryKey);
if (!compareArrayElements(availablePrimaryColumns, receivedPrimaryColumns)) {
throw new HttpException(
{
message: Messages.PRIMARY_KEY_INVALID,
},
HttpStatus.BAD_REQUEST,
);
}
const tableSettings = await this._dbContext.tableSettingsRepository.findTableSettings(connectionId, tableName);
if (tableSettings && !tableSettings?.can_delete) {
throw new HttpException(
{
message: Messages.CANT_DO_TABLE_OPERATION,
},
HttpStatus.FORBIDDEN,
);
}
const personalTableSettings = await this._dbContext.personalTableSettingsRepository.findUserTableSettings(
userId,
connectionId,
tableName,
);
const builtTableSettings = buildDAOsTableSettingsDs(tableSettings, personalTableSettings);
let oldRowData: Record<string, unknown>;
try {
oldRowData = await dao.getRowByPrimaryKey(tableName, primaryKey, builtTableSettings, userEmail);
} catch (e) {
throw new UnknownSQLException(e.message, ExceptionOperations.FAILED_TO_DELETE_ROW_FROM_TABLE);
}
if (!oldRowData) {
throw new HttpException(
{
message: Messages.ROW_PRIMARY_KEY_NOT_FOUND,
},
HttpStatus.BAD_REQUEST,
);
}
try {
await dao.deleteRowInTable(tableName, primaryKey, userEmail);
operationResult = OperationResultStatusEnum.successfully;
return {
row: oldRowData,
};
} catch (e) {
operationResult = OperationResultStatusEnum.unsuccessfully;
throw new DeleteRowException(e.message);
} finally {
const logRecord = {
table_name: tableName,
userId: userId,
connection: connection,
operationType: LogOperationTypeEnum.deleteRow,
operationStatusResult: operationResult,
row: primaryKey,
old_data: oldRowData,
table_primary_key: primaryKey,
};
await this.tableLogsService.crateAndSaveNewLogUtil(logRecord);
const isTest = isTestConnectionUtil(connection);
await this.amplitudeService.formAndSendLogRecord(
isTest ? AmplitudeEventTypeEnum.tableRowDeletedTest : AmplitudeEventTypeEnum.tableRowDeleted,
userId,
);
const foundAddTableActions = await this._dbContext.tableActionRepository.findTableActionsWithDeleteRowEvents(
connectionId,
tableName,
);
await this.tableActionActivationService.activateTableActions(
foundAddTableActions,
connection,
primaryKey,
userId,
tableName,
TableActionEventEnum.DELETE_ROW,
);
}
}
}