Skip to content

Commit

Permalink
Merge branch 'develop' into fix.braze-gen-tf-error
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpj2292 authored Nov 29, 2023
2 parents 8f6a72c + cd9a046 commit c4670d8
Show file tree
Hide file tree
Showing 7 changed files with 1,153 additions and 1,031 deletions.
23 changes: 18 additions & 5 deletions src/v0/destinations/sfmc/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const {
NetworkError,
ConfigurationError,
InstrumentationError,
isDefinedAndNotNull,
isEmpty,
} = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { EventType } = require('../../../constants');
Expand All @@ -17,7 +19,6 @@ const {
flattenJson,
toTitleCase,
getHashFromArray,
isEmpty,
simpleProcessRouterDest,
} = require('../../util');
const {
Expand Down Expand Up @@ -221,10 +222,22 @@ const responseBuilderSimple = async (message, category, destination) => {
}

if (category.type === 'identify' && createOrUpdateContacts) {
throw new ConfigurationError('Creating or updating contacts is disabled');
throw new ConfigurationError(
'Creating or updating contacts is disabled. To enable this feature set "Do Not Create or Update Contacts" to false',
);
}

if (category.type === 'track' && hashMapExternalKey[message.event.toLowerCase()]) {
if (category.type === 'track') {
if (isEmpty(message.event)) {
throw new ConfigurationError('Event name is required for track events');
}
if (typeof message.event !== 'string') {
throw new ConfigurationError('Event name must be a string');
}
if (!isDefinedAndNotNull(hashMapExternalKey[message.event.toLowerCase()])) {
throw new ConfigurationError('Event not mapped for this track call');
}

return responseBuilderForInsertData(
message,
hashMapExternalKey[message.event.toLowerCase()],
Expand All @@ -237,7 +250,7 @@ const responseBuilderSimple = async (message, category, destination) => {
);
}

throw new ConfigurationError('Event not mapped for this track call');
throw new ConfigurationError(`Event type '${category.type}' not supported`);
};

const processEvent = async (message, destination) => {
Expand Down Expand Up @@ -274,4 +287,4 @@ const processRouterDest = async (inputs, reqMetadata) => {
return respList;
};

module.exports = { process, processRouterDest };
module.exports = { process, processRouterDest, responseBuilderSimple };
125 changes: 125 additions & 0 deletions src/v0/destinations/sfmc/transform.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const { ConfigurationError } = require('@rudderstack/integrations-lib');
const axios = require('axios');
const MockAxiosAdapter = require('axios-mock-adapter');
const { responseBuilderSimple } = require('./transform');
beforeAll(() => {
const mock = new MockAxiosAdapter(axios);
mock
.onPost('https://yourSubDomain.auth.marketingcloudapis.com/v2/token')
.reply(200, '{"access_token":"yourAuthToken"}');
});

describe('responseBuilderSimple', () => {
const destination = {
Config: {
clientId: 'yourClientId',
clientSecret: 'yourClientSecret',
subDomain: 'yourSubDomain',
createOrUpdateContacts: false,
externalKey: 'yourExternalKey',
eventToExternalKey: [{ from: 'purchase', to: 'purchaseKey' }],
eventToPrimaryKey: [{ from: 'purchase', to: 'primaryKey' }],
eventToUUID: [{ event: 'purchase', uuid: true }],
},
};
it('should return an array of two payloads for identify calls when createOrUpdateContacts is false', async () => {
const message = {
type: 'identify',
userId: '12345',
};

const category = {
type: 'identify',
name: 'Identify',
};

const response = await responseBuilderSimple(message, category, destination);

expect(response).toHaveLength(2);
expect(response[0]).toHaveProperty('endpoint');
expect(response[0]).toHaveProperty('method');
expect(response[0]).toHaveProperty('body.JSON');
expect(response[0]).toHaveProperty('headers');
expect(response[1]).toHaveProperty('endpoint');
expect(response[1]).toHaveProperty('method');
expect(response[1]).toHaveProperty('body.JSON');
expect(response[1]).toHaveProperty('headers');
});

// Throws an error when event name is not provided for track calls
it('should throw an error when event name is not provided for track calls', async () => {
const message = {
type: 'track',
};

const category = {
type: 'track',
name: 'Track',
};

try {
await responseBuilderSimple(message, category, destination);
} catch (e) {
expect(e).toBeInstanceOf(ConfigurationError);
expect(e.message).toBe('Event name is required for track events');
}
});

// Throws an error when event is not mapped for track calls
it('should throw an error when event is not mapped for track calls', async () => {
const message = {
type: 'track',
event: 'unmappedEvent',
};

const category = {
type: 'track',
name: 'Track',
};
try {
await responseBuilderSimple(message, category, destination);
} catch (e) {
expect(e).toBeInstanceOf(ConfigurationError);
expect(e.message).toBe('Event not mapped for this track call');
}
});

// Throws an error when event type is not supported
it('should throw an error when event type is not supported', async () => {
const message = {
type: 'unsupported',
};

const category = {
type: 'unsupported',
name: 'Unsupported',
};

try {
await responseBuilderSimple(message, category, destination);
} catch (e) {
expect(e).toBeInstanceOf(ConfigurationError);
expect(e.message).toBe("Event type 'unsupported' not supported");
}
});

// Returns a payload for track calls when event is mapped and event name is a string
it('should return a payload for track calls when event is mapped and event name is a string', async () => {
const message = {
type: 'track',
event: 'purchase',
userId: '12345',
};

const category = {
type: 'track',
name: 'Track',
};

const response = await responseBuilderSimple(message, category, destination);
expect(response).toHaveProperty('endpoint');
expect(response).toHaveProperty('method');
expect(response).toHaveProperty('body.JSON');
expect(response).toHaveProperty('headers');
});
});
3 changes: 3 additions & 0 deletions src/v0/util/facebookUtils/networkHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ const errorDetailsMap = {
.setMessage('There have been too many calls to this ad-account.')
.build(),
},
200: {
default: new ErrorDetailsExtractorBuilder().setStatus(403).setMessageField('message').build(),
},
};

const getErrorDetailsFromErrorMap = (error) => {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/data/sfmc_output.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"error": "Creating or updating contacts is disabled"
"error": "Creating or updating contacts is disabled. To enable this feature set \"Do Not Create or Update Contacts\" to false"
},
[
{
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/data/sfmc_router_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"batched": false,
"statusCode": 400,
"error": "Creating or updating contacts is disabled",
"error": "Creating or updating contacts is disabled. To enable this feature set \"Do Not Create or Update Contacts\" to false",
"statTags": {
"errorCategory": "dataValidation",
"errorType": "configuration"
Expand Down
Loading

0 comments on commit c4670d8

Please sign in to comment.