Skip to content

Commit 8f18e1a

Browse files
authored
fix: str replace is not a function error (#3799)
* fix: str.replace is not a function * fix: resolving comments
1 parent f79dfe7 commit 8f18e1a

File tree

4 files changed

+345
-6
lines changed

4 files changed

+345
-6
lines changed

src/v0/destinations/google_adwords_enhanced_conversions/transform.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* eslint-disable no-param-reassign */
22

33
const get = require('get-value');
4-
const { cloneDeep } = require('lodash');
4+
const { cloneDeep, isNumber } = require('lodash');
55
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
6+
const isString = require('lodash/isString');
67
const {
78
constructPayload,
89
defaultRequestConfig,
@@ -35,7 +36,18 @@ const updateMappingJson = (mapping) => {
3536
const responseBuilder = async (metadata, message, { Config }, payload) => {
3637
const response = defaultRequestConfig();
3738
const { event } = message;
38-
const filteredCustomerId = removeHyphens(Config.customerId);
39+
const { subAccount } = Config;
40+
let { customerId, loginCustomerId } = Config;
41+
if (isNumber(customerId)) {
42+
customerId = customerId.toString();
43+
}
44+
if (isNumber(loginCustomerId)) {
45+
loginCustomerId = loginCustomerId.toString();
46+
}
47+
if (!isString(customerId) || !isString(loginCustomerId)) {
48+
throw new InstrumentationError('customerId and loginCustomerId should be a string or number');
49+
}
50+
const filteredCustomerId = removeHyphens(customerId);
3951
response.endpoint = `${BASE_ENDPOINT}/${filteredCustomerId}:uploadConversionAdjustments`;
4052
response.body.JSON = payload;
4153
const accessToken = getAccessToken(metadata, 'access_token');
@@ -45,9 +57,9 @@ const responseBuilder = async (metadata, message, { Config }, payload) => {
4557
'developer-token': getValueFromMessage(metadata, 'secret.developer_token'),
4658
};
4759
response.params = { event, customerId: filteredCustomerId };
48-
if (Config.subAccount)
49-
if (Config.loginCustomerId) {
50-
const filteredLoginCustomerId = removeHyphens(Config.loginCustomerId);
60+
if (subAccount)
61+
if (loginCustomerId) {
62+
const filteredLoginCustomerId = removeHyphens(loginCustomerId);
5163
response.headers['login-customer-id'] = filteredLoginCustomerId;
5264
} else throw new ConfigurationError(`LoginCustomerId is required as subAccount is true.`);
5365

src/v0/util/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
} = require('@rudderstack/integrations-lib');
2727

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

16481649
function removeHyphens(str) {
1650+
if (!isString(str)) {
1651+
return str;
1652+
}
16491653
return str.replace(/-/g, '');
16501654
}
16511655

src/v0/util/index.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
validateEventAndLowerCaseConversion,
1111
groupRouterTransformEvents,
1212
isAxiosError,
13+
removeHyphens,
1314
} = require('./index');
1415
const exp = require('constants');
1516

@@ -968,3 +969,18 @@ describe('isAxiosError', () => {
968969
expect(isAxiosError(error)).toBe(false);
969970
});
970971
});
972+
973+
describe('removeHyphens', () => {
974+
const data = [
975+
{ input: 'hello-w--orld', expected: 'helloworld' },
976+
{ input: '', expected: '' },
977+
{ input: null, expected: null },
978+
{ input: undefined, expected: undefined },
979+
{ input: 12345, expected: 12345 },
980+
];
981+
it('should remove hyphens from string else return the input as it is', () => {
982+
data.forEach(({ input, expected }) => {
983+
expect(removeHyphens(input)).toBe(expected);
984+
});
985+
});
986+
});

0 commit comments

Comments
 (0)