The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(server): preserve null values in record update util (#23639)
Thomas Trompettecb338962f5c0
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.tsnew file mode 100644index 00000000000..e1bcf4cc485--- /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.tsindex 239b9f5878e..94026a67829 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 = <T extends Record<string, unknown>>(const result: Record<string, unknown> = {};for (const [key, value] of Object.entries(record)) {- if (!isDefined(value)) {+ if (value === undefined) {continue;}