diff --git a/packages/twenty-front/src/modules/object-record/record-index/hooks/useHandleRecordGroupField.ts b/packages/twenty-front/src/modules/object-record/record-index/hooks/useHandleRecordGroupField.ts index 5e4300f0df..28c2455c3d 100644 --- a/packages/twenty-front/src/modules/object-record/record-index/hooks/useHandleRecordGroupField.ts +++ b/packages/twenty-front/src/modules/object-record/record-index/hooks/useHandleRecordGroupField.ts @@ -73,11 +73,24 @@ export const useHandleRecordGroupField = ({ }) satisfies ViewGroup, ); + const viewGroupsToDelete = view.viewGroups.filter( + (group) => group.fieldMetadataId !== fieldMetadataItem.id, + ); + if (viewGroupsToCreate.length > 0) { await createViewGroupRecords(viewGroupsToCreate, view); } + + if (viewGroupsToDelete.length > 0) { + await deleteViewGroupRecords(viewGroupsToDelete); + } }, - [createViewGroupRecords, currentViewIdCallbackState, getViewFromCache], + [ + createViewGroupRecords, + deleteViewGroupRecords, + currentViewIdCallbackState, + getViewFromCache, + ], ); const resetRecordGroupField = useRecoilCallback( diff --git a/packages/twenty-front/src/modules/views/types/GraphQLView.ts b/packages/twenty-front/src/modules/views/types/GraphQLView.ts index 7ea55b6690..9fb4cb1aaf 100644 --- a/packages/twenty-front/src/modules/views/types/GraphQLView.ts +++ b/packages/twenty-front/src/modules/views/types/GraphQLView.ts @@ -12,6 +12,9 @@ export type GraphQLView = { name: string; type: ViewType; key: ViewKey | null; + /** + * @deprecated Use `viewGroups.fieldMetadataId` instead. + */ kanbanFieldMetadataId: string; kanbanAggregateOperation?: AGGREGATE_OPERATIONS | null; kanbanAggregateOperationFieldMetadataId?: string | null; diff --git a/packages/twenty-front/src/modules/views/types/View.ts b/packages/twenty-front/src/modules/views/types/View.ts index 3a976710c0..e56e43b137 100644 --- a/packages/twenty-front/src/modules/views/types/View.ts +++ b/packages/twenty-front/src/modules/views/types/View.ts @@ -19,6 +19,9 @@ export type View = { viewFilters: ViewFilter[]; viewFilterGroups?: ViewFilterGroup[]; viewSorts: ViewSort[]; + /** + * @deprecated Use `viewGroups.fieldMetadataId` instead. + */ kanbanFieldMetadataId: string; kanbanAggregateOperation: AGGREGATE_OPERATIONS | null; kanbanAggregateOperationFieldMetadataId: string | null; diff --git a/packages/twenty-server/src/engine/metadata-modules/field-metadata/services/field-metadata-related-records.service.ts b/packages/twenty-server/src/engine/metadata-modules/field-metadata/services/field-metadata-related-records.service.ts index be86303524..61757b18eb 100644 --- a/packages/twenty-server/src/engine/metadata-modules/field-metadata/services/field-metadata-related-records.service.ts +++ b/packages/twenty-server/src/engine/metadata-modules/field-metadata/services/field-metadata-related-records.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; -import { In } from 'typeorm'; +import { EntityManager, In } from 'typeorm'; import { FieldMetadataComplexOption, @@ -27,6 +27,7 @@ export class FieldMetadataRelatedRecordsService { public async updateRelatedViewGroups( oldFieldMetadata: FieldMetadataEntity, newFieldMetadata: FieldMetadataEntity, + transactionManager?: EntityManager, ) { if ( !isSelectFieldMetadataType(newFieldMetadata.type) || @@ -49,6 +50,10 @@ export class FieldMetadataRelatedRecordsService { ); for (const view of views) { + if (view.viewGroups.length === 0) { + continue; + } + const maxPosition = view.viewGroups.reduce( (max, viewGroup) => Math.max(max, viewGroup.position), 0, @@ -64,7 +69,7 @@ export class FieldMetadataRelatedRecordsService { }), ); - await viewGroupRepository.insert(viewGroupsToCreate); + await viewGroupRepository.insert(viewGroupsToCreate, transactionManager); for (const { old: oldOption, new: newOption } of updated) { const viewGroup = view.viewGroups.find( @@ -82,15 +87,19 @@ export class FieldMetadataRelatedRecordsService { { fieldValue: newOption.value, }, + transactionManager, ); } const valuesToDelete = deleted.map((option) => option.value); - await viewGroupRepository.delete({ - fieldMetadataId: newFieldMetadata.id, - fieldValue: In(valuesToDelete), - }); + await viewGroupRepository.delete( + { + fieldMetadataId: newFieldMetadata.id, + fieldValue: In(valuesToDelete), + }, + transactionManager, + ); } } @@ -143,7 +152,9 @@ export class FieldMetadataRelatedRecordsService { return await viewRepository.find({ where: { - kanbanFieldMetadataId: fieldMetadata.id, + viewGroups: { + fieldMetadataId: fieldMetadata.id, + }, }, relations: ['viewGroups'], }); diff --git a/packages/twenty-server/src/modules/view/standard-objects/view.workspace-entity.ts b/packages/twenty-server/src/modules/view/standard-objects/view.workspace-entity.ts index 499b597219..63cc3ec36a 100644 --- a/packages/twenty-server/src/modules/view/standard-objects/view.workspace-entity.ts +++ b/packages/twenty-server/src/modules/view/standard-objects/view.workspace-entity.ts @@ -87,6 +87,9 @@ export class ViewWorkspaceEntity extends BaseWorkspaceEntity { label: 'kanbanfieldMetadataId', description: 'View Kanban column field', }) + /** + * @deprecated Use `viewGroups.fieldMetadataId` instead + */ kanbanFieldMetadataId: string; @WorkspaceField({