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
This commit is contained in:
Gabriel Jablonski 2025-12-09 12:59:48 -03:00 committed by GitHub
parent 797bde6566
commit 7e2ad3b3e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 9 deletions

View File

@ -285,7 +285,7 @@ export const getInputType = (
return getCustomAttributeInputType(customAttribute.attribute_display_type); return getCustomAttributeInputType(customAttribute.attribute_display_type);
} }
const type = getAutomationType(automationTypes, automation, key); 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); 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. * @returns {string} The custom attribute type.
*/ */
export const getCustomAttributeType = (automationTypes, automation, key) => { export const getCustomAttributeType = (automationTypes, automation, key) => {
return automationTypes[automation.event_name].conditions.find( return (
i => i.key === key automationTypes[automation.event_name].conditions.find(i => i.key === key)
).customAttributeType; ?.customAttributeType ?? ''
);
}; };
/** /**
@ -336,6 +337,6 @@ export const getCustomAttributeType = (automationTypes, automation, key) => {
export const showActionInput = (automationActionTypes, action) => { export const showActionInput = (automationActionTypes, action) => {
if (action === 'send_email_to_team' || action === 'send_message') if (action === 'send_email_to_team' || action === 'send_message')
return false; return false;
const type = automationActionTypes.find(i => i.key === action).inputType; const type = automationActionTypes.find(i => i.key === action)?.inputType;
return !!type; return !!type;
}; };

View File

@ -391,6 +391,17 @@ describe('getInputType', () => {
); );
expect(result).toEqual('search_select'); 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', () => { describe('getOperators', () => {
@ -420,6 +431,18 @@ describe('getOperators', () => {
.filterOperators .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', () => { describe('getCustomAttributeType', () => {
@ -430,10 +453,18 @@ describe('getCustomAttributeType', () => {
mockAutomation, mockAutomation,
'message_type' 'message_type'
); );
expect(result).toEqual( // message_type condition doesn't have customAttributeType defined, so it returns empty string
AUTOMATIONS.message_created.conditions.find(c => c.key === 'message_type') expect(result).toEqual('');
.customAttributeType });
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 }]; const mockActionTypes = [{ key: 'some_action', inputType: null }];
expect(helpers.showActionInput(mockActionTypes, 'some_action')).toBe(false); 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);
});
}); });