-
-
Notifications
You must be signed in to change notification settings - Fork 18
fix: update connection properties handling for table categories #1470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import { | |
| } from '../utils/build-update-connection-properties-object.js'; | ||
| import { validateCreateConnectionPropertiesDs } from '../utils/validate-create-connection-properties-ds.js'; | ||
| import { IUpdateConnectionProperties } from './connection-properties-use.cases.interface.js'; | ||
| import { TableCategoriesEntity } from '../../table-categories/table-categories.entity.js'; | ||
|
|
||
| @Injectable() | ||
| export class UpdateConnectionPropertiesUseCase | ||
|
|
@@ -46,30 +47,62 @@ export class UpdateConnectionPropertiesUseCase | |
| const updatePropertiesObject: IUpdateConnectionPropertiesObject = buildUpdateConnectionPropertiesObject(inputData); | ||
| const updated = Object.assign(connectionPropertiesToUpdate, updatePropertiesObject); | ||
|
|
||
| const categoriesToRemove = await this._dbContext.tableCategoriesRepository.find({ | ||
| const foundCategories = await this._dbContext.tableCategoriesRepository.find({ | ||
| where: { connection_properties_id: connectionPropertiesToUpdate.id }, | ||
| }); | ||
|
|
||
| if (categoriesToRemove && categoriesToRemove.length > 0) { | ||
| await this._dbContext.tableCategoriesRepository.remove(categoriesToRemove); | ||
| updated.table_categories = []; | ||
| } | ||
| const newCategories: Array<TableCategoriesEntity> = []; | ||
|
|
||
| const updatedProperties = await this._dbContext.connectionPropertiesRepository.saveNewConnectionProperties(updated); | ||
| if (table_categories && table_categories.length) { | ||
| const createdCategories = table_categories.map((category) => { | ||
| const newCategory = this._dbContext.tableCategoriesRepository.create({ | ||
| category_name: category.category_name, | ||
| tables: category.tables, | ||
| category_color: category.category_color, | ||
| category_id: category.category_id, | ||
| if (table_categories && table_categories.length > 0) { | ||
| const categoriesToRemove = foundCategories.filter((foundCategory) => { | ||
| return !table_categories?.some((inputCategory) => inputCategory.category_id === foundCategory.category_id); | ||
| }); | ||
| if (categoriesToRemove && categoriesToRemove.length > 0) { | ||
| await this._dbContext.tableCategoriesRepository.remove(categoriesToRemove); | ||
| } | ||
|
|
||
| const categoriesToCreate = table_categories.filter((inputCategory) => { | ||
| return !foundCategories.some((foundCategory) => foundCategory.category_id === inputCategory.category_id); | ||
| }); | ||
|
|
||
| if (categoriesToCreate && categoriesToCreate.length > 0) { | ||
| const createdCategories = categoriesToCreate.map((category) => { | ||
| const newCategory = this._dbContext.tableCategoriesRepository.create({ | ||
| category_name: category.category_name, | ||
| tables: category.tables, | ||
| category_color: category.category_color, | ||
| category_id: category.category_id, | ||
| }); | ||
| newCategory.connection_properties = connectionPropertiesToUpdate; | ||
| return newCategory; | ||
| }); | ||
| newCategory.connection_properties = updatedProperties; | ||
| return newCategory; | ||
| const savedNewCategories = await this._dbContext.tableCategoriesRepository.save(createdCategories); | ||
| newCategories.push(...savedNewCategories); | ||
| } | ||
|
|
||
| const categoriesToUpdate = table_categories.filter((inputCategory) => { | ||
| return foundCategories.some((foundCategory) => foundCategory.category_id === inputCategory.category_id); | ||
| }); | ||
| const savedCategories = await this._dbContext.tableCategoriesRepository.save(createdCategories); | ||
| updatedProperties.table_categories = savedCategories; | ||
|
|
||
| for (const category of categoriesToUpdate) { | ||
| const categoryToUpdate = foundCategories.find( | ||
| (foundCategory) => foundCategory.category_id === category.category_id, | ||
| ); | ||
| if (categoryToUpdate) { | ||
| categoryToUpdate.category_name = category.category_name; | ||
| categoryToUpdate.category_color = category.category_color; | ||
| categoryToUpdate.tables = category.tables; | ||
| const savedUpdatedCategory = await this._dbContext.tableCategoriesRepository.save(categoryToUpdate); | ||
| newCategories.push(savedUpdatedCategory); | ||
| } | ||
| } | ||
|
Comment on lines
+87
to
+98
|
||
| } else { | ||
| newCategories.push(...foundCategories); | ||
| } | ||
|
Comment on lines
+56
to
101
|
||
|
|
||
| const updatedProperties = await this._dbContext.connectionPropertiesRepository.saveNewConnectionProperties(updated); | ||
| updatedProperties.table_categories = newCategories; | ||
|
|
||
| return buildFoundConnectionPropertiesDs(updatedProperties); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ import { ValidationException } from '../../../src/exceptions/custom-exceptions/v | |||||||||||||||||||
| import { ValidationError } from 'class-validator'; | ||||||||||||||||||||
| import { Cacher } from '../../../src/helpers/cache/cacher.js'; | ||||||||||||||||||||
| import { WinstonLogger } from '../../../src/entities/logging/winston-logger.js'; | ||||||||||||||||||||
| import { CreateTableCategoryDto } from '../../../src/entities/table-categories/dto/create-table-category.dto.js'; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const mockFactory = new MockFactory(); | ||||||||||||||||||||
| let app: INestApplication; | ||||||||||||||||||||
|
|
@@ -273,7 +274,7 @@ test.serial(`${currentTest} should return created connection properties with tab | |||||||||||||||||||
| updateConnectionPropertiesROWithoutCategories.default_showing_table, | ||||||||||||||||||||
| updatedConnectionPropertiesWithOutCategories.default_showing_table, | ||||||||||||||||||||
| ); | ||||||||||||||||||||
| t.is(updateConnectionPropertiesROWithoutCategories.table_categories.length, 0); | ||||||||||||||||||||
| t.is(updateConnectionPropertiesROWithoutCategories.table_categories.length, 1); | ||||||||||||||||||||
|
||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||
| throw e; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -623,9 +624,93 @@ test.serial(`${currentTest} should return updated connection properties`, async | |||||||||||||||||||
| } | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // test.serial(`${currentTest} `, async (t) => { | ||||||||||||||||||||
| // try { | ||||||||||||||||||||
| // } catch (e) { | ||||||||||||||||||||
| // throw e; | ||||||||||||||||||||
| // } | ||||||||||||||||||||
| // }); | ||||||||||||||||||||
| test.serial( | ||||||||||||||||||||
| `${currentTest} should keep created table categories if the exists return updated connection properties`, | ||||||||||||||||||||
|
||||||||||||||||||||
| `${currentTest} should keep created table categories if the exists return updated connection properties`, | |
| `${currentTest} should keep created table categories if they exist return updated connection properties`, |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable newConnection2.
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable newConnectionToTestDB.
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable updateConnection.
| const { newConnection2, newConnectionToTestDB, updateConnection, newGroup1, newConnection } = getTestData(); | |
| const { newConnection2, newConnectionToTestDB, newGroup1, newConnection } = getTestData(); |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable newGroup1.
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable 'createTableCategoriesRO' is assigned but never used in the test. Consider either removing this variable assignment or adding assertions to verify the response content from the table categories creation endpoint.
| t.is(createTableCategoriesResponse.status, 200); | |
| t.is(createTableCategoriesResponse.status, 200); | |
| // Assert the response content for table categories creation | |
| t.true(Array.isArray(createTableCategoriesRO)); | |
| t.is(createTableCategoriesRO.length, 1); | |
| t.is(createTableCategoriesRO[0].category_name, 'Category 1'); | |
| t.is(createTableCategoriesRO[0].category_color, '#FF5733'); | |
| t.deepEqual(createTableCategoriesRO[0].tables, [testTableName]); | |
| t.is(createTableCategoriesRO[0].category_id, 'cat-001'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The optional chaining operator on 'table_categories' is redundant here since this code is inside a block that already checks 'table_categories && table_categories.length > 0' at line 56. The variable is guaranteed to be defined at this point, so 'table_categories?.some' can be simplified to 'table_categories.some'.