breaking (soft) - Migrate viewGroup.fieldMetadataId -> view.mainGroupByFieldMetadataId (2/3) (#16277)

Should be merged once https://github.com/twentyhq/twenty/pull/16206 has
been released + command run to prod

In this PR
- Remove usage of viewGroup.fieldMetadataId, both in BE and FE states. 
- But we still need to properly populate it until we fully remove
viewGroup.fieldMetadataId from db and ORM entity (upcoming 3rd PR out of
3). fieldMetadataId was removed from CoreViewGroup type and
CreateViewGroupInput and is determined BE-side based on the associated
view's mainGroupByFieldMetadataId. **I expect this means a downtime on
viewGroup creation, until both FE and BE are deployed and cache is
flushed.** This seems acceptable to me as it only regards viewGroup
creation.
    - this information is replaced by view.mainGroupByFieldMetadataID
- Handle view group creation, update and deletion in the BE as a
side-effect of a view creation, update or deletion. Optimistic effects
are still used
- Add validation at view creation or update regarding
mainGroupByFieldMetadata

Left to do in 3rd PR
- Remove viewGroup.fieldMetadataId from db and ORM entity
- Restore feature allowing to update an existing grouped view's group by
field (already OK on BE side but need to rebuild FE optimistic)
This commit is contained in:
Marie
2025-12-05 15:00:03 +01:00
committed by GitHub
parent 17d88a074b
commit 7ce22d5c7e
87 changed files with 1338 additions and 1067 deletions
@@ -11,9 +11,9 @@ import { usePersistView } from '@/views/hooks/internal/usePersistView';
import { usePersistViewField } from '@/views/hooks/internal/usePersistViewField';
import { usePersistViewFilterRecords } from '@/views/hooks/internal/usePersistViewFilter';
import { usePersistViewFilterGroupRecords } from '@/views/hooks/internal/usePersistViewFilterGroup';
import { usePersistViewGroupRecords } from '@/views/hooks/internal/usePersistViewGroup';
import { usePersistViewSortRecords } from '@/views/hooks/internal/usePersistViewSort';
import { useRefreshCoreViewsByObjectMetadataId } from '@/views/hooks/useRefreshCoreViewsByObjectMetadataId';
import { useTriggerViewGroupOptimisticEffect } from '@/views/optimistic-effects/hooks/useTriggerViewGroupOptimisticEffect';
import { isPersistingViewFieldsState } from '@/views/states/isPersistingViewFieldsState';
import { coreViewFromViewIdFamilySelector } from '@/views/states/selectors/coreViewFromViewIdFamilySelector';
import { type GraphQLView } from '@/views/types/GraphQLView';
@@ -28,7 +28,10 @@ import { mapRecordSortToViewSort } from '@/views/utils/mapRecordSortToViewSort';
import { useRecoilCallback } from 'recoil';
import { isDefined } from 'twenty-shared/utils';
import { v4 } from 'uuid';
import { ViewCalendarLayout } from '~/generated-metadata/graphql';
import {
type CoreViewGroup,
ViewCalendarLayout,
} from '~/generated-metadata/graphql';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
export const useCreateViewFromCurrentView = (viewBarComponentId?: string) => {
@@ -47,7 +50,8 @@ export const useCreateViewFromCurrentView = (viewBarComponentId?: string) => {
const { createViewSorts } = usePersistViewSortRecords();
const { createViewGroups } = usePersistViewGroupRecords();
const { triggerViewGroupOptimisticEffect } =
useTriggerViewGroupOptimisticEffect();
const { createViewFilters } = usePersistViewFilterRecords();
@@ -182,7 +186,10 @@ export const useCreateViewFromCurrentView = (viewBarComponentId?: string) => {
}
if (type === ViewType.Kanban) {
if (!isDefined(mainGroupByFieldMetadataId)) {
if (
!isDefined(mainGroupByFieldMetadataId) ||
mainGroupByFieldMetadataId === ''
) {
throw new Error('Kanban view must have a kanban field');
}
@@ -194,33 +201,38 @@ export const useCreateViewFromCurrentView = (viewBarComponentId?: string) => {
({
id: v4(),
__typename: 'ViewGroup',
fieldMetadataId: mainGroupByFieldMetadataId,
fieldValue: option.value,
isVisible: true,
position: index,
}) satisfies ViewGroup,
) ?? [];
viewGroupsToCreate.push({
__typename: 'ViewGroup',
id: v4(),
fieldValue: '',
position: viewGroupsToCreate.length,
isVisible: true,
fieldMetadataId: mainGroupByFieldMetadataId,
} satisfies ViewGroup);
const groupResult = await createViewGroups({
inputs: viewGroupsToCreate.map(({ __typename, ...viewGroup }) => ({
...viewGroup,
viewId: newViewId,
})),
});
if (groupResult.status === 'failed') {
set(isPersistingViewFieldsState, false);
return undefined;
if (
objectMetadataItem.fields.find(
(field) => field.id === mainGroupByFieldMetadataId,
)?.isNullable === true
) {
viewGroupsToCreate.push({
__typename: 'ViewGroup',
id: v4(),
fieldValue: '',
position: viewGroupsToCreate.length,
isVisible: true,
} satisfies ViewGroup);
}
triggerViewGroupOptimisticEffect({
createdViewGroups: viewGroupsToCreate.map(
({ __typename, ...viewGroup }) =>
({
...viewGroup,
viewId: newViewId,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
deletedAt: null,
}) as Omit<CoreViewGroup, 'workspaceId'>,
),
});
}
if (shouldCopyFiltersAndSortsAndAggregate === true) {
@@ -292,7 +304,6 @@ export const useCreateViewFromCurrentView = (viewBarComponentId?: string) => {
anyFieldFilterValue,
objectMetadataItem.fields,
objectMetadataItem.id,
createViewGroups,
currentRecordFilterGroups,
currentRecordFilters,
currentRecordSorts,
@@ -300,6 +311,7 @@ export const useCreateViewFromCurrentView = (viewBarComponentId?: string) => {
createViewFilters,
createViewSorts,
refreshCoreViewsByObjectMetadataId,
triggerViewGroupOptimisticEffect,
],
);