From 8259ded1d968ea60baf123685bebacef9a27be9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Magrin?= Date: Tue, 3 Dec 2024 12:07:13 +0100 Subject: [PATCH 1/4] fix: only update view groups when view already have view groups --- .../services/field-metadata-related-records.service.ts | 4 ++++ 1 file changed, 4 insertions(+) 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 be8630352497..9d53497e93b5 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 @@ -49,6 +49,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, From 7ff1876ed798918ea961e22ce6d321fe57e4e9e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Magrin?= Date: Tue, 3 Dec 2024 12:23:44 +0100 Subject: [PATCH 2/4] fix: changing fieldmetadata view group brake view groups --- .../hooks/useHandleRecordGroupField.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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 5e4300f0df15..28c2455c3d7b 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( From 73100fb4626e4b2bf8cca70c11e3f030d2bed6f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Magrin?= Date: Tue, 3 Dec 2024 17:01:49 +0100 Subject: [PATCH 3/4] fix: rename select option brake view group --- .../field-metadata-related-records.service.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) 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 9d53497e93b5..61757b18ebfe 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) || @@ -68,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( @@ -86,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, + ); } } @@ -147,7 +152,9 @@ export class FieldMetadataRelatedRecordsService { return await viewRepository.find({ where: { - kanbanFieldMetadataId: fieldMetadata.id, + viewGroups: { + fieldMetadataId: fieldMetadata.id, + }, }, relations: ['viewGroups'], }); From 7a8fa122496fc77bcbf00ea67261fffd551222d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Magrin?= Date: Tue, 3 Dec 2024 17:02:00 +0100 Subject: [PATCH 4/4] fix: mark kanbanFieldMetadataId as deprecated --- packages/twenty-front/src/modules/views/types/GraphQLView.ts | 3 +++ packages/twenty-front/src/modules/views/types/View.ts | 3 +++ .../src/modules/view/standard-objects/view.workspace-entity.ts | 3 +++ 3 files changed, 9 insertions(+) diff --git a/packages/twenty-front/src/modules/views/types/GraphQLView.ts b/packages/twenty-front/src/modules/views/types/GraphQLView.ts index cb8729f91564..abb71e868ad8 100644 --- a/packages/twenty-front/src/modules/views/types/GraphQLView.ts +++ b/packages/twenty-front/src/modules/views/types/GraphQLView.ts @@ -11,6 +11,9 @@ export type GraphQLView = { name: string; type: ViewType; key: ViewKey | null; + /** + * @deprecated Use `viewGroups.fieldMetadataId` instead. + */ kanbanFieldMetadataId: string; objectMetadataId: string; isCompact: boolean; diff --git a/packages/twenty-front/src/modules/views/types/View.ts b/packages/twenty-front/src/modules/views/types/View.ts index 111c5a5002ed..df405eaa7094 100644 --- a/packages/twenty-front/src/modules/views/types/View.ts +++ b/packages/twenty-front/src/modules/views/types/View.ts @@ -18,6 +18,9 @@ export type View = { viewFilters: ViewFilter[]; viewFilterGroups?: ViewFilterGroup[]; viewSorts: ViewSort[]; + /** + * @deprecated Use `viewGroups.fieldMetadataId` instead. + */ kanbanFieldMetadataId: string; position: number; icon: string; 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 67af0c9fa215..96eb9d1e34b2 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 @@ -84,6 +84,9 @@ export class ViewWorkspaceEntity extends BaseWorkspaceEntity { label: 'kanbanfieldMetadataId', description: 'View Kanban column field', }) + /** + * @deprecated Use `viewGroups.fieldMetadataId` instead + */ kanbanFieldMetadataId: string; @WorkspaceField({