diff --git a/packages/services/src/__tests__/helpers.test.ts b/packages/services/src/__tests__/helpers.test.ts index 28ac4d60..08e7bdc6 100644 --- a/packages/services/src/__tests__/helpers.test.ts +++ b/packages/services/src/__tests__/helpers.test.ts @@ -92,6 +92,31 @@ describe('concatService', () => { const result = concatService(data, ...args); expect(result).toBe('(01)05012345678900(10)2024001(21)TRF240001'); }); + + it('should return an empty string if the path does not exist', () => { + const data = { + test: 'test', + }; + const args: any = [{ type: 'path', value: '/something' }]; + const result = concatService(data, ...args); + expect(result).toBe(''); + }); + + it('should throw an error if the path is invalid', () => { + const data = { + test: 'test', + }; + const args: any = [{ type: 'path', value: 'invalid' }]; + + expect(() => concatService(data, ...args)).toThrow('Error concatenating values'); + }); + + it('should throw an error if the data is not an object', () => { + const data = 'test'; + const args: any = [{ type: 'path', value: '/test' }]; + + expect(() => concatService(data, ...args)).toThrow('Invalid data object'); + }); }); describe('constructIdentifierString', () => { @@ -186,4 +211,29 @@ describe('constructIdentifierString', () => { const result = constructIdentifierString(data, identifierKeyPath); expect(result).toBe('05012345678900'); }); + + it('should return an empty string if the path does not exist', () => { + const data = { + test: 'test', + }; + const identifierKeyPath = '/something'; + const result = constructIdentifierString(data, identifierKeyPath); + expect(result).toBe(''); + }); + + it('should throw an error if the path is invalid', () => { + const data = { + test: 'test', + }; + const identifierKeyPath = 'invalid'; + + expect(() => constructIdentifierString(data, identifierKeyPath)).toThrow('Error constructing identifier string'); + }); + + it('should throw an error if the data is not an object', () => { + const data = 'test'; + const identifierKeyPath = '/test'; + + expect(() => constructIdentifierString(data, identifierKeyPath)).toThrow('Invalid data object'); + }); }); diff --git a/packages/services/src/utils/helpers.ts b/packages/services/src/utils/helpers.ts index a4f70767..b7bef623 100644 --- a/packages/services/src/utils/helpers.ts +++ b/packages/services/src/utils/helpers.ts @@ -91,7 +91,17 @@ export const concatService = (data: any, ...args: ConcatArgument[]): string => { if (arg.type === ConcatArgumentType.TEXT) { return acc.concat(arg.value); } else if (arg.type === ConcatArgumentType.PATH) { - return acc.concat(JSONPointer.get(data, arg.value)); + if (typeof data !== 'object') { + throw new Error('Invalid data object'); + } + try { + const value = JSONPointer.get(data, arg.value); + if (value) { + return acc.concat(value); + } + } catch (error) { + throw new Error('Error concatenating values'); + } } return acc; }, ''); @@ -295,8 +305,20 @@ export const constructIdentifierString = ( data: any, identifierKeyPath: string | { function: string; args: any }, ): string => { + if (typeof data !== 'object') { + throw new Error('Invalid data object'); + } + if (typeof identifierKeyPath === 'string') { - return JSONPointer.get(data, identifierKeyPath); + try { + const value = JSONPointer.get(data, identifierKeyPath); + if (value) { + return value; + } + return ''; + } catch (error) { + throw new Error('Error constructing identifier string'); + } } else { try { const handlerFunction: any = supportedHandlerFunctions[identifierKeyPath.function]; @@ -305,7 +327,6 @@ export const constructIdentifierString = ( } return handlerFunction(data, ...identifierKeyPath.args); } catch (error: any) { - console.log(error); throw new Error('Error constructing identifier string using handler function'); } }