From 82ffd9b995b7ca456afc2a3e83199dc9d6dd22ad Mon Sep 17 00:00:00 2001 From: krishnachaitanya Date: Thu, 21 Nov 2024 13:53:59 +0530 Subject: [PATCH] chore: refactored code --- src/v0/destinations/airship/transform.js | 12 +++++++++++- src/v0/util/index.js | 24 ++++++------------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/v0/destinations/airship/transform.js b/src/v0/destinations/airship/transform.js index 29b529b1e77..f4459c08b6c 100644 --- a/src/v0/destinations/airship/transform.js +++ b/src/v0/destinations/airship/transform.js @@ -22,12 +22,22 @@ const { extractCustomFields, isEmptyObject, simpleProcessRouterDest, - transformSessionId, + convertToUuid, } = require('../../util'); const { JSON_MIME_TYPE } = require('../../util/constant'); const DEFAULT_ACCEPT_HEADER = 'application/vnd.urbanairship+json; version=3'; +const transformSessionId = (rawSessionId) => { + const sessionId = String(rawSessionId).trim(); // Attempt conversion to string and trim whitespace + if (!sessionId) { + throw new InstrumentationError( + 'Invalid session ID: must be a non-empty string after conversion to string.', + ); + } + return convertToUuid(sessionId); // Return the validated and converted session ID +}; + const identifyResponseBuilder = (message, { Config }) => { const tagPayload = constructPayload(message, identifyMapping); const { apiKey, dataCenter } = Config; diff --git a/src/v0/util/index.js b/src/v0/util/index.js index a032c763f11..314e6c18182 100644 --- a/src/v0/util/index.js +++ b/src/v0/util/index.js @@ -2332,25 +2332,13 @@ const isEventSentByVDMV1Flow = (event) => event?.message?.context?.mappedToDesti const isEventSentByVDMV2Flow = (event) => event?.connection?.config?.destination?.schemaVersion === VDM_V2_SCHEMA_VERSION; -const validateSessionId = (rawSessionId) => { - const sessionId = String(rawSessionId).trim(); // Attempt conversion to string and trim whitespace - if (!sessionId) { - throw new InstrumentationError( - 'Invalid session ID: must be a non-empty string after conversion to string.', - ); - } - return sessionId; // Return the validated and converted session ID -}; - -const transformSessionId = (rawSessionId) => { +const convertToUuid = (input) => { + const NAMESPACE = v5.DNS; try { - const sessionId = validateSessionId(rawSessionId); - - const NAMESPACE = v5.DNS; - const uuidV5 = v5(sessionId, NAMESPACE); - return uuidV5; + return v5(input, NAMESPACE); } catch (error) { - throw new InstrumentationError(`Failed to transform session ID: ${error.message}`); + const errorMessage = `Failed to transform input to uuid: ${error.message}`; + throw new InstrumentationError(errorMessage); } }; // ======================================================================== @@ -2479,5 +2467,5 @@ module.exports = { getRelativePathFromURL, removeEmptyKey, isAxiosError, - transformSessionId, + convertToUuid, };