Skip to content

Commit

Permalink
feat: NoValue is bot properly created the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
magrinj committed Dec 17, 2024
1 parent 1f4d135 commit 79ee9f0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ export const useHandleRecordGroupField = ({
}) satisfies ViewGroup,
);

if (
!existingGroupKeys.has(`${fieldMetadataItem.id}:`) &&
fieldMetadataItem.isNullable === true
) {
viewGroupsToCreate.push({
__typename: 'ViewGroup',
id: v4(),
fieldValue: '',
isVisible: true,
position: fieldMetadataItem.options.length,
fieldMetadataId: fieldMetadataItem.id,
} satisfies ViewGroup);
}

const viewGroupsToDelete = view.viewGroups.filter(
(group) => group.fieldMetadataId !== fieldMetadataItem.id,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { v4 } from 'uuid';
import { isDefined } from '~/utils/isDefined';

import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
Expand Down Expand Up @@ -42,46 +41,25 @@ export const mapViewGroupsToRecordGroupDefinitions = ({
(option) => option.value === viewGroup.fieldValue,
);

if (!selectedOption) {
if (!selectedOption && selectFieldMetadataItem.isNullable === false) {
return null;
}

return {
id: viewGroup.id,
fieldMetadataId: viewGroup.fieldMetadataId,
type: RecordGroupDefinitionType.Value,
title: selectedOption.label,
value: selectedOption.value,
color: selectedOption.color,
type: !isDefined(selectedOption)
? RecordGroupDefinitionType.NoValue
: RecordGroupDefinitionType.Value,
title: selectedOption?.label ?? 'No Value',
value: selectedOption?.value ?? null,
color: selectedOption?.color ?? 'transparent',
position: viewGroup.position,
isVisible: viewGroup.isVisible,
} as RecordGroupDefinition;
})
.filter(isDefined);

if (selectFieldMetadataItem.isNullable === true) {
const viewGroup = viewGroups.find(
(viewGroup) => viewGroup.fieldValue === '',
);

const noValueColumn = {
id: viewGroup?.id ?? v4(),
title: 'No Value',
type: RecordGroupDefinitionType.NoValue,
value: null,
position:
viewGroup?.position ??
recordGroupDefinitionsFromViewGroups
.map((option) => option.position)
.reduce((a, b) => Math.max(a, b), 0) + 1,
isVisible: viewGroup?.isVisible ?? true,
fieldMetadataId: selectFieldMetadataItem.id,
color: 'transparent',
} satisfies RecordGroupDefinition;

return [...recordGroupDefinitionsFromViewGroups, noValueColumn];
}

return recordGroupDefinitionsFromViewGroups.sort(
(a, b) => a.position - b.position,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'src/engine/metadata-modules/field-metadata/dtos/options.input';
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { isSelectFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-select-field-metadata-type.util';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { ViewGroupWorkspaceEntity } from 'src/modules/view/standard-objects/view-group.workspace-entity';
import { ViewWorkspaceEntity } from 'src/modules/view/standard-objects/view.workspace-entity';
Expand All @@ -28,7 +29,7 @@ export class FieldMetadataRelatedRecordsService {
oldFieldMetadata: FieldMetadataEntity,
newFieldMetadata: FieldMetadataEntity,
transactionManager?: EntityManager,
) {
): Promise<void> {
if (
!isSelectFieldMetadataType(newFieldMetadata.type) ||
!isSelectFieldMetadataType(oldFieldMetadata.type)
Expand All @@ -54,10 +55,7 @@ export class FieldMetadataRelatedRecordsService {
continue;
}

const maxPosition = view.viewGroups.reduce(
(max, viewGroup) => Math.max(max, viewGroup.position),
0,
);
const maxPosition = this.getMaxPosition(view.viewGroups);

const viewGroupsToCreate = created.map((option, index) =>
viewGroupRepository.create({
Expand All @@ -72,21 +70,19 @@ export class FieldMetadataRelatedRecordsService {
await viewGroupRepository.insert(viewGroupsToCreate, transactionManager);

for (const { old: oldOption, new: newOption } of updated) {
const viewGroup = view.viewGroups.find(
(viewGroup) => viewGroup.fieldValue === oldOption.value,
const existingViewGroup = view.viewGroups.find(
(group) => group.fieldValue === oldOption.value,
);

if (!viewGroup) {
throw new Error(`View group not found for option ${oldOption.value}`);
if (!existingViewGroup) {
throw new Error(
`View group not found for option "${oldOption.value}" during update.`,
);
}

await viewGroupRepository.update(
{
id: viewGroup.id,
},
{
fieldValue: newOption.value,
},
{ id: existingViewGroup.id },
{ fieldValue: newOption.value },
transactionManager,
);
}
Expand All @@ -100,13 +96,49 @@ export class FieldMetadataRelatedRecordsService {
},
transactionManager,
);

await this.syncNoValueViewGroup(
newFieldMetadata,
view,
viewGroupRepository,
transactionManager,
);
}
}

private async syncNoValueViewGroup(
fieldMetadata: FieldMetadataEntity,
view: ViewWorkspaceEntity,
viewGroupRepository: WorkspaceRepository<ViewGroupWorkspaceEntity>,
transactionManager?: EntityManager,
): Promise<void> {
const noValueGroup = view.viewGroups.find(
(group) => group.fieldValue === '',
);

if (fieldMetadata.isNullable && !noValueGroup) {
const maxPosition = this.getMaxPosition(view.viewGroups);
const newGroup = viewGroupRepository.create({
fieldMetadataId: fieldMetadata.id,
fieldValue: '',
position: maxPosition + 1,
isVisible: true,
viewId: view.id,
});

await viewGroupRepository.insert(newGroup, transactionManager);
} else if (!fieldMetadata.isNullable && noValueGroup) {
await viewGroupRepository.delete(
{ id: noValueGroup.id },
transactionManager,
);
}
}

private getOptionsDifferences(
oldOptions: (FieldMetadataDefaultOption | FieldMetadataComplexOption)[],
newOptions: (FieldMetadataDefaultOption | FieldMetadataComplexOption)[],
) {
): Differences<FieldMetadataDefaultOption | FieldMetadataComplexOption> {
const differences: Differences<
FieldMetadataDefaultOption | FieldMetadataComplexOption
> = {
Expand All @@ -115,12 +147,8 @@ export class FieldMetadataRelatedRecordsService {
deleted: [],
};

const oldOptionsMap = new Map(
oldOptions.map((option) => [option.id, option]),
);
const newOptionsMap = new Map(
newOptions.map((option) => [option.id, option]),
);
const oldOptionsMap = new Map(oldOptions.map((opt) => [opt.id, opt]));
const newOptionsMap = new Map(newOptions.map((opt) => [opt.id, opt]));

for (const newOption of newOptions) {
const oldOption = oldOptionsMap.get(newOption.id);
Expand Down Expand Up @@ -150,7 +178,7 @@ export class FieldMetadataRelatedRecordsService {
'view',
);

return await viewRepository.find({
return viewRepository.find({
where: {
viewGroups: {
fieldMetadataId: fieldMetadata.id,
Expand All @@ -159,4 +187,8 @@ export class FieldMetadataRelatedRecordsService {
relations: ['viewGroups'],
});
}

private getMaxPosition(viewGroups: ViewGroupWorkspaceEntity[]): number {
return viewGroups.reduce((max, group) => Math.max(max, group.position), 0);
}
}

0 comments on commit 79ee9f0

Please sign in to comment.