Skip to content

Commit

Permalink
fix: str replace is not a function error (#3799)
Browse files Browse the repository at this point in the history
* fix: str.replace is not a function

* fix: resolving comments
  • Loading branch information
manish339k authored and Sai Sankeerth committed Nov 5, 2024
1 parent ec31108 commit f0c1d13
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable no-param-reassign */

const get = require('get-value');
const { cloneDeep } = require('lodash');
const { cloneDeep, isNumber } = require('lodash');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const isString = require('lodash/isString');
const {
constructPayload,
defaultRequestConfig,
Expand Down Expand Up @@ -35,7 +36,18 @@ const updateMappingJson = (mapping) => {
const responseBuilder = async (metadata, message, { Config }, payload) => {
const response = defaultRequestConfig();
const { event } = message;
const filteredCustomerId = removeHyphens(Config.customerId);
const { subAccount } = Config;
let { customerId, loginCustomerId } = Config;
if (isNumber(customerId)) {
customerId = customerId.toString();
}
if (isNumber(loginCustomerId)) {
loginCustomerId = loginCustomerId.toString();
}
if (!isString(customerId) || !isString(loginCustomerId)) {
throw new InstrumentationError('customerId and loginCustomerId should be a string or number');
}
const filteredCustomerId = removeHyphens(customerId);
response.endpoint = `${BASE_ENDPOINT}/${filteredCustomerId}:uploadConversionAdjustments`;
response.body.JSON = payload;
const accessToken = getAccessToken(metadata, 'access_token');
Expand All @@ -45,9 +57,9 @@ const responseBuilder = async (metadata, message, { Config }, payload) => {
'developer-token': getValueFromMessage(metadata, 'secret.developer_token'),
};
response.params = { event, customerId: filteredCustomerId };
if (Config.subAccount)
if (Config.loginCustomerId) {
const filteredLoginCustomerId = removeHyphens(Config.loginCustomerId);
if (subAccount)
if (loginCustomerId) {
const filteredLoginCustomerId = removeHyphens(loginCustomerId);
response.headers['login-customer-id'] = filteredLoginCustomerId;
} else throw new ConfigurationError(`LoginCustomerId is required as subAccount is true.`);

Expand Down
6 changes: 5 additions & 1 deletion src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
} = require('@rudderstack/integrations-lib');

const { JsonTemplateEngine, PathType } = require('@rudderstack/json-template-engine');
const isString = require('lodash/isString');
const logger = require('../../logger');
const stats = require('../../util/stats');
const { DestCanonicalNames, DestHandlerMap } = require('../../constants/destinationCanonicalNames');
Expand Down Expand Up @@ -1622,7 +1623,7 @@ function isHttpStatusRetryable(status) {
function generateUUID() {
return crypto.randomUUID({
disableEntropyCache: true,
}); /* using disableEntropyCache as true to not cache the generated uuids.
}); /* using disableEntropyCache as true to not cache the generated uuids.
For more Info https://nodejs.org/api/crypto.html#cryptorandomuuidoptions:~:text=options%20%3CObject%3E-,disableEntropyCache,-%3Cboolean%3E%20By
*/
}
Expand All @@ -1646,6 +1647,9 @@ function isAppleFamily(platform) {
}

function removeHyphens(str) {
if (!isString(str)) {
return str;
}
return str.replace(/-/g, '');
}

Expand Down
16 changes: 16 additions & 0 deletions src/v0/util/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
validateEventAndLowerCaseConversion,
groupRouterTransformEvents,
isAxiosError,
removeHyphens,
} = require('./index');
const exp = require('constants');

Expand Down Expand Up @@ -968,3 +969,18 @@ describe('isAxiosError', () => {
expect(isAxiosError(error)).toBe(false);
});
});

describe('removeHyphens', () => {
const data = [
{ input: 'hello-w--orld', expected: 'helloworld' },
{ input: '', expected: '' },
{ input: null, expected: null },
{ input: undefined, expected: undefined },
{ input: 12345, expected: 12345 },
];
it('should remove hyphens from string else return the input as it is', () => {
data.forEach(({ input, expected }) => {
expect(removeHyphens(input)).toBe(expected);
});
});
});
Loading

0 comments on commit f0c1d13

Please sign in to comment.