From 7e2ad3b3e15b45fca7748e8dcc33148adb8e20cf Mon Sep 17 00:00:00 2001 From: Gabriel Jablonski Date: Tue, 9 Dec 2025 12:59:48 -0300 Subject: [PATCH] fix: handle potential null values in automation type retrieval (#163) * fix: handle potential null values in automation type retrieval * fix: handle cases where custom attribute keys are not found, returning default values --- .../dashboard/helper/automationHelper.js | 13 +++--- .../helper/specs/automationHelper.spec.js | 44 +++++++++++++++++-- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/app/javascript/dashboard/helper/automationHelper.js b/app/javascript/dashboard/helper/automationHelper.js index 3e5f46f90..472256156 100644 --- a/app/javascript/dashboard/helper/automationHelper.js +++ b/app/javascript/dashboard/helper/automationHelper.js @@ -285,7 +285,7 @@ export const getInputType = ( return getCustomAttributeInputType(customAttribute.attribute_display_type); } const type = getAutomationType(automationTypes, automation, key); - return type.inputType; + return type?.inputType ?? ''; }; /** @@ -311,7 +311,7 @@ export const getOperators = ( } } const type = getAutomationType(automationTypes, automation, key); - return type.filterOperators; + return type?.filterOperators ?? []; }; /** @@ -322,9 +322,10 @@ export const getOperators = ( * @returns {string} The custom attribute type. */ export const getCustomAttributeType = (automationTypes, automation, key) => { - return automationTypes[automation.event_name].conditions.find( - i => i.key === key - ).customAttributeType; + return ( + automationTypes[automation.event_name].conditions.find(i => i.key === key) + ?.customAttributeType ?? '' + ); }; /** @@ -336,6 +337,6 @@ export const getCustomAttributeType = (automationTypes, automation, key) => { export const showActionInput = (automationActionTypes, action) => { if (action === 'send_email_to_team' || action === 'send_message') return false; - const type = automationActionTypes.find(i => i.key === action).inputType; + const type = automationActionTypes.find(i => i.key === action)?.inputType; return !!type; }; diff --git a/app/javascript/dashboard/helper/specs/automationHelper.spec.js b/app/javascript/dashboard/helper/specs/automationHelper.spec.js index 10088963a..62587525a 100644 --- a/app/javascript/dashboard/helper/specs/automationHelper.spec.js +++ b/app/javascript/dashboard/helper/specs/automationHelper.spec.js @@ -391,6 +391,17 @@ describe('getInputType', () => { ); expect(result).toEqual('search_select'); }); + + it('returns empty string when attribute key is not found', () => { + const mockAutomation = { event_name: 'message_created' }; + const result = helpers.getInputType( + customAttributes, + AUTOMATIONS, + mockAutomation, + 'non_existent_key' + ); + expect(result).toEqual(''); + }); }); describe('getOperators', () => { @@ -420,6 +431,18 @@ describe('getOperators', () => { .filterOperators ); }); + + it('returns empty array when attribute key is not found', () => { + const mockAutomation = { event_name: 'message_created' }; + const result = helpers.getOperators( + customAttributes, + AUTOMATIONS, + mockAutomation, + 'create', + 'non_existent_key' + ); + expect(result).toEqual([]); + }); }); describe('getCustomAttributeType', () => { @@ -430,10 +453,18 @@ describe('getCustomAttributeType', () => { mockAutomation, 'message_type' ); - expect(result).toEqual( - AUTOMATIONS.message_created.conditions.find(c => c.key === 'message_type') - .customAttributeType + // message_type condition doesn't have customAttributeType defined, so it returns empty string + expect(result).toEqual(''); + }); + + it('returns empty string when attribute key is not found', () => { + const mockAutomation = { event_name: 'message_created' }; + const result = helpers.getCustomAttributeType( + AUTOMATIONS, + mockAutomation, + 'non_existent_key' ); + expect(result).toEqual(''); }); }); @@ -452,4 +483,11 @@ describe('showActionInput', () => { const mockActionTypes = [{ key: 'some_action', inputType: null }]; expect(helpers.showActionInput(mockActionTypes, 'some_action')).toBe(false); }); + + it('returns false when action key is not found in action types', () => { + const mockActionTypes = [{ key: 'add_label', inputType: 'select' }]; + expect( + helpers.showActionInput(mockActionTypes, 'non_existent_action') + ).toBe(false); + }); });