Skip to content
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

fix: record group issues #8854

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
);
Comment on lines +76 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: this filter removes all groups from other fields, which means switching back to a previous field will lose any customizations made to those groups


if (viewGroupsToCreate.length > 0) {
await createViewGroupRecords(viewGroupsToCreate, view);
}

if (viewGroupsToDelete.length > 0) {
await deleteViewGroupRecords(viewGroupsToDelete);
}
Comment on lines 80 to +86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: the order of operations here could cause a brief visual flicker - consider deleting old groups before creating new ones

},
[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 @@ -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;
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 @@ -18,6 +18,9 @@ export type View = {
viewFilters: ViewFilter[];
viewFilterGroups?: ViewFilterGroup[];
viewSorts: ViewSort[];
/**
* @deprecated Use `viewGroups.fieldMetadataId` instead.
*/
kanbanFieldMetadataId: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider making kanbanFieldMetadataId optional since it's deprecated

position: number;
icon: string;
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,
);
Comment on lines 94 to +102
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: check if valuesToDelete is empty before performing delete operation to avoid unnecessary database calls

}
}

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 @@ -84,6 +84,9 @@ export class ViewWorkspaceEntity extends BaseWorkspaceEntity {
label: 'kanbanfieldMetadataId',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: label should be 'Kanban Field Metadata Id' to match standard casing

description: 'View Kanban column field',
})
/**
* @deprecated Use `viewGroups.fieldMetadataId` instead
*/
kanbanFieldMetadataId: string;

@WorkspaceField({
Expand Down
Loading