Skip to content

Commit

Permalink
fix: record group issues (#8854)
Browse files Browse the repository at this point in the history
This PR is fixing the following issues with record groups:

- [x] [Backend] - Only update view groups when a field is edited if this
one already has view groups
- [x] [Backend] - Editing a Select field metadata option brake view
groups
- [x] [Frontend] - Changing the group by field from one to another brake
record group and doesn't remove the previous ones
- [x] [Frontend & Backend] - Mark `kanbanFieldMetadataId` as deprecated
in favour of `viewGroups.fieldMetadataId`

Also the following has been checked:

- [x] Properly displayed a table with only one view groups
- [x] Properly displayed a table without view groups
  • Loading branch information
magrinj authored Dec 4, 2024
1 parent 9142bdf commit 2fa5fb7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions packages/twenty-front/src/modules/views/types/GraphQLView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions packages/twenty-front/src/modules/views/types/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';

import { In } from 'typeorm';
import { EntityManager, In } from 'typeorm';

import {
FieldMetadataComplexOption,
Expand All @@ -27,6 +27,7 @@ export class FieldMetadataRelatedRecordsService {
public async updateRelatedViewGroups(
oldFieldMetadata: FieldMetadataEntity,
newFieldMetadata: FieldMetadataEntity,
transactionManager?: EntityManager,
) {
if (
!isSelectFieldMetadataType(newFieldMetadata.type) ||
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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,
);
}
}

Expand Down Expand Up @@ -143,7 +152,9 @@ export class FieldMetadataRelatedRecordsService {

return await viewRepository.find({
where: {
kanbanFieldMetadataId: fieldMetadata.id,
viewGroups: {
fieldMetadataId: fieldMetadata.id,
},
},
relations: ['viewGroups'],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export class ViewWorkspaceEntity extends BaseWorkspaceEntity {
label: 'kanbanfieldMetadataId',
description: 'View Kanban column field',
})
/**
* @deprecated Use `viewGroups.fieldMetadataId` instead
*/
kanbanFieldMetadataId: string;

@WorkspaceField({
Expand Down

0 comments on commit 2fa5fb7

Please sign in to comment.