Skip to content

Commit 131f5b2

Browse files
authored
Merge pull request #1494 from rocket-admin/backend_personal_table_settings_fix
refactor: improve personal table settings use case and update relatedtests
2 parents 4b88263 + 3524604 commit 131f5b2

File tree

2 files changed

+295
-255
lines changed

2 files changed

+295
-255
lines changed

backend/src/entities/table-settings/personal-table-settings/use-cases/create-update-personal-table-settings.use.case.ts

Lines changed: 98 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,101 +5,122 @@ import { IGlobalDatabaseContext } from '../../../../common/application/global-da
55
import { BaseType } from '../../../../common/data-injection.tokens.js';
66
import { ConnectionEntity } from '../../../connection/connection.entity.js';
77
import {
8-
CreatePersonalTableSettingsDs,
9-
PersonalTableSettingsData,
8+
CreatePersonalTableSettingsDs,
9+
PersonalTableSettingsData,
1010
} from '../data-structures/create-personal-table-settings.ds.js';
1111
import { FoundPersonalTableSettingsDto } from '../dto/found-personal-table-settings.dto.js';
1212
import { buildNewPersonalTableSettingsEntity } from '../utils/build-new-personal-table-settings-entity.util.js';
1313
import { ICreateUpdatePersonalTableSettings } from './personal-table-settings.use-cases.interface.js';
1414

1515
@Injectable({ scope: Scope.REQUEST })
1616
export class CreateUpdatePersonalTableSettingsUseCase
17-
extends AbstractUseCase<CreatePersonalTableSettingsDs, FoundPersonalTableSettingsDto>
18-
implements ICreateUpdatePersonalTableSettings
17+
extends AbstractUseCase<CreatePersonalTableSettingsDs, FoundPersonalTableSettingsDto>
18+
implements ICreateUpdatePersonalTableSettings
1919
{
20-
constructor(
21-
@Inject(BaseType.GLOBAL_DB_CONTEXT)
22-
protected _dbContext: IGlobalDatabaseContext,
23-
) {
24-
super();
25-
}
20+
constructor(
21+
@Inject(BaseType.GLOBAL_DB_CONTEXT)
22+
protected _dbContext: IGlobalDatabaseContext,
23+
) {
24+
super();
25+
}
2626

27-
public async implementation(
28-
personalTableSettingsData: CreatePersonalTableSettingsDs,
29-
): Promise<FoundPersonalTableSettingsDto> {
30-
const {
31-
table_settings_metadata: { connection_id, master_password, table_name, user_id },
32-
table_settings_data,
33-
} = personalTableSettingsData;
27+
public async implementation(
28+
personalTableSettingsData: CreatePersonalTableSettingsDs,
29+
): Promise<FoundPersonalTableSettingsDto> {
30+
const {
31+
table_settings_metadata: { connection_id, master_password, table_name, user_id },
32+
table_settings_data,
33+
} = personalTableSettingsData;
3434

35-
const foundConnection = await this._dbContext.connectionRepository.findAndDecryptConnection(
36-
connection_id,
37-
master_password,
38-
);
35+
const foundConnection = await this._dbContext.connectionRepository.findAndDecryptConnection(
36+
connection_id,
37+
master_password,
38+
);
3939

40-
await this.validatePersonalTableSettingsData(table_settings_data, foundConnection, table_name);
40+
await this.validatePersonalTableSettingsData(table_settings_data, foundConnection, table_name);
4141

42-
const foundTableSettings = await this._dbContext.personalTableSettingsRepository.findUserTableSettings(
43-
user_id,
44-
connection_id,
45-
table_name,
46-
);
42+
const foundTableSettings = await this._dbContext.personalTableSettingsRepository.findUserTableSettings(
43+
user_id,
44+
connection_id,
45+
table_name,
46+
);
4747

48-
const settings = foundTableSettings || {};
49-
const newSettingsEntity = buildNewPersonalTableSettingsEntity(table_settings_data);
50-
newSettingsEntity.connection_id = foundConnection.id;
51-
newSettingsEntity.table_name = table_name;
52-
newSettingsEntity.user_id = user_id;
53-
Object.assign(settings, newSettingsEntity);
54-
return await this._dbContext.personalTableSettingsRepository.save(settings);
55-
}
48+
const newSettingsEntity = buildNewPersonalTableSettingsEntity(table_settings_data);
5649

57-
private async validatePersonalTableSettingsData(
58-
settingsData: PersonalTableSettingsData,
59-
connection: ConnectionEntity,
60-
tableName: string,
61-
): Promise<void> {
62-
const { columns_view, list_fields, list_per_page, ordering, ordering_field } = settingsData;
63-
const dao = getDataAccessObject(connection);
64-
const tableStructure = await dao.getTableStructure(tableName, null);
65-
const tableColumnNames = tableStructure.map((col) => col.column_name);
66-
const errors = [];
50+
if (foundTableSettings) {
51+
if (newSettingsEntity.columns_view !== undefined) {
52+
foundTableSettings.columns_view = newSettingsEntity.columns_view;
53+
}
54+
if (newSettingsEntity.list_fields !== undefined) {
55+
foundTableSettings.list_fields = newSettingsEntity.list_fields;
56+
}
57+
if (newSettingsEntity.list_per_page !== undefined) {
58+
foundTableSettings.list_per_page = newSettingsEntity.list_per_page;
59+
}
60+
if (newSettingsEntity.ordering !== undefined) {
61+
foundTableSettings.ordering = newSettingsEntity.ordering;
62+
}
63+
if (newSettingsEntity.ordering_field !== undefined) {
64+
foundTableSettings.ordering_field = newSettingsEntity.ordering_field;
65+
}
66+
if (newSettingsEntity.original_names !== undefined) {
67+
foundTableSettings.original_names = newSettingsEntity.original_names;
68+
}
69+
return await this._dbContext.personalTableSettingsRepository.save(foundTableSettings);
70+
} else {
71+
newSettingsEntity.connection_id = foundConnection.id;
72+
newSettingsEntity.table_name = table_name;
73+
newSettingsEntity.user_id = user_id;
74+
return await this._dbContext.personalTableSettingsRepository.save(newSettingsEntity);
75+
}
76+
}
6777

68-
if (columns_view !== null && columns_view !== undefined) {
69-
const invalidColumns = columns_view.filter((col) => !tableColumnNames.includes(col));
70-
if (invalidColumns.length > 0) {
71-
errors.push(`Invalid columns in columns_view: ${invalidColumns.join(', ')}`);
72-
}
73-
}
78+
private async validatePersonalTableSettingsData(
79+
settingsData: PersonalTableSettingsData,
80+
connection: ConnectionEntity,
81+
tableName: string,
82+
): Promise<void> {
83+
const { columns_view, list_fields, list_per_page, ordering, ordering_field } = settingsData;
84+
const dao = getDataAccessObject(connection);
85+
const tableStructure = await dao.getTableStructure(tableName, null);
86+
const tableColumnNames = tableStructure.map((col) => col.column_name);
87+
const errors = [];
7488

75-
if (list_fields !== null && list_fields !== undefined) {
76-
const invalidFields = list_fields.filter((field) => !tableColumnNames.includes(field));
77-
if (invalidFields.length > 0) {
78-
errors.push(`Invalid columns in list_fields: ${invalidFields.join(', ')}`);
79-
}
80-
}
89+
if (columns_view !== null && columns_view !== undefined) {
90+
const invalidColumns = columns_view.filter((col) => !tableColumnNames.includes(col));
91+
if (invalidColumns.length > 0) {
92+
errors.push(`Invalid columns in columns_view: ${invalidColumns.join(', ')}`);
93+
}
94+
}
8195

82-
if (list_per_page !== null && list_per_page !== undefined) {
83-
if (typeof list_per_page !== 'number' || list_per_page < 1 || list_per_page > 1000) {
84-
errors.push('list_per_page must be a number between 1 and 1000');
85-
}
86-
}
96+
if (list_fields !== null && list_fields !== undefined) {
97+
const invalidFields = list_fields.filter((field) => !tableColumnNames.includes(field));
98+
if (invalidFields.length > 0) {
99+
errors.push(`Invalid columns in list_fields: ${invalidFields.join(', ')}`);
100+
}
101+
}
87102

88-
if (ordering !== null && ordering !== undefined) {
89-
const validOrderings = ['ASC', 'DESC'];
90-
if (!validOrderings.includes(ordering)) {
91-
errors.push(`ordering must be one of: ${validOrderings.join(', ')}`);
92-
}
93-
}
103+
if (list_per_page !== null && list_per_page !== undefined) {
104+
if (typeof list_per_page !== 'number' || list_per_page < 1 || list_per_page > 1000) {
105+
errors.push('list_per_page must be a number between 1 and 1000');
106+
}
107+
}
94108

95-
if (ordering_field !== null && ordering_field !== undefined) {
96-
if (!tableColumnNames.includes(ordering_field)) {
97-
errors.push(`Invalid ordering_field: ${ordering_field}`);
98-
}
99-
}
109+
if (ordering !== null && ordering !== undefined) {
110+
const validOrderings = ['ASC', 'DESC'];
111+
if (!validOrderings.includes(ordering)) {
112+
errors.push(`ordering must be one of: ${validOrderings.join(', ')}`);
113+
}
114+
}
100115

101-
if (errors.length > 0) {
102-
throw new BadRequestException(`Validation failed: ${errors.join('; ')}`);
103-
}
104-
}
116+
if (ordering_field !== null && ordering_field !== undefined) {
117+
if (!tableColumnNames.includes(ordering_field)) {
118+
errors.push(`Invalid ordering_field: ${ordering_field}`);
119+
}
120+
}
121+
122+
if (errors.length > 0) {
123+
throw new BadRequestException(`Validation failed: ${errors.join('; ')}`);
124+
}
125+
}
105126
}

0 commit comments

Comments
 (0)