Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: changes for supporting record event in FB audience #3351

Merged
merged 17 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 35 additions & 42 deletions src/v0/destinations/fb_custom_audience/transform.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
const lodash = require('lodash');
const get = require('get-value');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const {
InstrumentationError,
TransformationError,
ConfigurationError,
} = require('@rudderstack/integrations-lib');
const {
defaultRequestConfig,
defaultPostRequestConfig,
defaultDeleteRequestConfig,
checkSubsetOfArray,
isDefinedAndNotNullAndNotEmpty,
returnArrayOfSubarrays,
Expand All @@ -22,36 +15,20 @@
batchingWithPayloadSize,
generateAppSecretProof,
} = require('./util');
const {
getEndPoint,
schemaFields,
USER_ADD,
USER_DELETE,
typeFields,
subTypeFields,
} = require('./config');
const { schemaFields, USER_ADD, USER_DELETE } = require('./config');

const { MappedToDestinationKey } = require('../../../constants');
const { processRecordInputs, responseBuilderSimple, getDataSource } = require('./transformV2');

const responseBuilderSimple = (payload, audienceId) => {
if (payload) {
const responseParams = payload.responseField;
const response = defaultRequestConfig();
response.endpoint = getEndPoint(audienceId);

if (payload.operationCategory === 'add') {
response.method = defaultPostRequestConfig.requestMethod;
function extraKeysPresent(dictionary, keyList) {
// eslint-disable-next-line no-restricted-syntax
for (const key in dictionary) {
if (!keyList.includes(key)) {
return true;

Check warning on line 27 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#L27

Added line #L27 was not covered by tests
}
if (payload.operationCategory === 'remove') {
response.method = defaultDeleteRequestConfig.requestMethod;
}

response.params = responseParams;
return response;
}
// fail-safety for developer error
throw new TransformationError(`Payload could not be constructed`);
};
return false;
}

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

Expand Down Expand Up @@ -102,7 +79,6 @@
const prepareParams = {};
// creating the parameters field
const paramsPayload = {};
const dataSource = {};

prepareParams.access_token = accessToken;

Expand All @@ -118,13 +94,7 @@
}
// creating the data_source block

if (type && type !== 'NA' && typeFields.includes(type)) {
dataSource.type = type;
}

if (subType && subType !== 'NA' && subTypeFields.includes(subType)) {
dataSource.sub_type = subType;
}
const dataSource = getDataSource(type, subType);
if (Object.keys(dataSource).length > 0) {
paramsPayload.data_source = dataSource;
}
Expand Down Expand Up @@ -250,6 +220,7 @@
),
);
}

toSendEvents.forEach((sendEvent) => {
respList.push(responseBuilderSimple(sendEvent, operationAudienceId));
});
Expand All @@ -265,7 +236,29 @@
const process = (event) => processEvent(event.message, event.destination);

const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(inputs, process, reqMetadata);
const respList = [];
const groupedInputs = lodash.groupBy(inputs, (input) => input.message.type?.toLowerCase());
let transformedRecordEvent = [];
let transformedAudienceEvent = [];

const eventTypes = ['record', 'audiencelist'];
if (extraKeysPresent(groupedInputs, eventTypes)) {
throw new ConfigurationError('unsupported events present in the event');

Check warning on line 246 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#L246

Added line #L246 was not covered by tests
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
}

if (groupedInputs.record) {
transformedRecordEvent = await processRecordInputs(groupedInputs.record, reqMetadata);
}

if (groupedInputs.audiencelist) {
transformedAudienceEvent = await simpleProcessRouterDest(
groupedInputs.audiencelist,
process,
reqMetadata,
);
}

ItsSudip marked this conversation as resolved.
Show resolved Hide resolved
respList.push(...transformedRecordEvent, ...transformedAudienceEvent);
return flattenMap(respList);
};

Expand Down
Loading
Loading