Skip to content

Commit 6855e01

Browse files
committed
fix: update connection properties handling for table categories
1 parent 7722462 commit 6855e01

2 files changed

Lines changed: 142 additions & 24 deletions

File tree

backend/src/entities/connection-properties/use-cases/update-connection-properties.use.case.ts

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '../utils/build-update-connection-properties-object.js';
1414
import { validateCreateConnectionPropertiesDs } from '../utils/validate-create-connection-properties-ds.js';
1515
import { IUpdateConnectionProperties } from './connection-properties-use.cases.interface.js';
16+
import { TableCategoriesEntity } from '../../table-categories/table-categories.entity.js';
1617

1718
@Injectable()
1819
export class UpdateConnectionPropertiesUseCase
@@ -46,30 +47,62 @@ export class UpdateConnectionPropertiesUseCase
4647
const updatePropertiesObject: IUpdateConnectionPropertiesObject = buildUpdateConnectionPropertiesObject(inputData);
4748
const updated = Object.assign(connectionPropertiesToUpdate, updatePropertiesObject);
4849

49-
const categoriesToRemove = await this._dbContext.tableCategoriesRepository.find({
50+
const foundCategories = await this._dbContext.tableCategoriesRepository.find({
5051
where: { connection_properties_id: connectionPropertiesToUpdate.id },
5152
});
5253

53-
if (categoriesToRemove && categoriesToRemove.length > 0) {
54-
await this._dbContext.tableCategoriesRepository.remove(categoriesToRemove);
55-
updated.table_categories = [];
56-
}
54+
const newCategories: Array<TableCategoriesEntity> = [];
5755

58-
const updatedProperties = await this._dbContext.connectionPropertiesRepository.saveNewConnectionProperties(updated);
59-
if (table_categories && table_categories.length) {
60-
const createdCategories = table_categories.map((category) => {
61-
const newCategory = this._dbContext.tableCategoriesRepository.create({
62-
category_name: category.category_name,
63-
tables: category.tables,
64-
category_color: category.category_color,
65-
category_id: category.category_id,
56+
if (table_categories && table_categories.length > 0) {
57+
const categoriesToRemove = foundCategories.filter((foundCategory) => {
58+
return !table_categories?.some((inputCategory) => inputCategory.category_id === foundCategory.category_id);
59+
});
60+
if (categoriesToRemove && categoriesToRemove.length > 0) {
61+
await this._dbContext.tableCategoriesRepository.remove(categoriesToRemove);
62+
}
63+
64+
const categoriesToCreate = table_categories.filter((inputCategory) => {
65+
return !foundCategories.some((foundCategory) => foundCategory.category_id === inputCategory.category_id);
66+
});
67+
68+
if (categoriesToCreate && categoriesToCreate.length > 0) {
69+
const createdCategories = categoriesToCreate.map((category) => {
70+
const newCategory = this._dbContext.tableCategoriesRepository.create({
71+
category_name: category.category_name,
72+
tables: category.tables,
73+
category_color: category.category_color,
74+
category_id: category.category_id,
75+
});
76+
newCategory.connection_properties = connectionPropertiesToUpdate;
77+
return newCategory;
6678
});
67-
newCategory.connection_properties = updatedProperties;
68-
return newCategory;
79+
const savedNewCategories = await this._dbContext.tableCategoriesRepository.save(createdCategories);
80+
newCategories.push(...savedNewCategories);
81+
}
82+
83+
const categoriesToUpdate = table_categories.filter((inputCategory) => {
84+
return foundCategories.some((foundCategory) => foundCategory.category_id === inputCategory.category_id);
6985
});
70-
const savedCategories = await this._dbContext.tableCategoriesRepository.save(createdCategories);
71-
updatedProperties.table_categories = savedCategories;
86+
87+
for (const category of categoriesToUpdate) {
88+
const categoryToUpdate = foundCategories.find(
89+
(foundCategory) => foundCategory.category_id === category.category_id,
90+
);
91+
if (categoryToUpdate) {
92+
categoryToUpdate.category_name = category.category_name;
93+
categoryToUpdate.category_color = category.category_color;
94+
categoryToUpdate.tables = category.tables;
95+
const savedUpdatedCategory = await this._dbContext.tableCategoriesRepository.save(categoryToUpdate);
96+
newCategories.push(savedUpdatedCategory);
97+
}
98+
}
99+
} else {
100+
newCategories.push(...foundCategories);
72101
}
102+
103+
const updatedProperties = await this._dbContext.connectionPropertiesRepository.saveNewConnectionProperties(updated);
104+
updatedProperties.table_categories = newCategories;
105+
73106
return buildFoundConnectionPropertiesDs(updatedProperties);
74107
}
75108
}

backend/test/ava-tests/saas-tests/connection-properties-e2e.test.ts

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ValidationException } from '../../../src/exceptions/custom-exceptions/v
1616
import { ValidationError } from 'class-validator';
1717
import { Cacher } from '../../../src/helpers/cache/cacher.js';
1818
import { WinstonLogger } from '../../../src/entities/logging/winston-logger.js';
19+
import { CreateTableCategoryDto } from '../../../src/entities/table-categories/dto/create-table-category.dto.js';
1920

2021
const mockFactory = new MockFactory();
2122
let app: INestApplication;
@@ -273,7 +274,7 @@ test.serial(`${currentTest} should return created connection properties with tab
273274
updateConnectionPropertiesROWithoutCategories.default_showing_table,
274275
updatedConnectionPropertiesWithOutCategories.default_showing_table,
275276
);
276-
t.is(updateConnectionPropertiesROWithoutCategories.table_categories.length, 0);
277+
t.is(updateConnectionPropertiesROWithoutCategories.table_categories.length, 1);
277278
} catch (e) {
278279
throw e;
279280
}
@@ -623,9 +624,93 @@ test.serial(`${currentTest} should return updated connection properties`, async
623624
}
624625
});
625626

626-
// test.serial(`${currentTest} `, async (t) => {
627-
// try {
628-
// } catch (e) {
629-
// throw e;
630-
// }
631-
// });
627+
test.serial(
628+
`${currentTest} should keep created table categories if the exists return updated connection properties`,
629+
async (t) => {
630+
try {
631+
const { newConnection2, newConnectionToTestDB, updateConnection, newGroup1, newConnection } = getTestData();
632+
const { token } = await registerUserAndReturnUserInfo(app);
633+
const secondHiddenTableName = `${faker.lorem.words(1)}_${faker.string.uuid()}`;
634+
await resetPostgresTestDB(secondHiddenTableName);
635+
636+
const createConnectionResponse = await request(app.getHttpServer())
637+
.post('/connection')
638+
.send(newConnection)
639+
.set('Cookie', token)
640+
.set('Content-Type', 'application/json')
641+
.set('Accept', 'application/json');
642+
const createConnectionRO = JSON.parse(createConnectionResponse.text);
643+
t.is(createConnectionResponse.status, 201);
644+
645+
const createConnectionPropertiesResponse = await request(app.getHttpServer())
646+
.post(`/connection/properties/${createConnectionRO.id}`)
647+
.send(newConnectionProperties)
648+
.set('Cookie', token)
649+
.set('Content-Type', 'application/json')
650+
.set('Accept', 'application/json');
651+
const createConnectionPropertiesRO = JSON.parse(createConnectionPropertiesResponse.text);
652+
t.is(createConnectionPropertiesResponse.status, 201);
653+
t.is(createConnectionPropertiesRO.hidden_tables[0], newConnectionProperties.hidden_tables[0]);
654+
t.is(createConnectionPropertiesRO.connectionId, createConnectionRO.id);
655+
656+
const categoriesDTO: Array<CreateTableCategoryDto> = [
657+
{
658+
category_name: 'Category 1',
659+
category_color: '#FF5733',
660+
tables: [testTableName],
661+
category_id: 'cat-001',
662+
},
663+
];
664+
665+
const createTableCategoriesResponse = await request(app.getHttpServer())
666+
.put(`/table-categories/${createConnectionRO.id}`)
667+
.send(categoriesDTO)
668+
.set('Cookie', token)
669+
.set('Content-Type', 'application/json')
670+
.set('Accept', 'application/json');
671+
672+
const createTableCategoriesRO = JSON.parse(createTableCategoriesResponse.text);
673+
674+
t.is(createTableCategoriesResponse.status, 200);
675+
676+
const propertiesCopy = JSON.parse(JSON.stringify(newConnectionProperties));
677+
propertiesCopy.hidden_tables = [testTableName, secondHiddenTableName];
678+
679+
const updateConnectionPropertiesResponse = await request(app.getHttpServer())
680+
.put(`/connection/properties/${createConnectionRO.id}`)
681+
.send(propertiesCopy)
682+
.set('Cookie', token)
683+
.set('Content-Type', 'application/json')
684+
.set('Accept', 'application/json');
685+
686+
t.is(updateConnectionPropertiesResponse.status, 200);
687+
688+
const updateConnectionPropertiesRO = JSON.parse(updateConnectionPropertiesResponse.text);
689+
t.is(updateConnectionPropertiesRO.hidden_tables.length, 2);
690+
t.is(updateConnectionPropertiesRO.hidden_tables.includes(testTableName), true);
691+
t.is(updateConnectionPropertiesRO.hidden_tables.includes(secondHiddenTableName), true);
692+
t.is(updateConnectionPropertiesRO.connectionId, createConnectionRO.id);
693+
t.is(updateConnectionPropertiesRO.table_categories.length, 1);
694+
t.is(updateConnectionPropertiesRO.table_categories[0].category_name, 'Category 1');
695+
t.is(updateConnectionPropertiesRO.table_categories[0].tables.length, 1);
696+
t.is(updateConnectionPropertiesRO.table_categories[0].tables[0], testTableName);
697+
698+
const findTableCategoriesResponse = await request(app.getHttpServer())
699+
.get(`/table-categories/${createConnectionRO.id}`)
700+
.set('Cookie', token)
701+
.set('Content-Type', 'application/json')
702+
.set('Accept', 'application/json');
703+
704+
const findTableCategoriesRO = JSON.parse(findTableCategoriesResponse.text);
705+
706+
t.is(findTableCategoriesResponse.status, 200);
707+
708+
t.is(findTableCategoriesRO.length, 1);
709+
t.is(findTableCategoriesRO[0].category_name, 'Category 1');
710+
t.is(findTableCategoriesRO[0].tables.length, 1);
711+
t.is(findTableCategoriesRO[0].tables[0], testTableName);
712+
} catch (e) {
713+
throw e;
714+
}
715+
},
716+
);

0 commit comments

Comments
 (0)