diff --git a/src/v0/destinations/fb_custom_audience/recordTransform.js b/src/v0/destinations/fb_custom_audience/recordTransform.js index f863ca84c2..0f7b65c0bf 100644 --- a/src/v0/destinations/fb_custom_audience/recordTransform.js +++ b/src/v0/destinations/fb_custom_audience/recordTransform.js @@ -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; } diff --git a/src/v0/destinations/fb_custom_audience/transform.js b/src/v0/destinations/fb_custom_audience/transform.js index 80d22165ca..c5c340c043 100644 --- a/src/v0/destinations/fb_custom_audience/transform.js +++ b/src/v0/destinations/fb_custom_audience/transform.js @@ -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); } } - return false; + return unsupportedEventTypes; } // Function responsible prepare the payload field of every event parameter - const preparePayload = ( userUpdateList, userSchema, @@ -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'); } diff --git a/src/v0/destinations/fb_custom_audience/util.test.js b/src/v0/destinations/fb_custom_audience/util.test.js new file mode 100644 index 0000000000..e6e838d0de --- /dev/null +++ b/src/v0/destinations/fb_custom_audience/util.test.js @@ -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); + }); + }); +});