Skip to content

Commit

Permalink
chore: addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikas Venkatraman authored and Vikas Venkatraman committed Jun 3, 2024
1 parent df65834 commit 0d45432
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/v0/destinations/fb_custom_audience/recordTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async function processRecordInputs(groupedRecordInputs) {
// audience id validation
let operationAudienceId = audienceId;
const mappedToDestination = get(message, MappedToDestinationKey);
if (!operationAudienceId && mappedToDestination) {
if (mappedToDestination) {
const { objectType } = getDestinationExternalIDInfoForRetl(message, 'FB_CUSTOM_AUDIENCE');
operationAudienceId = objectType;
}
Expand Down
13 changes: 8 additions & 5 deletions src/v0/destinations/fb_custom_audience/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ const { schemaFields, USER_ADD, USER_DELETE } = require('./config');

const { MappedToDestinationKey } = require('../../../constants');
const { processRecordInputs } = require('./recordTransform');
const logger = require('../../../logger');

function extraKeysPresent(dictionary, keyList) {
function checkForUnsupportedEventTypes(dictionary, keyList) {
const unsupportedEventTypes = [];
// eslint-disable-next-line no-restricted-syntax
for (const key in dictionary) {
if (!keyList.includes(key)) {
return true;
unsupportedEventTypes.push(key);

Check warning on line 31 in src/v0/destinations/fb_custom_audience/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/fb_custom_audience/transform.js#L31

Added line #L31 was not covered by tests
}
}
return false;
return unsupportedEventTypes;
}

// Function responsible prepare the payload field of every event parameter

const preparePayload = (
userUpdateList,
userSchema,
Expand Down Expand Up @@ -244,7 +245,9 @@ const processRouterDest = async (inputs, reqMetadata) => {
let transformedAudienceEvent = [];

const eventTypes = ['record', 'audiencelist'];
if (extraKeysPresent(groupedInputs, eventTypes)) {
const unsupportedEventList = checkForUnsupportedEventTypes(groupedInputs, eventTypes);
if (unsupportedEventList.length > 0) {
logger.info(`unsupported events found ${unsupportedEventList}`);
throw new ConfigurationError('unsupported events present in the event');

Check warning on line 251 in src/v0/destinations/fb_custom_audience/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/fb_custom_audience/transform.js#L250-L251

Added lines #L250 - L251 were not covered by tests
}

Expand Down
125 changes: 125 additions & 0 deletions src/v0/destinations/fb_custom_audience/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const { getDataSource, responseBuilderSimple, getUpdatedDataElement } = require('./util');

const basePayload = {
responseField: {
access_token: 'ABC',
payload: {
schema: ['EMAIL', 'FI'],
data: [
[
'b100c2ec0718fe6b4805b623aeec6710719d042ceea55f5c8135b010ec1c7b36',
'1e14a2f476f7611a8b22bc85d14237fdc88aac828737e739416c32c5bce3bd16',
],
],
},
},
};

// operationCategory: 'add',
// method: "POST",

const baseResponse = {
version: '1',
type: 'REST',
endpoint: 'https://graph.facebook.com/v18.0/23848494844100489/users',
headers: {},
params: {
access_token: 'ABC',
payload: {
schema: ['EMAIL', 'FI'],
data: [
[
'b100c2ec0718fe6b4805b623aeec6710719d042ceea55f5c8135b010ec1c7b36',
'1e14a2f476f7611a8b22bc85d14237fdc88aac828737e739416c32c5bce3bd16',
],
],
},
},
body: {
JSON: {},
JSON_ARRAY: {},
XML: {},
FORM: {},
},
files: {},
};

describe('FB_custom_audience utils test', () => {
describe('getDataSource function tests', () => {
it('Should return empty datasource if type and subType are both NA', () => {
const expectedDataSource = {};
const dataSource = getDataSource('NA', 'NA');
expect(dataSource).toEqual(expectedDataSource);
});
it('Should set subType and type if value present in destination config macthes with preset list', () => {
const expectedDataSource = {
type: 'EVENT_BASED',
};
const dataSource = getDataSource('EVENT_BASED', 'something');
expect(dataSource).toEqual(expectedDataSource);
});
});

describe('responseBuilderSimple function tests', () => {
it('Should return correct response for add payload', () => {
const payload = basePayload;
payload.operationCategory = 'add';
const expectedResponse = baseResponse;
expectedResponse.method = 'POST';
const response = responseBuilderSimple(payload, '23848494844100489');
expect(response).toEqual(expectedResponse);
});

it('Should return correct response for delete payload', () => {
const payload = basePayload;
payload.operationCategory = 'remove';
const expectedResponse = baseResponse;
expectedResponse.method = 'DELETE';
const response = responseBuilderSimple(payload, '23848494844100489');
expect(response).toEqual(expectedResponse);
});

it('Should throw error if payload is empty', () => {
try {
const response = responseBuilderSimple(payload, '');
expect(response).toEqual();
} catch (error) {
expect(error.message).toEqual(`payload is not defined`);
}
});
});

describe('getUpdatedDataElement function tests', () => {
it('Should hash field if isHashRequired is set to true', () => {
const expectedDataElement = [
'59107c750fd5ee2758d1988f2bf12d9f110439221ebdb7997e70d6a2c1c5afda',
];
let dataElement = [];
dataElement = getUpdatedDataElement(dataElement, true, 'FN', 'some-name');
expect(dataElement).toEqual(expectedDataElement);
});

it('Should not hash field if isHashRequired is set to false', () => {
const expectedDataElement = ['some-name'];
let dataElement = [];
dataElement = getUpdatedDataElement(dataElement, false, 'FN', 'some-name');
expect(dataElement).toEqual(expectedDataElement);
});

it('Should not hash MADID or EXTERN_ID and just pass value', () => {
const expectedDataElement = ['some-id', 'some-ext-id'];
let dataElement = [];
dataElement = getUpdatedDataElement(dataElement, true, 'MADID', 'some-id');
dataElement = getUpdatedDataElement(dataElement, true, 'EXTERN_ID', 'some-ext-id');
expect(dataElement).toEqual(expectedDataElement);
});

it('Should not hash MADID or EXTERN_ID and just pass empty value if value does not exist', () => {
const expectedDataElement = ['', ''];
let dataElement = [];
dataElement = getUpdatedDataElement(dataElement, true, 'MADID', '');
dataElement = getUpdatedDataElement(dataElement, true, 'EXTERN_ID', '');
expect(dataElement).toEqual(expectedDataElement);
});
});
});

0 comments on commit 0d45432

Please sign in to comment.