diff --git a/packages/twenty-server/src/engine/core-modules/record-crud/utils/__tests__/remove-undefined-from-record.util.spec.ts b/packages/twenty-server/src/engine/core-modules/record-crud/utils/__tests__/remove-undefined-from-record.util.spec.ts new file mode 100644 index 0000000000..e1bcf4cc48 --- /dev/null +++ b/packages/twenty-server/src/engine/core-modules/record-crud/utils/__tests__/remove-undefined-from-record.util.spec.ts @@ -0,0 +1,40 @@ +import { removeUndefinedFromRecord } from 'src/engine/core-modules/record-crud/utils/remove-undefined-from-record.util'; + +describe('removeUndefinedFromRecord', () => { + it('should strip undefined values', () => { + expect(removeUndefinedFromRecord({ name: 'John', age: undefined })).toEqual( + { + name: 'John', + }, + ); + }); + + it('should preserve null values so a field can be cleared', () => { + expect( + removeUndefinedFromRecord({ name: 'John', closeDate: null }), + ).toEqual({ name: 'John', closeDate: null }); + }); + + it('should preserve null sub-properties in composite fields', () => { + expect( + removeUndefinedFromRecord({ + emails: { primaryEmail: null, additionalEmails: undefined }, + }), + ).toEqual({ emails: { primaryEmail: null } }); + }); + + it('should drop nested objects that only contain undefined', () => { + expect( + removeUndefinedFromRecord({ + emails: { primaryEmail: undefined }, + name: 'John', + }), + ).toEqual({ name: 'John' }); + }); + + it('should preserve arrays as-is', () => { + expect( + removeUndefinedFromRecord({ tags: ['a', 'b'], removed: undefined }), + ).toEqual({ tags: ['a', 'b'] }); + }); +}); diff --git a/packages/twenty-server/src/engine/core-modules/record-crud/utils/remove-undefined-from-record.util.ts b/packages/twenty-server/src/engine/core-modules/record-crud/utils/remove-undefined-from-record.util.ts index 239b9f5878..94026a6782 100644 --- a/packages/twenty-server/src/engine/core-modules/record-crud/utils/remove-undefined-from-record.util.ts +++ b/packages/twenty-server/src/engine/core-modules/record-crud/utils/remove-undefined-from-record.util.ts @@ -1,5 +1,3 @@ -import { isDefined } from 'twenty-shared/utils'; - // Recursively removes undefined values from an object // This is needed because workflows/tools may pass partial composite fields // with undefined sub-properties, but the validation layer expects either @@ -10,7 +8,7 @@ export const removeUndefinedFromRecord = >( const result: Record = {}; for (const [key, value] of Object.entries(record)) { - if (!isDefined(value)) { + if (value === undefined) { continue; }