Fix relation picker for activity target (#8303)

[This PR](https://github.com/twentyhq/twenty/pull/8210) introduced a
regression, causing noteId or taskId (respectively for noteTarget or
taskTarget creation) to be overwritten with an undefined value in the
input for noteTarget or taskTarget creation.
This is because in ActivityTargetInlineCellEditMode, in addition to the
noteId and taskId we are declaring, we are looking into the object
(noteTarget or taskTarget)'s fields and prefilling the record-to-create
with a value, potentially undefined, for all of the object fields.
So when looping over noteTarget's fields, we would find the `note`
relation field, and eventually add `note: undefined` to the
record-to-create input, in addition to the non-empty and valid existing
`noteId`.
Then in sanitizeRecordInput, from the note added right above, we add an
empty noteId to the input from node, overwriting the "good" noteId.

There are several ways to fix this, I chose to update prefillRecord not
to add an empty "note" object that makes no sense in addition to the
"noteId" we already have at this stage.
It is also possible to update `sanitizeRecordInput` not to overwrite a
value from a relation (noteId from note relation) if there is already a
value in the input.
This commit is contained in:
Marie 2024-11-04 14:39:48 +01:00 committed by GitHub
parent 258fd07839
commit 76e8bf33ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 0 deletions

View File

@ -185,10 +185,18 @@ export const ActivityTargetInlineCellEditMode = ({
activityObjectNameSingular === CoreObjectNameSingular.Task
? activity.id
: null,
task:
activityObjectNameSingular === CoreObjectNameSingular.Task
? activity
: null,
noteId:
activityObjectNameSingular === CoreObjectNameSingular.Note
? activity.id
: null,
note:
activityObjectNameSingular === CoreObjectNameSingular.Note
? activity
: null,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
[fieldName]: record.record,

View File

@ -98,6 +98,7 @@ export const useFavorites = () => {
) => {
createOneFavorite({
[targetObjectNameSingular]: targetRecord,
[`${targetObjectNameSingular}Id`]: targetRecord.id,
position: favorites.length + 1,
workspaceMemberId: currentWorkspaceMember?.id,
});

View File

@ -1,8 +1,10 @@
import { isUndefined } from '@sniptt/guards';
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { generateDefaultFieldValue } from '@/object-record/utils/generateDefaultFieldValue';
import { FieldMetadataType, RelationDefinitionType } from '~/generated/graphql';
import { isDefined } from '~/utils/isDefined';
export const prefillRecord = <T extends ObjectRecord>({
@ -16,6 +18,13 @@ export const prefillRecord = <T extends ObjectRecord>({
objectMetadataItem.fields
.map((fieldMetadataItem) => {
const inputValue = input[fieldMetadataItem.name];
if (
fieldMetadataItem.type === FieldMetadataType.Relation &&
fieldMetadataItem.relationDefinition?.direction ===
RelationDefinitionType.ManyToOne
) {
throwIfInputRelationDataIsInconsistent(input, fieldMetadataItem);
}
return [
fieldMetadataItem.name,
@ -27,3 +36,16 @@ export const prefillRecord = <T extends ObjectRecord>({
.filter(isDefined),
) as T;
};
const throwIfInputRelationDataIsInconsistent = (
input: Record<string, unknown>,
fieldMetadataItem: FieldMetadataItem,
) => {
const inputValue = input[fieldMetadataItem.name];
const relationIdFieldName = `${fieldMetadataItem.name}Id`;
if (isDefined(inputValue) && !isDefined(input[relationIdFieldName])) {
throw new Error(
`Inconsistent input: ${fieldMetadataItem.name} is specified but ${relationIdFieldName} is missing`,
);
}
};